Revert "[clang-itdy] Simplify virtual near-miss check"
authorStephen Kelly <steveire@gmail.com>
Sat, 27 Feb 2021 19:30:38 +0000 (19:30 +0000)
committerStephen Kelly <steveire@gmail.com>
Sat, 27 Feb 2021 19:30:38 +0000 (19:30 +0000)
This reverts commit 9a4b574dd6a07d6811356529ebb8a3f15d6e40a2.

clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.cpp
clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.h
clang-tools-extra/test/clang-tidy/checkers/bugprone-virtual-near-miss.cpp

index dc810c6..150b517 100644 (file)
@@ -216,9 +216,10 @@ bool VirtualNearMissCheck::isOverriddenByDerivedClass(
 
 void VirtualNearMissCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
-      cxxMethodDecl(unless(anyOf(isOverride(), cxxConstructorDecl(),
-                                 cxxDestructorDecl(), cxxConversionDecl(),
-                                 isStatic(), isOverloadedOperator())))
+      cxxMethodDecl(
+          unless(anyOf(isOverride(), isImplicit(), cxxConstructorDecl(),
+                       cxxDestructorDecl(), cxxConversionDecl(), isStatic(),
+                       isOverloadedOperator())))
           .bind("method"),
       this);
 }
@@ -233,15 +234,7 @@ void VirtualNearMissCheck::check(const MatchFinder::MatchResult &Result) {
   assert(DerivedRD);
 
   for (const auto &BaseSpec : DerivedRD->bases()) {
-    const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl();
-    if (const auto *TST =
-            dyn_cast<TemplateSpecializationType>(BaseSpec.getType())) {
-      auto TN = TST->getTemplateName();
-      BaseRD =
-          dyn_cast<CXXRecordDecl>(TN.getAsTemplateDecl()->getTemplatedDecl());
-    }
-
-    if (BaseRD) {
+    if (const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl()) {
       for (const auto *BaseMD : BaseRD->methods()) {
         if (!isPossibleToBeOverridden(BaseMD))
           continue;
@@ -257,12 +250,16 @@ void VirtualNearMissCheck::check(const MatchFinder::MatchResult &Result) {
             auto Range = CharSourceRange::getTokenRange(
                 SourceRange(DerivedMD->getLocation()));
 
-            diag(DerivedMD->getBeginLoc(),
-                 "method '%0' has a similar name and the same signature as "
-                 "virtual method '%1'; did you mean to override it?")
+            bool ApplyFix = !BaseMD->isTemplateInstantiation() &&
+                            !DerivedMD->isTemplateInstantiation();
+            auto Diag =
+                diag(DerivedMD->getBeginLoc(),
+                     "method '%0' has a similar name and the same signature as "
+                     "virtual method '%1'; did you mean to override it?")
                 << DerivedMD->getQualifiedNameAsString()
-                << BaseMD->getQualifiedNameAsString()
-                << FixItHint::CreateReplacement(Range, BaseMD->getName());
+                << BaseMD->getQualifiedNameAsString();
+            if (ApplyFix)
+              Diag << FixItHint::CreateReplacement(Range, BaseMD->getName());
           }
         }
       }
index ec90251..69ae278 100644 (file)
@@ -32,9 +32,6 @@ public:
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
-  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
-    return TK_IgnoreUnlessSpelledInSource;
-  }
 
 private:
   /// Check if the given method is possible to be overridden by some other
index f3f8d32..553d2f4 100644 (file)
@@ -44,8 +44,9 @@ template <typename T>
 struct TDerived : TBase<T> {
   virtual void tfunk(T t);
   // Should not apply fix for template.
-  // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: method 'TDerived::tfunk' has {{.*}} 'TBase::tfunc'
-  // CHECK-FIXES: virtual void tfunc(T t);
+  // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: method 'TDerived<double>::tfunk' has {{.*}} 'TBase<double>::tfunc'
+  // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: method 'TDerived<int>::tfunk' has {{.*}} 'TBase<int>::tfunc'
+  // CHECK-FIXES: virtual void tfunk(T t);
 };
 
 TDerived<int> T1;