[clang-tools-extra] fa8e740 - [clang-tidy] readability-identifier-naming - fix StructCase and UnionCase in C (#65202)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 3 09:40:23 PDT 2023
Author: Piotr Zegar
Date: 2023-09-03T18:40:18+02:00
New Revision: fa8e74073762300d07b02adec42c629daf82c44b
URL: https://github.com/llvm/llvm-project/commit/fa8e74073762300d07b02adec42c629daf82c44b
DIFF: https://github.com/llvm/llvm-project/commit/fa8e74073762300d07b02adec42c629daf82c44b.diff
LOG: [clang-tidy] readability-identifier-naming - fix StructCase and UnionCase in C (#65202)
In C struct are visible as RecordDecl, not as CXXRecordDecl, this type
of declaration were not supported in this check before. Changing check
to support it. Added tests.
Fixes: #55422
Added:
Modified:
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/hungarian-notation1/.clang-tidy
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index ad20b777202a854..7539b3899682e13 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -1151,13 +1151,15 @@ StyleKind IdentifierNamingCheck::findStyleKind(
return SK_Invalid;
}
- if (const auto *Decl = dyn_cast<CXXRecordDecl>(D)) {
+ if (const auto *Decl = dyn_cast<RecordDecl>(D)) {
if (Decl->isAnonymousStructOrUnion())
return SK_Invalid;
if (const auto *Definition = Decl->getDefinition()) {
- if (Definition->isAbstract() && NamingStyles[SK_AbstractClass])
- return SK_AbstractClass;
+ if (const auto *CxxRecordDecl = dyn_cast<CXXRecordDecl>(Definition)) {
+ if (CxxRecordDecl->isAbstract() && NamingStyles[SK_AbstractClass])
+ return SK_AbstractClass;
+ }
if (Definition->isStruct() && NamingStyles[SK_Struct])
return SK_Struct;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 69f7268bc3779dc..740c1c87e054ecb 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -256,9 +256,11 @@ Changes in existing checks
``length()`` method as an alternative to ``size()``.
- Improved :doc:`readability-identifier-naming
- <clang-tidy/checks/readability/identifier-naming>` check to emit proper
- warnings when a type forward declaration precedes its definition and
- added support for ``Leading_upper_snake_case`` naming convention.
+ <clang-tidy/checks/readability/identifier-naming>` check to issue accurate
+ warnings when a type's forward declaration precedes its definition.
+ Additionally, it now provides appropriate warnings for ``struct`` and
+ ``union`` in C, while also incorporating support for the
+ ``Leading_upper_snake_case`` naming convention.
- Improved :doc:`readability-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/hungarian-notation1/.clang-tidy b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/hungarian-notation1/.clang-tidy
index fb68fa542e16033..ff41479e2e96dbd 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/hungarian-notation1/.clang-tidy
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/hungarian-notation1/.clang-tidy
@@ -1,6 +1,8 @@
Checks: readability-identifier-naming
CheckOptions:
readability-identifier-naming.AbstractClassCase: CamelCase
+ readability-identifier-naming.StructCase: CamelCase
+ readability-identifier-naming.UnionCase: camelBack
readability-identifier-naming.ClassCase: CamelCase
readability-identifier-naming.ClassConstantCase: CamelCase
readability-identifier-naming.ClassMemberCase: CamelCase
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c
index abd7e1b8f275d89..dd82c11a3077638 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c
@@ -63,9 +63,14 @@ struct MyStruct { int StructCase; };
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: invalid case style for public member 'StructCase' [readability-identifier-naming]
// CHECK-FIXES: {{^}}struct MyStruct { int iStructCase; };
+struct shouldBeCamelCaseStruct { int iField; };
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'shouldBeCamelCaseStruct' [readability-identifier-naming]
+// CHECK-FIXES: {{^}}struct ShouldBeCamelCaseStruct { int iField; };
+
union MyUnion { int UnionCase; long lUnionCase; };
-// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for public member 'UnionCase' [readability-identifier-naming]
-// CHECK-FIXES: {{^}}union MyUnion { int iUnionCase; long lUnionCase; };
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for union 'MyUnion' [readability-identifier-naming]
+// CHECK-MESSAGES: :[[@LINE-2]]:21: warning: invalid case style for public member 'UnionCase' [readability-identifier-naming]
+// CHECK-FIXES: {{^}}union myUnion { int iUnionCase; long lUnionCase; };
//===----------------------------------------------------------------------===//
// C string
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp
index 10feff37f0899d1..b974faecc5879b2 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp
@@ -115,9 +115,14 @@ struct MyStruct { int StructCase; };
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: invalid case style for public member 'StructCase' [readability-identifier-naming]
// CHECK-FIXES: {{^}}struct MyStruct { int iStructCase; };
+struct shouldBeCamelCaseStruct { int iField; };
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'shouldBeCamelCaseStruct' [readability-identifier-naming]
+// CHECK-FIXES: {{^}}struct ShouldBeCamelCaseStruct { int iField; };
+
union MyUnion { int UnionCase; long lUnionCase; };
-// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for public member 'UnionCase' [readability-identifier-naming]
-// CHECK-FIXES: {{^}}union MyUnion { int iUnionCase; long lUnionCase; };
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for union 'MyUnion' [readability-identifier-naming]
+// CHECK-MESSAGES: :[[@LINE-2]]:21: warning: invalid case style for public member 'UnionCase' [readability-identifier-naming]
+// CHECK-FIXES: {{^}}union myUnion { int iUnionCase; long lUnionCase; };
//===----------------------------------------------------------------------===//
// C string
More information about the cfe-commits
mailing list