Add misc-unused-alias-decls check that currently finds unused namespace

alias declarations. In the future, we might want to reuse it to also
fine unsed using declarations and such.

llvm-svn: 243754
This commit is contained in:
Daniel Jasper 2015-07-31 16:08:10 +00:00
parent 8a7ef3b2ee
commit bb42b03021
5 changed files with 115 additions and 0 deletions

View File

@ -14,6 +14,7 @@ add_clang_library(clangTidyMiscModule
StaticAssertCheck.cpp
SwappedArgumentsCheck.cpp
UndelegatedConstructor.cpp
UnusedAliasDeclsCheck.cpp
UnusedParametersCheck.cpp
UnusedRAIICheck.cpp
UniqueptrResetReleaseCheck.cpp

View File

@ -23,6 +23,7 @@
#include "SwappedArgumentsCheck.h"
#include "UndelegatedConstructor.h"
#include "UniqueptrResetReleaseCheck.h"
#include "UnusedAliasDeclsCheck.h"
#include "UnusedParametersCheck.h"
#include "UnusedRAIICheck.h"
#include "UseOverrideCheck.h"
@ -59,6 +60,8 @@ public:
"misc-undelegated-constructor");
CheckFactories.registerCheck<UniqueptrResetReleaseCheck>(
"misc-uniqueptr-reset-release");
CheckFactories.registerCheck<UnusedAliasDeclsCheck>(
"misc-unused-alias-decls");
CheckFactories.registerCheck<UnusedParametersCheck>(
"misc-unused-parameters");
CheckFactories.registerCheck<UnusedRAIICheck>("misc-unused-raii");

View File

@ -0,0 +1,63 @@
//===--- UnusedAliasDeclsCheck.cpp - clang-tidy----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "UnusedAliasDeclsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
// FIXME: Move this to ASTMatchers.h.
const ast_matchers::internal::VariadicDynCastAllOfMatcher<
Decl, NamespaceAliasDecl> namespaceAliasDecl;
void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(namespaceAliasDecl().bind("alias"), this);
Finder->addMatcher(nestedNameSpecifier().bind("nns"), this);
}
void UnusedAliasDeclsCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *AliasDecl = Result.Nodes.getNodeAs<Decl>("alias")) {
// We cannot do anything about headers (yet), as the alias declarations used
// in one header could be used by some other translation unit.
if (!Result.SourceManager->isInMainFile(AliasDecl->getLocation()))
return;
FoundDecls[AliasDecl] = CharSourceRange::getCharRange(
AliasDecl->getLocStart(),
Lexer::findLocationAfterToken(
AliasDecl->getLocEnd(), tok::semi, *Result.SourceManager,
Result.Context->getLangOpts(),
/*SkipTrailingWhitespaceAndNewLine=*/true));
return;
}
if (const auto *NestedName =
Result.Nodes.getNodeAs<NestedNameSpecifier>("nns")) {
if (const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
FoundDecls[AliasDecl] = CharSourceRange();
}
}
}
void UnusedAliasDeclsCheck::onEndOfTranslationUnit() {
for (const auto &FoundDecl : FoundDecls) {
if (!FoundDecl.second.isValid())
continue;
diag(FoundDecl.first->getLocation(), "this namespace alias decl is unused")
<< FixItHint::CreateRemoval(FoundDecl.second);
}
}
} // namespace tidy
} // namespace clang

View File

@ -0,0 +1,35 @@
//===--- UnusedAliasDeclsCheck.h - clang-tidy--------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
/// \brief Finds unused namespace alias declarations.
class UnusedAliasDeclsCheck : public ClangTidyCheck {
public:
UnusedAliasDeclsCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
void onEndOfTranslationUnit();
private:
llvm::DenseMap<const Decl *, CharSourceRange> FoundDecls;
};
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H

View File

@ -0,0 +1,13 @@
// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-unused-alias-decls %t
// REQUIRES: shell
namespace my_namespace {
class C {};
}
namespace unused_alias = ::my_namespace; // eol-comments aren't removed (yet)
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: this namespace alias decl is unused
// CHECK-FIXES: {{^}}// eol-comments aren't removed (yet)
namespace used_alias = ::my_namespace;
void f() { used_alias::C c; }