hokein updated this revision to Diff 56566.
hokein added a comment.

Address remaining comments.


http://reviews.llvm.org/D20018

Files:
  clang-tidy/misc/UnusedUsingDeclsCheck.cpp
  clang-tidy/misc/UnusedUsingDeclsCheck.h
  test/clang-tidy/misc-unused-using-decls.cpp

Index: test/clang-tidy/misc-unused-using-decls.cpp
===================================================================
--- test/clang-tidy/misc-unused-using-decls.cpp
+++ test/clang-tidy/misc-unused-using-decls.cpp
@@ -10,6 +10,32 @@
 class D { public: static int i; };
 template <typename T> class E {};
 template <typename T> class F {};
+class G { public: static void func() {} };
+class H { public: static int i; };
+class I {
+ public:
+  static int ii;
+};
+
+class Base {
+ public:
+  void f();
+};
+
+D UsedInstance;
+D UnusedInstance;
+
+int UsedFunc() { return 1; }
+int UnusedFunc() { return 1; }
+template <typename T> int UsedTemplateFunc() { return 1; }
+template <typename T> int UnusedTemplateFunc() { return 1; }
+
+class ostream {
+public:
+  ostream &operator<<(ostream &(*PF)(ostream &));
+};
+extern ostream cout;
+ostream &endl(ostream &os);
 }
 
 // ----- Using declarations -----
@@ -24,12 +50,37 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'E' is unused
 // CHECK-FIXES: {{^}}// E
 using n::F;
+using n::G;
+using n::H;
+using n::I;
+int I::ii = 1;
+class Derived : public n::Base {
+ public:
+  using Base::f;
+};
+using n::UsedInstance;
+using n::UsedFunc;
+using n::UsedTemplateFunc;
+using n::UnusedInstance; // UnusedInstance
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'UnusedInstance' is unused
+// CHECK-FIXES: {{^}}// UnusedInstance
+using n::UnusedFunc; // UnusedFunc
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'UnusedFunc' is unused
+// CHECK-FIXES: {{^}}// UnusedFunc
+using n::cout;
+using n::endl;
 
 // ----- Usages -----
 void f(B b);
 void g() {
   vector<C> data;
   D::i = 1;
   F<int> f;
+  void (*func)() = &G::func;
+  int *i = &H::i;
+  UsedInstance.i;
+  UsedFunc();
+  UsedTemplateFunc<int>();
+  cout << endl;
 }
 
Index: clang-tidy/misc/UnusedUsingDeclsCheck.h
===================================================================
--- clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -30,6 +30,8 @@
   void onEndOfTranslationUnit() override;
 
 private:
+  void removeFromFoundDecls(const Decl *D);
+
   llvm::DenseMap<const Decl*, const UsingDecl*> FoundDecls;
   llvm::DenseMap<const Decl*, CharSourceRange> FoundRanges;
 };
Index: clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===================================================================
--- clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -23,6 +23,7 @@
   auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
   Finder->addMatcher(loc(recordType(DeclMatcher)), this);
   Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
+  Finder->addMatcher(declRefExpr().bind("used"), this);
 }
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
@@ -34,8 +35,13 @@
     const auto *TargetDecl =
         Using->shadow_begin()->getTargetDecl()->getCanonicalDecl();
 
-    // FIXME: Handle other target types.
-    if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl))
+    // Ignores using-declarations defined in class definition.
+    if (isa<CXXRecordDecl>(TargetDecl->getDeclContext()))
+      return;
+
+    if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl) &&
+        !isa<FunctionDecl>(TargetDecl) && !isa<VarDecl>(TargetDecl) &&
+        !isa<FunctionTemplateDecl>(TargetDecl))
       return;
 
     FoundDecls[TargetDecl] = Using;
@@ -57,10 +63,26 @@
     if (const auto *Specialization =
             dyn_cast<ClassTemplateSpecializationDecl>(Used))
       Used = Specialization->getSpecializedTemplate();
-    auto I = FoundDecls.find(Used->getCanonicalDecl());
-    if (I != FoundDecls.end())
-      I->second = nullptr;
+    removeFromFoundDecls(Used);
+    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())
+        removeFromFoundDecls(FDT);
+      else
+        removeFromFoundDecls(FD);
+    } else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
+      removeFromFoundDecls(VD);
+    }
+  }
+}
+
+void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
+  auto I = FoundDecls.find(D->getCanonicalDecl());
+  if (I != FoundDecls.end())
+    I->second = nullptr;
 }
 
 void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to