[clang-tools-extra] r243754 - Add misc-unused-alias-decls check that currently finds unused namespace

Daniel Jasper djasper at google.com
Fri Jul 31 09:08:11 PDT 2015


Author: djasper
Date: Fri Jul 31 11:08:10 2015
New Revision: 243754

URL: http://llvm.org/viewvc/llvm-project?rev=243754&view=rev
Log:
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.

Added:
    clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h
    clang-tools-extra/trunk/test/clang-tidy/misc-unused-alias-decls.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt?rev=243754&r1=243753&r2=243754&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt Fri Jul 31 11:08:10 2015
@@ -14,6 +14,7 @@ add_clang_library(clangTidyMiscModule
   StaticAssertCheck.cpp
   SwappedArgumentsCheck.cpp
   UndelegatedConstructor.cpp
+  UnusedAliasDeclsCheck.cpp
   UnusedParametersCheck.cpp
   UnusedRAIICheck.cpp
   UniqueptrResetReleaseCheck.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp?rev=243754&r1=243753&r2=243754&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp Fri Jul 31 11:08:10 2015
@@ -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");

Added: clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.cpp?rev=243754&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.cpp Fri Jul 31 11:08:10 2015
@@ -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

Added: clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h?rev=243754&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h Fri Jul 31 11:08:10 2015
@@ -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
+

Added: clang-tools-extra/trunk/test/clang-tidy/misc-unused-alias-decls.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-unused-alias-decls.cpp?rev=243754&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-alias-decls.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-alias-decls.cpp Fri Jul 31 11:08:10 2015
@@ -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; }





More information about the cfe-commits mailing list