[clang-tools-extra] f0e2a5e - [clang-tidy][readability] add Leading_upper_snake_case

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 28 12:56:03 PDT 2023


Author: MasterCopy8GB
Date: 2023-08-28T19:53:09Z
New Revision: f0e2a5e260c24597f11b122d20051f057d96b544

URL: https://github.com/llvm/llvm-project/commit/f0e2a5e260c24597f11b122d20051f057d96b544
DIFF: https://github.com/llvm/llvm-project/commit/f0e2a5e260c24597f11b122d20051f057d96b544.diff

LOG: [clang-tidy][readability] add Leading_upper_snake_case

Add Leading_upper_snake_case to IdentifierNamingCheck naming convention.

Fixes: #42451

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D158787

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
    clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/readability/identifier-naming.rst
    clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-case-violation.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index e875717da247e3..ad20b777202a85 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -46,7 +46,9 @@ OptionEnumMapping<
           {readability::IdentifierNamingCheck::CT_CamelSnakeCase,
            "Camel_Snake_Case"},
           {readability::IdentifierNamingCheck::CT_CamelSnakeBack,
-           "camel_Snake_Back"}};
+           "camel_Snake_Back"},
+          {readability::IdentifierNamingCheck::CT_LeadingUpperSnakeCase,
+           "Leading_upper_snake_case"}};
   return {Mapping};
 }
 
@@ -871,6 +873,7 @@ bool IdentifierNamingCheck::matchesStyle(
       llvm::Regex("^[A-Z][a-zA-Z0-9]*$"),
       llvm::Regex("^[A-Z]([a-z0-9]*(_[A-Z])?)*"),
       llvm::Regex("^[a-z]([a-z0-9]*(_[A-Z])?)*"),
+      llvm::Regex("^[A-Z]([a-z0-9_]*[a-z])*$"),
   };
 
   if (!Name.consume_front(Style.Prefix))
@@ -993,6 +996,18 @@ std::string IdentifierNamingCheck::fixupWithCase(
       Fixup += Word.substr(1).lower();
     }
     break;
+
+  case IdentifierNamingCheck::CT_LeadingUpperSnakeCase:
+    for (auto const &Word : Words) {
+      if (&Word != &Words.front()) {
+        Fixup += "_";
+        Fixup += Word.lower();
+      } else {
+        Fixup += toupper(Word.front());
+        Fixup += Word.substr(1).lower();
+      }
+    }
+    break;
   }
 
   return Fixup.str().str();

