[PATCH] D96224: [clang-itdy] Simplify virtual near-miss check

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 20 12:47:12 PST 2021


steveire updated this revision to Diff 325232.
steveire added a comment.

Update


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96224/new/

https://reviews.llvm.org/D96224

Files:
  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: clang-tools-extra/test/clang-tidy/checkers/bugprone-virtual-near-miss.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-virtual-near-miss.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-virtual-near-miss.cpp
@@ -44,9 +44,8 @@
 struct TDerived : TBase<T> {
   virtual void tfunk(T t);
   // Should not apply fix for template.
-  // 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);
+  // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: method 'TDerived::tfunk' has {{.*}} 'TBase::tfunc'
+  // CHECK-FIXES: virtual void tfunc(T t);
 };
 
 TDerived<int> T1;
Index: clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.h
@@ -32,6 +32,9 @@
   }
   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: clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.cpp
@@ -216,10 +216,9 @@
 
 void VirtualNearMissCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
-      cxxMethodDecl(
-          unless(anyOf(isOverride(), isImplicit(), cxxConstructorDecl(),
-                       cxxDestructorDecl(), cxxConversionDecl(), isStatic(),
-                       isOverloadedOperator())))
+      cxxMethodDecl(unless(anyOf(isOverride(), cxxConstructorDecl(),
+                                 cxxDestructorDecl(), cxxConversionDecl(),
+                                 isStatic(), isOverloadedOperator())))
           .bind("method"),
       this);
 }
@@ -234,7 +233,15 @@
   assert(DerivedRD);
 
   for (const auto &BaseSpec : DerivedRD->bases()) {
-    if (const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl()) {
+    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) {
       for (const auto *BaseMD : BaseRD->methods()) {
         if (!isPossibleToBeOverridden(BaseMD))
           continue;
@@ -250,16 +257,12 @@
             auto Range = CharSourceRange::getTokenRange(
                 SourceRange(DerivedMD->getLocation()));
 
-            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?")
+            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();
-            if (ApplyFix)
-              Diag << FixItHint::CreateReplacement(Range, BaseMD->getName());
+                << BaseMD->getQualifiedNameAsString()
+                << FixItHint::CreateReplacement(Range, BaseMD->getName());
           }
         }
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96224.325232.patch
Type: text/x-patch
Size: 4085 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210220/145b881b/attachment-0001.bin>


More information about the cfe-commits mailing list