[PATCH] D79674: [clang-tidy] Better support for Override function in RenamerClangTidy based checks
Nathan James via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat May 9 11:40:59 PDT 2020
njames93 created this revision.
njames93 added reviewers: aaron.ballman, gribozavr2, alexfh.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
Methods that override virtual methods will now get renamed if the initial virtual method has a name violation.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D79674
Files:
clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
@@ -267,14 +267,32 @@
virtual ~AOverridden() = default;
virtual void BadBaseMethod() = 0;
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: invalid case style for virtual method 'BadBaseMethod'
+ // CHECK-FIXES: {{^}} virtual void v_Bad_Base_Method() = 0;
};
class COverriding : public AOverridden {
public:
// Overriding a badly-named base isn't a new violation.
void BadBaseMethod() override {}
+ // CHECK-FIXES: {{^}} void v_Bad_Base_Method() override {}
+
+ void foo() {
+ BadBaseMethod();
+ // CHECK-FIXES: {{^}} v_Bad_Base_Method();
+ this->BadBaseMethod();
+ // CHECK-FIXES: {{^}} this->v_Bad_Base_Method();
+ AOverridden::BadBaseMethod();
+ // CHECK-FIXES: {{^}} AOverridden::v_Bad_Base_Method();
+ COverriding::BadBaseMethod();
+ // CHECK-FIXES: {{^}} COverriding::v_Bad_Base_Method();
+ }
};
+void VirtualCall(AOverridden &a_vItem) {
+ a_vItem.BadBaseMethod();
+ // CHECK-FIXES: {{^}} a_vItem.v_Bad_Base_Method();
+}
+
template <typename derived_t>
class CRTPBase {
public:
Index: clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
+++ clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
@@ -127,6 +127,26 @@
this));
}
+/// Returns the function that \p Method is overridding. If There are none or
+/// multiple overrides it returns nullptr. If the overridden function itself is
+/// overridding then it will recurse up to find the first decl of the function.
+static const CXXMethodDecl *getOverrideMethod(const CXXMethodDecl *Method) {
+ if (Method->size_overridden_methods() != 1)
+ return nullptr; // no overrides
+ while (true) {
+ const CXXMethodDecl *Next = *Method->begin_overridden_methods();
+ assert(Next && "Overridden method shouldn't be null");
+ if (Next->size_overridden_methods() == 0) {
+ return Next;
+ }
+ if (Next->size_overridden_methods() == 1) {
+ Method = Next;
+ continue;
+ }
+ return nullptr; // Multiple overrides
+ }
+}
+
void RenamerClangTidyCheck::addUsage(
const RenamerClangTidyCheck::NamingCheckId &Decl, SourceRange Range,
SourceManager *SourceMgr) {
@@ -160,7 +180,10 @@
void RenamerClangTidyCheck::addUsage(const NamedDecl *Decl, SourceRange Range,
SourceManager *SourceMgr) {
-
+ if (const auto *Method = dyn_cast<CXXMethodDecl>(Decl)) {
+ if (const CXXMethodDecl *Overridden = getOverrideMethod(Method))
+ Decl = Overridden;
+ }
return addUsage(RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(),
Decl->getNameAsString()),
Range, SourceMgr);
@@ -390,6 +413,14 @@
}
}
+ // Fix overridden methods
+ if (const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
+ if (const CXXMethodDecl *Overridden = getOverrideMethod(Method)) {
+ addUsage(Overridden, Method->getLocation());
+ return; // Don't try to add the actual decl as a Failure.
+ }
+ }
+
// Ignore ClassTemplateSpecializationDecl which are creating duplicate
// replacements with CXXRecordDecl.
if (isa<ClassTemplateSpecializationDecl>(Decl))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79674.263029.patch
Type: text/x-patch
Size: 3706 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200509/fc1e89e3/attachment.bin>
More information about the cfe-commits
mailing list