[clang-tools-extra] r277444 - [clang-tidy] Fix an unused-using-decl false positive about template arguments in

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 2 04:26:37 PDT 2016


Author: hokein
Date: Tue Aug  2 06:26:35 2016
New Revision: 277444

URL: http://llvm.org/viewvc/llvm-project?rev=277444&view=rev
Log:
[clang-tidy] Fix an unused-using-decl false positive about template arguments in
function call expression.

Summary:
The check doesn't mark the template argument as used when the template
argument is a template.

Reviewers: djasper, alexfh

Subscribers: klimek, cfe-commits

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

Added:
    clang-tools-extra/trunk/test/clang-tidy/Inputs/unused-using-decls.h
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=277444&r1=277443&r2=277444&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp Tue Aug  2 06:26:35 2016
@@ -37,6 +37,11 @@ void UnusedUsingDeclsCheck::registerMatc
   Finder->addMatcher(declRefExpr().bind("used"), this);
   Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))),
                      this);
+  Finder->addMatcher(
+      callExpr(hasDeclaration(functionDecl(hasAnyTemplateArgument(
+          anyOf(refersToTemplate(templateName().bind("used")),
+                refersToDeclaration(functionDecl().bind("used"))))))),
+      this);
 }
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
@@ -71,20 +76,27 @@ void UnusedUsingDeclsCheck::check(const
       Contexts.push_back(Context);
     return;
   }
-
   // Mark using declarations as used by setting FoundDecls' value to zero. As
   // the AST is walked in order, usages are only marked after a the
   // corresponding using declaration has been found.
   // 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))
+    if (const auto *FD = dyn_cast<FunctionDecl>(Used)) {
+      removeFromFoundDecls(FD->getPrimaryTemplate());
+    } else if (const auto *Specialization =
+                   dyn_cast<ClassTemplateSpecializationDecl>(Used)) {
       Used = Specialization->getSpecializedTemplate();
+    }
     removeFromFoundDecls(Used);
     return;
   }
 
+  if (const auto *Used = Result.Nodes.getNodeAs<TemplateName>("used")) {
+    removeFromFoundDecls(Used->getAsTemplateDecl());
+    return;
+  }
+
   if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) {
     if (const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
       if (const auto *FDT = FD->getPrimaryTemplate())
@@ -109,6 +121,8 @@ void UnusedUsingDeclsCheck::check(const
 }
 
 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
+  if (!D)
+    return;
   // FIXME: Currently, we don't handle the using-decls being used in different
   // scopes (such as different namespaces, different functions). Instead of
   // giving an incorrect message, we mark all of them as used.

Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/unused-using-decls.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/unused-using-decls.h?rev=277444&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/unused-using-decls.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/unused-using-decls.h Tue Aug  2 06:26:35 2016
@@ -0,0 +1,11 @@
+class MyClass {
+public:
+  template <template <typename> class S, typename T>
+  S<T> *func1(T *a) {
+    return new S<T>();
+  }
+  template <typename T, T (*S)()>
+  void func2(T a) {
+    S();
+  }
+};

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=277444&r1=277443&r2=277444&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 Aug  2 06:26:35 2016
@@ -1,4 +1,5 @@
-// RUN: %check_clang_tidy %s misc-unused-using-decls %t -- -- -fno-delayed-template-parsing
+// RUN: %check_clang_tidy %s misc-unused-using-decls %t -- -- -fno-delayed-template-parsing -isystem %S/Inputs/
+
 
 // ----- Definitions -----
 template <typename T> class vector {};
@@ -54,6 +55,16 @@ enum Color4 { Blue };
 
 }  // namespace n
 
+#include "unused-using-decls.h"
+namespace ns {
+template <typename T>
+class AA {
+  T t;
+};
+template <typename T>
+T ff() { T t; return t; }
+} // namespace ns
+
 // ----- Using declarations -----
 // eol-comments aren't removed (yet)
 using n::A; // A
@@ -136,6 +147,9 @@ using n::Color2;
 using n::Color3;
 using n::Blue;
 
+using ns::AA;
+using ns::ff;
+
 // ----- Usages -----
 void f(B b);
 void g() {
@@ -151,4 +165,9 @@ void g() {
   Color2 color2;
   int t1 = Color3::Yellow;
   int t2 = Blue;
+
+  MyClass a;
+  int t3 = 0;
+  a.func1<AA>(&t3);
+  a.func2<int, ff>(t3);
 }




More information about the cfe-commits mailing list