diff  --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
index 2e3d4ea8c39c81..14626981cc42d8 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
@@ -44,7 +44,8 @@ class IdentifierNamingCheck final : public RenamerClangTidyCheck {
     CT_UpperCase,
     CT_CamelCase,
     CT_CamelSnakeCase,
-    CT_CamelSnakeBack
+    CT_CamelSnakeBack,
+    CT_LeadingUpperSnakeCase
   };
 
   enum HungarianPrefixType {

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e17cf863d9a72d..95ee6daf7209e5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -252,7 +252,8 @@ Changes in existing checks
 
 - Improved :doc:`readability-identifier-naming
   <clang-tidy/checks/readability/identifier-naming>` check to emit proper
-  warnings when a type forward declaration precedes its definition.
+  warnings when a type forward declaration precedes its definition and
+  added support for ``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/docs/clang-tidy/checks/readability/identifier-naming.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/identifier-naming.rst
index 621fe0258e2770..467968ce43faea 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/identifier-naming.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/identifier-naming.rst
@@ -17,7 +17,8 @@ Casing types include:
  - ``CamelCase``,
  - ``camel_Snake_Back``,
  - ``Camel_Snake_Case``,
- - ``aNy_CasE``.
+ - ``aNy_CasE``,
+ - ``Leading_upper_snake_case``.
 
 It also supports a fixed prefix and suffix that will be prepended or appended
 to the identifiers, regardless of the casing.
@@ -30,6 +31,10 @@ The naming of virtual methods is reported where they occur in the base class,
 but not where they are overridden, as it can't be fixed locally there.
 This also applies for pseudo-override patterns like CRTP.
 
+``Leading_upper_snake_case`` is a naming convention where the first word is capitalized
+followed by lower case word(s) seperated by underscore(s) '_'. Examples include:
+Cap_snake_case, Cobra_case, Foo_bar_baz, and Master_copy_8gb.
+
 Options
 -------
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-case-violation.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-case-violation.cpp
index 6102a2e7031afb..794f6d313695cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-case-violation.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-case-violation.cpp
@@ -5,6 +5,7 @@
 // RUN:   readability-identifier-naming.ClassCase: UUPER_CASE, \
 // RUN:   readability-identifier-naming.StructCase: CAMEL, \
 // RUN:   readability-identifier-naming.EnumCase: AnY_cASe, \
+// RUN:   readability-identifier-naming.VirtualMethodCase: lEaDiNg_upper_SNAKE_CaSe, \
 // RUN:   }}" -- 2>&1 | FileCheck %s --implicit-check-not="{{warning|error}}:"
 
 // CHECK-DAG: warning: invalid configuration value 'camelback' for option 'readability-identifier-naming.FunctionCase'; did you mean 'camelBack'? [clang-tidy-config]
@@ -13,3 +14,4 @@
 // CHECK-DAG: warning: invalid configuration value 'CAMEL' for option 'readability-identifier-naming.StructCase' [clang-tidy-config]
 // This fails on the EditDistance, but as it matches ignoring case suggest the correct value
 // CHECK-DAG: warning: invalid configuration value 'AnY_cASe' for option 'readability-identifier-naming.EnumCase'; did you mean 'aNy_CasE'? [clang-tidy-config]
+// CHECK-DAG: warning: invalid configuration value 'lEaDiNg_upper_SNAKE_CaSe' for option 'readability-identifier-naming.VirtualMethodCase'; did you mean 'Leading_upper_snake_case'? [clang-tidy-config]

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
index 485f0ba1583e8e..3445a21bfdbc4a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
@@ -49,7 +49,7 @@
 // RUN:     readability-identifier-naming.StaticConstantCase: UPPER_CASE, \
 // RUN:     readability-identifier-naming.StaticVariableCase: camelBack, \
 // RUN:     readability-identifier-naming.StaticVariablePrefix: 's_', \
-// RUN:     readability-identifier-naming.StructCase: lower_case, \
+// RUN:     readability-identifier-naming.StructCase: Leading_upper_snake_case, \
 // RUN:     readability-identifier-naming.TemplateParameterCase: UPPER_CASE, \
 // RUN:     readability-identifier-naming.TemplateTemplateParameterCase: CamelCase, \
 // RUN:     readability-identifier-naming.TemplateUsingCase: lower_case, \
@@ -513,9 +513,9 @@ constexpr int CE_function() { return 3; }
 
 struct THIS___Structure {
 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'THIS___Structure'
-// CHECK-FIXES: {{^}}struct this_structure {{{$}}
+// CHECK-FIXES: {{^}}struct This_structure {{{$}}
     THIS___Structure();
-// CHECK-FIXES: {{^}}    this_structure();{{$}}
+// CHECK-FIXES: {{^}}    This_structure();{{$}}
 
   union __MyUnion_is_wonderful__ {};
 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for union '__MyUnion_is_wonderful__'
@@ -524,7 +524,7 @@ struct THIS___Structure {
 
 typedef THIS___Structure struct_type;
 // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: invalid case style for typedef 'struct_type'
-// CHECK-FIXES: {{^}}typedef this_structure struct_type_t;{{$}}
+// CHECK-FIXES: {{^}}typedef This_structure struct_type_t;{{$}}
 
 struct_type GlobalTypedefTestFunction(struct_type a_argument1) {
 // CHECK-FIXES: {{^}}struct_type_t GlobalTypedefTestFunction(struct_type_t a_argument1) {
@@ -534,7 +534,7 @@ struct_type GlobalTypedefTestFunction(struct_type a_argument1) {
 
 using my_struct_type = THIS___Structure;
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for type alias 'my_struct_type'
-// CHECK-FIXES: {{^}}using my_Struct_Type_t = this_structure;{{$}}
+// CHECK-FIXES: {{^}}using my_Struct_Type_t = This_structure;{{$}}
 
 template<typename t_t>
 using SomeOtherTemplate = my_other_templated_class  <:: FOO_NS  ::my_class>;
@@ -596,6 +596,8 @@ void MY_TEST_Macro(function) {}
 }
 
 template <typename t_t> struct a {
+// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: invalid case style for struct 'a'
+// CHECK-FIXES: {{^}}template <typename t_t> struct A {{{$}}
   typename t_t::template b<> c;
 
   char const MY_ConstMember_string[4] = "123";
@@ -609,9 +611,11 @@ template <typename t_t> struct a {
 
 template<typename t_t>
 char const a<t_t>::MyConstClass_string[] = "123";
-// CHECK-FIXES: {{^}}char const a<t_t>::kMyConstClassString[] = "123";{{$}}
+// CHECK-FIXES: {{^}}char const A<t_t>::kMyConstClassString[] = "123";{{$}}
 
 template <template <typename> class A> struct b { A<int> c; };
+// CHECK-MESSAGES: :[[@LINE-1]]:47: warning: invalid case style for struct 'b'
+// CHECK-FIXES:template <template <typename> class A> struct B { A<int> c; };{{$}}
 
 unsigned MY_GLOBAL_array[] = {1,2,3};
 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for global variable 'MY_GLOBAL_array'
@@ -645,17 +649,17 @@ using namespace FOO_NS::InlineNamespace;
 // CHECK-FIXES: {{^}}using namespace foo_ns::inline_namespace;
 
 void QualifiedTypeLocTest(THIS___Structure);
-// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(this_structure);{{$}}
+// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(This_structure);{{$}}
 void QualifiedTypeLocTest(THIS___Structure &);
-// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(this_structure &);{{$}}
+// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(This_structure &);{{$}}
 void QualifiedTypeLocTest(THIS___Structure &&);
-// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(this_structure &&);{{$}}
+// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(This_structure &&);{{$}}
 void QualifiedTypeLocTest(const THIS___Structure);
-// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(const this_structure);{{$}}
+// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(const This_structure);{{$}}
 void QualifiedTypeLocTest(const THIS___Structure &);
-// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(const this_structure &);{{$}}
+// CHECK-FIXES: {{^}}void QualifiedTypeLocTest(const This_structure &);{{$}}
 void QualifiedTypeLocTest(volatile THIS___Structure &);
-// CHECK-FIXES: {{^}}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.
@@ -700,6 +704,8 @@ auto GetRes(type_t& Param) -> decltype(Param.res());
 // Check implicit declarations in coroutines
 
 struct async_obj {
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'async_obj'
+// CHECK-FIXES: {{^}}struct Async_obj {{{$}}
 public:
   never_suspend operator co_await() const noexcept;
 };
@@ -711,8 +717,8 @@ task ImplicitDeclTest(async_obj &a_object) {
 // 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 {
+// CHECK-FIXES: {{^}}struct Forward_decl_struct;
+// CHECK-FIXES: {{^}}struct Forward_decl_struct {
 struct ForwardDeclStruct {
 };
 


        


More information about the cfe-commits mailing list