[PATCH] D82059: [clang-tidy] RenamerClangTidy group redecls into 1 warning.

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 17 15:40:12 PDT 2020


njames93 created this revision.
njames93 added reviewers: aaron.ballman, alexfh, jbcoe, gribozavr2.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

This changes the behavious of `RenamerClangTidyCheck` based checks by grouping declarations of the same thing into 1 warning where it is first declared.
This cleans up clang-tidy output and prevents issues where 1 fix-it couldn't be applied, yet all other warnings(and fix-its) for the same declaration would be applied.
The old behaviour of forward declaring a class without defining it isn't affected, i.e. no warnings will be emitted for that case.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82059

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
@@ -214,17 +214,16 @@
 // CHECK-FIXES: {{^}}    static int ClassMember2;{{$}}
 };
 class my_class;
-// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'my_class'
+// No warning needed here as this is tied to the previous declaration.
+// Just make sure the fix is applied.
 // CHECK-FIXES: {{^}}class CMyClass;{{$}}
 
 class my_forward_declared_class; // No warning should be triggered.
 
 const int my_class::classConstant = 4;
-// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for class constant 'classConstant'
 // CHECK-FIXES: {{^}}const int CMyClass::kClassConstant = 4;{{$}}
 
 int my_class::ClassMember_2 = 5;
-// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: invalid case style for class member 'ClassMember_2'
 // CHECK-FIXES: {{^}}int CMyClass::ClassMember2 = 5;{{$}}
 
 class my_derived_class : public virtual my_class {};
@@ -500,7 +499,6 @@
 
 template<typename t_t>
 char const a<t_t>::MyConstClass_string[] = "123";
-// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for class constant 'MyConstClass_string'
 // CHECK-FIXES: {{^}}char const a<t_t>::kMyConstClassString[] = "123";{{$}}
 
 template <template <typename> class A> struct b { A<int> c; };
@@ -545,3 +543,22 @@
 // CHECK-FIXES: {{^}}void QualifiedTypeLocTest(const this_structure &);{{$}}
 void QualifiedTypeLocTest(volatile THIS___Structure &);
 // CHECK-FIXES: {{^}}void QualifiedTypeLocTest(volatile this_structure &);{{$}}
+
+namespace redecls {
+// We only want the warning to show up once here for the first decl.
+// CHECK-MESSAGES: :[[@LINE+1]]:6: warning: invalid case style for global function 'badNamedFunction'
+void badNamedFunction();
+void badNamedFunction();
+void badNamedFunction(){}
+//      CHECK-FIXES: {{^}}void BadNamedFunction();
+// CHECK-FIXES-NEXT: {{^}}void BadNamedFunction();
+// CHECK-FIXES-NEXT: {{^}}void BadNamedFunction(){}
+void ReferenceBadNamedFunction() {
+  auto l_Ptr = badNamedFunction;
+  // CHECK-FIXES: {{^}}  auto l_Ptr = BadNamedFunction;
+  l_Ptr();
+  badNamedFunction();
+  // CHECK-FIXES: {{^}}  BadNamedFunction();
+}
+
+} // namespace redecls
\ No newline at end of file
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
@@ -168,7 +168,7 @@
 
 void RenamerClangTidyCheck::addUsage(const NamedDecl *Decl, SourceRange Range,
                                      SourceManager *SourceMgr) {
-
+  Decl = cast<NamedDecl>(Decl->getCanonicalDecl());
   return addUsage(RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(),
                                                        Decl->getNameAsString()),
                   Range, SourceMgr);
@@ -380,6 +380,14 @@
     if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
       return;
 
+    {
+      const auto *Canonical = cast<NamedDecl>(Decl->getCanonicalDecl());
+      if (Canonical != Decl) {
+        addUsage(Canonical, Decl->getLocation());
+        return;
+      }
+    }
+
     // Fix type aliases in value declarations.
     if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
       if (const Type *TypePtr = Value->getType().getTypePtrOrNull()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82059.271505.patch
Type: text/x-patch
Size: 3660 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200617/630ec3e0/attachment.bin>


More information about the cfe-commits mailing list