[PATCH] D66365: [clang-tidy] [readability-convert-member-functions-to-static] ignore functions that hide base class methods

Matthias Gehre via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 16 13:32:30 PDT 2019


mgehre created this revision.
mgehre added reviewers: aaron.ballman, Eugene.Zelenko.
Herald added a subscriber: xazax.hun.
Herald added a project: clang.

Fixes bug https://bugs.llvm.org/show_bug.cgi?id=43002


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66365

Files:
  clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
  clang-tools-extra/test/clang-tidy/readability-convert-member-functions-to-static.cpp


Index: clang-tools-extra/test/clang-tidy/readability-convert-member-functions-to-static.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/readability-convert-member-functions-to-static.cpp
+++ clang-tools-extra/test/clang-tidy/readability-convert-member-functions-to-static.cpp
@@ -216,3 +216,21 @@
     return;
   }
 };
+
+class HiddingBase {
+  int f();
+
+  static int g();
+};
+
+class HiddingDerived : public HiddingBase {
+  int f() {
+    return 0;
+  }
+
+  int g() {
+    // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'g' can be made static
+    // CHECK-FIXES: {{^}}  static int g() {
+    return 0;
+  }
+};
Index: clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
+++ clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
@@ -71,6 +71,31 @@
   return UsageOfThis.Used;
 }
 
+/// Does any base class have a non-static method with the same name?
+AST_MATCHER(CXXMethodDecl, isHiding) {
+  const IdentifierInfo *ThisIdentifier = Node.getIdentifier();
+  if (!ThisIdentifier)
+    return false;
+
+  const CXXRecordDecl *Record = Node.getParent();
+
+  auto StaticOrDifferentName = [ThisIdentifier](const CXXMethodDecl *Method) {
+                          if (Method->isStatic())
+                            return true;
+
+                          const IdentifierInfo *Ident = Method->getIdentifier();
+                          if (!Ident)
+                            return true;
+
+                          return Ident->getName() != ThisIdentifier->getName();
+                        };
+
+  return !Record->forallBases([StaticOrDifferentName](const CXXRecordDecl *Base) {
+    return llvm::all_of(Base->methods(),
+                        StaticOrDifferentName);
+  });
+}
+
 void ConvertMemberFunctionsToStatic::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       cxxMethodDecl(
@@ -86,7 +111,8 @@
                                           // depending on template base class.
                       ),
               isInsideMacroDefinition(),
-              hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
+              hasCanonicalDecl(isInsideMacroDefinition()), isHiding(),
+              usesThis())))
           .bind("x"),
       this);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66365.215667.patch
Type: text/x-patch
Size: 2462 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190816/4094fd13/attachment.bin>


More information about the cfe-commits mailing list