summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-07-31 16:08:10 +0000
committerDaniel Jasper <djasper@google.com>2015-07-31 16:08:10 +0000
commitbb42b030219710676ecb224bd3748641ff828c86 (patch)
tree8f40c2ab37d8680540b7ac9d0ec59b1aede23c6a /clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
parent8a7ef3b2eec5e6046a3aab36521813c8ea6093e0 (diff)
downloadbcm5719-llvm-bb42b030219710676ecb224bd3748641ff828c86.tar.gz
bcm5719-llvm-bb42b030219710676ecb224bd3748641ff828c86.zip
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
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
new file mode 100644
index 00000000000..a56c1bfa8bd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
@@ -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
OpenPOWER on IntegriCloud