[PATCH] D157190: [clang-tidy] Fixed false-negative in readability-identifier-naming

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 5 11:08:41 PDT 2023


PiotrZSL updated this revision to Diff 547503.
PiotrZSL added a comment.

Rebase + make tests more simple


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D157190

Files:
  clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  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
@@ -707,3 +707,19 @@
 task ImplicitDeclTest(async_obj &a_object) {
   co_await a_object;  // CHECK-MESSAGES-NOT: warning: invalid case style for local variable
 }
+
+// Test scenario when canonical declaration will be a forward declaration
+struct ForwardDeclStruct;
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'ForwardDeclStruct' [readability-identifier-naming]
+// CHECK-FIXES: {{^}}struct forward_decl_struct;
+// CHECK-FIXES: {{^}}struct forward_decl_struct {
+struct ForwardDeclStruct {
+};
+
+struct forward_declared_as_struct;
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for class 'forward_declared_as_struct' [readability-identifier-naming]
+// CHECK-FIXES: {{^}}struct CForwardDeclaredAsStruct;
+// CHECK-FIXES: {{^}}class CForwardDeclaredAsStruct {
+class forward_declared_as_struct {
+};
+
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -172,6 +172,10 @@
   <clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
   ``inline`` namespaces in the same format as :program:`clang-format`.
 
+- Improved the :doc:`readability-identifier-naming
+  <clang-tidy/checks/readability/identifier-naming>` check to emit proper
+  warnings when a type forward declaration precedes its definition.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
Index: clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -1140,30 +1140,28 @@
     if (Decl->isAnonymousStructOrUnion())
       return SK_Invalid;
 
-    if (!Decl->getCanonicalDecl()->isThisDeclarationADefinition())
-      return SK_Invalid;
-
-    if (Decl->hasDefinition() && Decl->isAbstract() &&
-        NamingStyles[SK_AbstractClass])
-      return SK_AbstractClass;
+    if (const auto *Definition = Decl->getDefinition()) {
+      if (Definition->isAbstract() && NamingStyles[SK_AbstractClass])
+        return SK_AbstractClass;
 
-    if (Decl->isStruct() && NamingStyles[SK_Struct])
-      return SK_Struct;
+      if (Definition->isStruct() && NamingStyles[SK_Struct])
+        return SK_Struct;
 
-    if (Decl->isStruct() && NamingStyles[SK_Class])
-      return SK_Class;
+      if (Definition->isStruct() && NamingStyles[SK_Class])
+        return SK_Class;
 
-    if (Decl->isClass() && NamingStyles[SK_Class])
-      return SK_Class;
+      if (Definition->isClass() && NamingStyles[SK_Class])
+        return SK_Class;
 
-    if (Decl->isClass() && NamingStyles[SK_Struct])
-      return SK_Struct;
+      if (Definition->isClass() && NamingStyles[SK_Struct])
+        return SK_Struct;
 
-    if (Decl->isUnion() && NamingStyles[SK_Union])
-      return SK_Union;
+      if (Definition->isUnion() && NamingStyles[SK_Union])
+        return SK_Union;
 
-    if (Decl->isEnum() && NamingStyles[SK_Enum])
-      return SK_Enum;
+      if (Definition->isEnum() && NamingStyles[SK_Enum])
+        return SK_Enum;
+    }
 
     return SK_Invalid;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157190.547503.patch
Type: text/x-patch
Size: 3566 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230805/63a70ab4/attachment-0001.bin>


More information about the cfe-commits mailing list