[clang-tools-extra] r283879 - [clang-tidy] Fix template agrument false positives in unused-using-decls.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 11 06:50:35 PDT 2016


Author: hokein
Date: Tue Oct 11 08:50:34 2016
New Revision: 283879

URL: http://llvm.org/viewvc/llvm-project?rev=283879&view=rev
Log:
[clang-tidy] Fix template agrument false positives in unused-using-decls.

Summary:
* Fix a false postive when an using class is used in an explicit template instantiation.
* Fix a false postive when an using template class is used as template argument.

Reviewers: aaron.ballman

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25437

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=283879&r1=283878&r2=283879&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp Tue Oct 11 08:50:34 2016
@@ -42,6 +42,8 @@ void UnusedUsingDeclsCheck::registerMatc
           anyOf(refersToTemplate(templateName().bind("used")),
                 refersToDeclaration(functionDecl().bind("used"))))))),
       this);
+  Finder->addMatcher(loc(templateSpecializationType(
+      hasAnyTemplateArgument(templateArgument().bind("used")))), this);
 }
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
@@ -91,6 +93,18 @@ void UnusedUsingDeclsCheck::check(const
     return;
   }
 
+  if (const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>("used")) {
+    // FIXME: Support non-type template parameters.
+    if (Used->getKind() == TemplateArgument::Template) {
+      if (const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
+        removeFromFoundDecls(TD);
+    } else if (Used->getKind() == TemplateArgument::Type) {
+      if (auto *RD = Used->getAsType()->getAsCXXRecordDecl())
+        removeFromFoundDecls(RD);
+    }
+    return;
+  }
+
   if (const auto *Used = Result.Nodes.getNodeAs<TemplateName>("used")) {
     removeFromFoundDecls(Used->getAsTemplateDecl());
     return;

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=283879&r1=283878&r2=283879&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 Tue Oct 11 08:50:34 2016
@@ -21,6 +21,16 @@ template <typename T> class J {};
 class G;
 class H;
 
+template <typename T> class K {};
+template <template <typename> class S>
+class L {};
+
+template <typename T> class M {};
+class N {};
+
+template <int T> class P {};
+const int Constant = 0;
+
 class Base {
  public:
   void f();
@@ -150,6 +160,14 @@ using n::Blue;
 using ns::AA;
 using ns::ff;
 
+using n::K;
+
+using n::N;
+
+// FIXME: Currently non-type template arguments are not supported.
+using n::Constant;
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'Constant' is unused
+
 // ----- Usages -----
 void f(B b);
 void g() {
@@ -170,4 +188,16 @@ void g() {
   int t3 = 0;
   a.func1<AA>(&t3);
   a.func2<int, ff>(t3);
+
+  n::L<K> l;
 }
+
+template<class T>
+void h(n::M<T>* t) {}
+// n::N is used the explicit template instantiation.
+template void h(n::M<N>* t);
+
+// Test on Non-type template arguments.
+template <int T>
+void i(n::P<T>* t) {}
+template void i(n::P<Constant>* t);




More information about the cfe-commits mailing list