[clang-tools-extra] r266866 - clang-tidy: [misc-unused-using-decls] Support template types.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 20 02:48:59 PDT 2016


Author: djasper
Date: Wed Apr 20 04:48:56 2016
New Revision: 266866

URL: http://llvm.org/viewvc/llvm-project?rev=266866&view=rev
Log:
clang-tidy: [misc-unused-using-decls] Support template types.

This fixes llvm.org/PR27429.

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp?rev=266866&r1=266865&r2=266866&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp Wed Apr 20 04:48:56 2016
@@ -20,8 +20,9 @@ namespace misc {
 
 void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
-  Finder->addMatcher(recordType(hasDeclaration(namedDecl().bind("used"))),
-                     this);
+  auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
+  Finder->addMatcher(loc(recordType(DeclMatcher)), this);
+  Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
 }
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
@@ -34,7 +35,7 @@ void UnusedUsingDeclsCheck::check(const
         Using->shadow_begin()->getTargetDecl()->getCanonicalDecl();
 
     // FIXME: Handle other target types.
-    if (!isa<RecordDecl>(TargetDecl))
+    if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl))
       return;
 
     FoundDecls[TargetDecl] = Using;
@@ -53,6 +54,9 @@ void UnusedUsingDeclsCheck::check(const
   // FIXME: This currently doesn't look at whether the type reference is
   // actually found with the help of the using declaration.
   if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) {
+    if (const auto *Specialization =
+            dyn_cast<ClassTemplateSpecializationDecl>(Used))
+      Used = Specialization->getSpecializedTemplate();
     auto I = FoundDecls.find(Used->getCanonicalDecl());
     if (I != FoundDecls.end())
       I->second = nullptr;

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp?rev=266866&r1=266865&r2=266866&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp Wed Apr 20 04:48:56 2016
@@ -8,6 +8,8 @@ class B;
 class C;
 class D;
 class D { public: static int i; };
+template <typename T> class E {};
+template <typename T> class F {};
 }
 
 // ----- Using declarations -----
@@ -18,11 +20,16 @@ using n::A; // A
 using n::B;
 using n::C;
 using n::D;
+using n::E; // E
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'E' is unused
+// CHECK-FIXES: {{^}}// E
+using n::F;
 
 // ----- Usages -----
 void f(B b);
 void g() {
   vector<C> data;
   D::i = 1;
+  F<int> f;
 }
 




More information about the cfe-commits mailing list