https://github.com/dkaszews updated https://github.com/llvm/llvm-project/pull/171686
>From 30a266b21a6e96441886778ab85ebff08a9f68e2 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Wed, 10 Dec 2025 20:28:57 +0100 Subject: [PATCH 01/10] Add default config --- .../readability/IdentifierNamingCheck.cpp | 29 ++++++++++++------- .../readability/IdentifierNamingCheck.h | 4 +++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp index 79f8437057b23..3efeade89a4c9 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -71,6 +71,7 @@ namespace readability { // clang-format off #define NAMING_KEYS(m) \ + m(Default) \ m(Namespace) \ m(InlineNamespace) \ m(EnumConstant) \ @@ -1162,7 +1163,7 @@ StyleKind IdentifierNamingCheck::findStyleKind( if (NamingStyles[SK_Constant]) return SK_Constant; - return SK_Invalid; + return undefinedStyle(NamingStyles); } if (const auto *Decl = dyn_cast<RecordDecl>(D)) { @@ -1194,7 +1195,7 @@ StyleKind IdentifierNamingCheck::findStyleKind( return SK_Enum; } - return SK_Invalid; + return undefinedStyle(NamingStyles); } if (const auto *Decl = dyn_cast<FieldDecl>(D)) { @@ -1238,7 +1239,7 @@ StyleKind IdentifierNamingCheck::findStyleKind( if (NamingStyles[SK_Parameter]) return SK_Parameter; - return SK_Invalid; + return undefinedStyle(NamingStyles); } if (const auto *Decl = dyn_cast<VarDecl>(D)) { @@ -1284,7 +1285,7 @@ StyleKind IdentifierNamingCheck::findStyleKind( if (NamingStyles[SK_Function]) return SK_Function; - return SK_Invalid; + return undefinedStyle(NamingStyles); } if (const auto *Decl = dyn_cast<FunctionDecl>(D)) { @@ -1308,7 +1309,7 @@ StyleKind IdentifierNamingCheck::findStyleKind( if (NamingStyles[SK_TemplateParameter]) return SK_TemplateParameter; - return SK_Invalid; + return undefinedStyle(NamingStyles); } if (isa<NonTypeTemplateParmDecl>(D)) { @@ -1318,7 +1319,7 @@ StyleKind IdentifierNamingCheck::findStyleKind( if (NamingStyles[SK_TemplateParameter]) return SK_TemplateParameter; - return SK_Invalid; + return undefinedStyle(NamingStyles); } if (isa<TemplateTemplateParmDecl>(D)) { @@ -1328,13 +1329,13 @@ StyleKind IdentifierNamingCheck::findStyleKind( if (NamingStyles[SK_TemplateParameter]) return SK_TemplateParameter; - return SK_Invalid; + return undefinedStyle(NamingStyles); } if (isa<ConceptDecl>(D) && NamingStyles[SK_Concept]) return SK_Concept; - return SK_Invalid; + return undefinedStyle(NamingStyles); } std::optional<RenamerClangTidyCheck::FailureInfo> @@ -1468,7 +1469,7 @@ StyleKind IdentifierNamingCheck::findStyleKindForAnonField( return findStyleKindForVar(V, Type, NamingStyles); } - return SK_Invalid; + return undefinedStyle(NamingStyles); } StyleKind IdentifierNamingCheck::findStyleKindForField( @@ -1494,7 +1495,7 @@ StyleKind IdentifierNamingCheck::findStyleKindForField( if (NamingStyles[SK_Member]) return SK_Member; - return SK_Invalid; + return undefinedStyle(NamingStyles); } StyleKind IdentifierNamingCheck::findStyleKindForVar( @@ -1571,8 +1572,14 @@ StyleKind IdentifierNamingCheck::findStyleKindForVar( if (NamingStyles[SK_Variable]) return SK_Variable; - return SK_Invalid; + return undefinedStyle(NamingStyles); +} + +StyleKind IdentifierNamingCheck::undefinedStyle( + ArrayRef<std::optional<NamingStyle>> NamingStyles) const { + return NamingStyles[SK_Default] ? SK_Default : SK_Invalid; } } // namespace readability } // namespace clang::tidy + diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h index 0b17af88810c2..cc05333269b80 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @@ -220,6 +220,9 @@ class IdentifierNamingCheck final : public RenamerClangTidyCheck { findStyleKindForVar(const VarDecl *Var, QualType Type, ArrayRef<std::optional<NamingStyle>> NamingStyles) const; + StyleKind + undefinedStyle(ArrayRef<std::optional<NamingStyle>> NamingStyles) const; + /// Stores the style options as a vector, indexed by the specified \ref /// StyleKind, for a given directory. mutable llvm::StringMap<FileStyle> NamingStylesCache; @@ -241,3 +244,4 @@ struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> { } // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H + >From 5dada5cefaa683092a9c1afd710e4a851dd94c38 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Wed, 10 Dec 2025 21:50:08 +0100 Subject: [PATCH 02/10] WIP tests --- .../identifier-naming-default-lower.cpp | 26 +++++++++++++++++++ .../identifier-naming-default-none.cpp | 25 ++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp new file mode 100644 index 0000000000000..276bc35a9be10 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp @@ -0,0 +1,26 @@ +// RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ +// RUN: -config='{CheckOptions: { \ +// RUN: readability-identifier-naming.DefaultCase: "lower_case" }}' + +int BadGlobal; + +int good_global; + +struct BadStruct { + int BadField; +}; + +struct good_struct { + int good_field; +}; + +int BadFunction(int BadParameter) { + int BadVariable = BadParameter; + return BadVariable; +} + +int good_function(int good_parameter) { + int good_variable = good_parameter; + return good_variable; +} + diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp new file mode 100644 index 0000000000000..8e82abfa61259 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp @@ -0,0 +1,25 @@ +// RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ +// RUN: -config='{CheckOptions: {}}' + +int BadGlobal; + +int good_global; + +struct BadStruct { + int BadField; +}; + +struct good_struct { + int good_field; +}; + +int BadFunction(int BadParameter) { + int BadVariable = BadParameter; + return BadVariable; +} + +int good_function(int good_parameter) { + int good_variable = good_parameter; + return good_variable; +} + >From 3fb5b0c3b7713b347c14732d3b4b1975223f41e8 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Thu, 11 Dec 2025 19:48:05 +0100 Subject: [PATCH 03/10] Pass lower test --- .../identifier-naming-default-lower.cpp | 66 +++++++++++++++---- .../identifier-naming-default-none.cpp | 44 +++++++++---- 2 files changed, 85 insertions(+), 25 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp index 276bc35a9be10..167f41c8cf469 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp @@ -1,26 +1,66 @@ // RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ // RUN: -config='{CheckOptions: { \ -// RUN: readability-identifier-naming.DefaultCase: "lower_case" }}' +// RUN: readability-identifier-naming.DefaultCase: "lower_case", \ +// RUN: }}' -int BadGlobal; +// DefaultCase enables every type of symbol to be checked with same case +// TODO: DefaultCase does not work for macros? +#define MyMacro -int good_global; +namespace MyNamespace { +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: invalid case style for default 'MyNamespace' [readability-identifier-naming] +// CHECK-FIXES: namespace my_namespace { -struct BadStruct { - int BadField; +using MyAlias = int; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for default 'MyAlias' [readability-identifier-naming] +// CHECK-FIXES: using my_alias = int; + +int MyGlobal; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for default 'MyGlobal' [readability-identifier-naming] +// CHECK-FIXES: int my_global; + +struct MyStruct { +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for default 'MyStruct' [readability-identifier-naming] +// CHECK-FIXES: struct my_struct { + int MyField; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for default 'MyField' [readability-identifier-naming] +// CHECK-FIXES: int my_field; }; -struct good_struct { - int good_field; +template <typename MyTypename> +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for default 'MyTypename' [readability-identifier-naming] +// CHECK-FIXES: template <typename my_typename> +int MyFunction(int MyArgument) { +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for default 'MyFunction' [readability-identifier-naming] +// CHECK-MESSAGES: :[[@LINE-2]]:20: warning: invalid case style for default 'MyArgument' [readability-identifier-naming] +// CHECK-FIXES: int my_function(int my_argument) { + int MyVariable = MyArgument; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for default 'MyVariable' [readability-identifier-naming] +// CHECK-FIXES: int my_variable = my_argument; + return MyVariable; +// CHECK-FIXES: return my_variable; +} + +} + +// These are all already formatted as desired +#define my_macro_2 + +namespace my_namespace_2 { + +using my_alias = int; + +my_alias my_global; + +struct my_struct { + int my_field; }; -int BadFunction(int BadParameter) { - int BadVariable = BadParameter; - return BadVariable; +template <typename my_typename> +int my_function(int my_argument) { + int my_variable = my_argument; + return my_variable; } -int good_function(int good_parameter) { - int good_variable = good_parameter; - return good_variable; } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp index 8e82abfa61259..b2370642cd37d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp @@ -1,25 +1,45 @@ // RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ // RUN: -config='{CheckOptions: {}}' -int BadGlobal; +// Empty options effectively disable the check, allowing PascalCase... +#define MyMacro -int good_global; +namespace MyNamespace { -struct BadStruct { - int BadField; +using MyAlias = int; + +int MyGlobal; + +struct MyStruct { + int MyField; }; -struct good_struct { - int good_field; +template <typename MyTypename> +int MyFunction(int MyArgument) { + int MyVariable = MyArgument; + return MyVariable; +} + +} + +// ...or lower_case for the same set of symbol types +#define my_macro_2 + +namespace my_namespace_2 { + +using my_alias = int; + +my_alias my_global; + +struct my_struct { + int my_field; }; -int BadFunction(int BadParameter) { - int BadVariable = BadParameter; - return BadVariable; +template <typename my_typename> +int my_function(int my_argument) { + int my_variable = my_argument; + return my_variable; } -int good_function(int good_parameter) { - int good_variable = good_parameter; - return good_variable; } >From 1006949a5f54f62dad37704ac92792353bedcaf6 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Thu, 11 Dec 2025 19:57:45 +0100 Subject: [PATCH 04/10] Add default override test --- .../identifier-naming-default-override.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp new file mode 100644 index 0000000000000..bf6221122763e --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp @@ -0,0 +1,72 @@ +// RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ +// RUN: -config='{CheckOptions: { \ +// RUN: readability-identifier-naming.DefaultCase: "lower_case", \ +// RUN: readability-identifier-naming.MacroDefinitionCase: "UPPER_CASE", \ +// RUN: readability-identifier-naming.TemplateParameterCase: "UPPER_CASE", \ +// RUN: }}' + +// DefaultCase enables every type of symbol to be checked with same case +#define MyMacro +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for macro definition 'MyMacro' [readability-identifier-naming] +// CHECK-FIXES: #define MY_MACRO + +namespace MyNamespace { +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: invalid case style for default 'MyNamespace' [readability-identifier-naming] +// CHECK-FIXES: namespace my_namespace { + +using MyAlias = int; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for default 'MyAlias' [readability-identifier-naming] +// CHECK-FIXES: using my_alias = int; + +int MyGlobal; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for default 'MyGlobal' [readability-identifier-naming] +// CHECK-FIXES: int my_global; + +struct MyStruct { +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for default 'MyStruct' [readability-identifier-naming] +// CHECK-FIXES: struct my_struct { + int MyField; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for default 'MyField' [readability-identifier-naming] +// CHECK-FIXES: int my_field; +}; + +template <typename MyTypename> +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for template parameter 'MyTypename' [readability-identifier-naming] +// CHECK-FIXES: template <typename MY_TYPENAME> +int MyFunction(int MyArgument) { +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for default 'MyFunction' [readability-identifier-naming] +// CHECK-MESSAGES: :[[@LINE-2]]:20: warning: invalid case style for default 'MyArgument' [readability-identifier-naming] +// CHECK-FIXES: int my_function(int my_argument) { + int MyVariable = MyArgument; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for default 'MyVariable' [readability-identifier-naming] +// CHECK-FIXES: int my_variable = my_argument; + return MyVariable; +// CHECK-FIXES: return my_variable; +} + +} + +#define my_macro_2 +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for macro definition 'my_macro_2' [readability-identifier-naming] +// CHECK-FIXES: #define MY_MACRO_2 + +namespace my_namespace_2 { + +using my_alias = int; + +my_alias my_global; + +struct my_struct { + int my_field; +}; + +template <typename my_typename> +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for template parameter 'my_typename' [readability-identifier-naming] +// CHECK-FIXES: template <typename MY_TYPENAME> +int my_function(int my_argument) { + int my_variable = my_argument; + return my_variable; +} + +} + >From 85f628a714251aa524be97d45458b2a0374b2006 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Thu, 11 Dec 2025 20:13:12 +0100 Subject: [PATCH 05/10] Add docs, enable failing macro case --- .../checks/readability/identifier-naming.rst | 57 +++++++++++++++++++ .../identifier-naming-default-lower.cpp | 5 +- .../identifier-naming-default-override.cpp | 1 - 3 files changed, 60 insertions(+), 3 deletions(-) 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 2c0d69f0264e7..f632f89071250 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 @@ -58,6 +58,9 @@ The available options are summarized below: **Specific options** + - :option:`DefaultCase`, :option:`DefaultPrefix`, + :option:`DefaultSuffix`, :option:`DefaultIgnoredRegexp`, + :option:`DefaultHungarianPrefix` - :option:`AbstractClassCase`, :option:`AbstractClassPrefix`, :option:`AbstractClassSuffix`, :option:`AbstractClassIgnoredRegexp`, :option:`AbstractClassHungarianPrefix` @@ -223,6 +226,60 @@ Options description A detailed description of each option is presented below: +.. option:: DefaultCase + + When defined, the check will ensure all name by default conform to the + selected casing. + +.. option:: DefaultPrefix + + When defined, the check will ensure all name by default will add the + prefixed with the given value (regardless of casing). + +.. option:: DefaultIgnoredRegexp + + Identifier naming checks won't be enforced for all name by default + matching this regular expression. + +.. option:: DefaultSuffix + + When defined, the check will ensure all name by default will add the + suffix with the given value (regardless of casing). + +.. option:: DefaultHungarianPrefix + + When enabled, the check ensures that the declared identifier will + have a Hungarian notation prefix based on the declared type. + +The check only works on kinds of identifiers which have been configured, +so an empty config effectively disables it. +The "default" option can be used to enable all kinds of identifiers, +then optionally override specific kinds which are desired with a different case. + +For example using values of: + + - DefaultCase of ``lower_case`` + - MacroDefinitionCase of ``UPPER_CASE`` + - TemplateParameterCase of ``CamelCase`` + +Identifies and/or transforms names as follows: + +Before: + +.. code-block:: c++ + + #define macroDefinition + template <typename typenameParameter> + int functionDeclaration(typenameParameter paramVal, int paramCount); + +After: + +.. code-block:: c++ + + #define MACRO_DEFINITION + template <typename TypenameParameter> + int function_declarations(TypenameParameter param_val, int param_count); + .. option:: AbstractClassCase When defined, the check will ensure abstract class names conform to the diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp index 167f41c8cf469..20ea67dd136a5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp @@ -3,9 +3,10 @@ // RUN: readability-identifier-naming.DefaultCase: "lower_case", \ // RUN: }}' -// DefaultCase enables every type of symbol to be checked with same case -// TODO: DefaultCase does not work for macros? +// DefaultCase enables every type of identifier to be checked with same case #define MyMacro +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for default 'MyMacro' [readability-identifier-naming] +// CHECK-FIXES: #define my_macro namespace MyNamespace { // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: invalid case style for default 'MyNamespace' [readability-identifier-naming] diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp index bf6221122763e..71f65c0ce0e14 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp @@ -5,7 +5,6 @@ // RUN: readability-identifier-naming.TemplateParameterCase: "UPPER_CASE", \ // RUN: }}' -// DefaultCase enables every type of symbol to be checked with same case #define MyMacro // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for macro definition 'MyMacro' [readability-identifier-naming] // CHECK-FIXES: #define MY_MACRO >From b1c6789bdee772abcf497406c96a4242ae07ebb5 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Thu, 11 Dec 2025 20:29:29 +0100 Subject: [PATCH 06/10] Handle macros --- .../clang-tidy/readability/IdentifierNamingCheck.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp index 3efeade89a4c9..3f65fb362af68 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -1403,9 +1403,14 @@ IdentifierNamingCheck::getMacroFailureInfo(const Token &MacroNameTok, if (!Style.isActive()) return std::nullopt; + const auto &Styles = Style.getStyles(); + const StyleKind UsedKind = !Styles[SK_MacroDefinition] && Styles[SK_Default] + ? SK_Default + : SK_MacroDefinition; + return getFailureInfo("", MacroNameTok.getIdentifierInfo()->getName(), nullptr, Loc, Style.getStyles(), Style.getHNOption(), - SK_MacroDefinition, SM, IgnoreFailedSplit); + UsedKind, SM, IgnoreFailedSplit); } RenamerClangTidyCheck::DiagInfo >From 486325b2081dd4405a0b025777307e93a65f8320 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Thu, 11 Dec 2025 21:11:18 +0100 Subject: [PATCH 07/10] Review fixes --- .../readability/IdentifierNamingCheck.cpp | 1 - .../readability/IdentifierNamingCheck.h | 1 - .../checks/readability/identifier-naming.rst | 66 +++++++++---------- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp index 3f65fb362af68..189bfe49d3984 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -1587,4 +1587,3 @@ StyleKind IdentifierNamingCheck::undefinedStyle( } // namespace readability } // namespace clang::tidy - diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h index cc05333269b80..87735808dff39 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @@ -244,4 +244,3 @@ struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> { } // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H - 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 f632f89071250..c6517356cbd4b 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 @@ -44,6 +44,35 @@ settings. The options and their corresponding values are: - ``LowerCase`` - example: ``int i_Variable`` - ``CamelCase`` - example: ``int IVariable`` +The check only works on kinds of identifiers which have been configured, +so an empty config effectively disables it. +The "default" option can be used to enable all kinds of identifiers, +then optionally override specific kinds which are desired with a different case. + +For example using values of: + + - DefaultCase of ``lower_case`` + - MacroDefinitionCase of ``UPPER_CASE`` + - TemplateParameterCase of ``CamelCase`` + +Identifies and/or transforms names as follows: + +Before: + +.. code-block:: c++ + + #define macroDefinition + template <typename typenameParameter> + int functionDeclaration(typenameParameter paramVal, int paramCount); + +After: + +.. code-block:: c++ + + #define MACRO_DEFINITION + template <typename TypenameParameter> + int function_declarations(TypenameParameter param_val, int param_count); + Options summary --------------- @@ -228,22 +257,22 @@ A detailed description of each option is presented below: .. option:: DefaultCase - When defined, the check will ensure all name by default conform to the + When defined, the check will ensure all names by default conform to the selected casing. .. option:: DefaultPrefix - When defined, the check will ensure all name by default will add the + When defined, the check will ensure all names by default will add the prefixed with the given value (regardless of casing). .. option:: DefaultIgnoredRegexp - Identifier naming checks won't be enforced for all name by default + Identifier naming checks won't be enforced for all names by default matching this regular expression. .. option:: DefaultSuffix - When defined, the check will ensure all name by default will add the + When defined, the check will ensure all names by default will add the suffix with the given value (regardless of casing). .. option:: DefaultHungarianPrefix @@ -251,35 +280,6 @@ A detailed description of each option is presented below: When enabled, the check ensures that the declared identifier will have a Hungarian notation prefix based on the declared type. -The check only works on kinds of identifiers which have been configured, -so an empty config effectively disables it. -The "default" option can be used to enable all kinds of identifiers, -then optionally override specific kinds which are desired with a different case. - -For example using values of: - - - DefaultCase of ``lower_case`` - - MacroDefinitionCase of ``UPPER_CASE`` - - TemplateParameterCase of ``CamelCase`` - -Identifies and/or transforms names as follows: - -Before: - -.. code-block:: c++ - - #define macroDefinition - template <typename typenameParameter> - int functionDeclaration(typenameParameter paramVal, int paramCount); - -After: - -.. code-block:: c++ - - #define MACRO_DEFINITION - template <typename TypenameParameter> - int function_declarations(TypenameParameter param_val, int param_count); - .. option:: AbstractClassCase When defined, the check will ensure abstract class names conform to the >From b2ebbdcf6b044b01cbc5bb603258212862088a06 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Thu, 11 Dec 2025 21:39:02 +0100 Subject: [PATCH 08/10] Add release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8efde0ab121e6..b47de5be8416c 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -604,6 +604,11 @@ Changes in existing checks comments between the ``if`` condition and the ``then`` block are preserved when applying the fix. +- Improved :doc:`readability-identifier-naming + <clang-tidy/checks/readability/identifier-naming>` by adding option + `DefaultCase` which simplifies configs by removing the need to specify each + identifier kind separately. + Removed checks ^^^^^^^^^^^^^^ >From 1351f65008829cda846ab0f7789ef8e11bb9ac7a Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Fri, 12 Dec 2025 17:23:14 +0100 Subject: [PATCH 09/10] Instantiate templates in tests --- .../readability/identifier-naming-default-lower.cpp | 7 ++++++- .../readability/identifier-naming-default-none.cpp | 7 ++++++- .../readability/identifier-naming-default-override.cpp | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp index 20ea67dd136a5..2c7b89ec2b52e 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-lower.cpp @@ -1,7 +1,8 @@ // RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ // RUN: -config='{CheckOptions: { \ // RUN: readability-identifier-naming.DefaultCase: "lower_case", \ -// RUN: }}' +// RUN: }}' \ +// RUN: -- -fno-delayed-template-parsing // DefaultCase enables every type of identifier to be checked with same case #define MyMacro @@ -42,6 +43,8 @@ int MyFunction(int MyArgument) { // CHECK-FIXES: return my_variable; } +template int MyFunction<int>(int); + } // These are all already formatted as desired @@ -63,5 +66,7 @@ int my_function(int my_argument) { return my_variable; } +template int my_function<int>(int); + } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp index b2370642cd37d..041bfa2ec7702 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-none.cpp @@ -1,5 +1,6 @@ // RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ -// RUN: -config='{CheckOptions: {}}' +// RUN: -config='{CheckOptions: {}}' \ +// RUN: -- -fno-delayed-template-parsing // Empty options effectively disable the check, allowing PascalCase... #define MyMacro @@ -20,6 +21,8 @@ int MyFunction(int MyArgument) { return MyVariable; } +template int MyFunction<int>(int); + } // ...or lower_case for the same set of symbol types @@ -41,5 +44,7 @@ int my_function(int my_argument) { return my_variable; } +template int my_function<int>(int); + } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp index 71f65c0ce0e14..05e42b0c6accc 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-default-override.cpp @@ -3,7 +3,8 @@ // RUN: readability-identifier-naming.DefaultCase: "lower_case", \ // RUN: readability-identifier-naming.MacroDefinitionCase: "UPPER_CASE", \ // RUN: readability-identifier-naming.TemplateParameterCase: "UPPER_CASE", \ -// RUN: }}' +// RUN: }}' \ +// RUN: -- -fno-delayed-template-parsing #define MyMacro // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for macro definition 'MyMacro' [readability-identifier-naming] @@ -43,6 +44,8 @@ int MyFunction(int MyArgument) { // CHECK-FIXES: return my_variable; } +template int MyFunction<int>(int); + } #define my_macro_2 @@ -67,5 +70,7 @@ int my_function(int my_argument) { return my_variable; } +template int my_function<int>(int); + } >From 567dd98179418bf6ecbe18650f321d18b86e80e6 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski <[email protected]> Date: Fri, 12 Dec 2025 18:16:58 +0100 Subject: [PATCH 10/10] Fix release notes order --- clang-tools-extra/docs/ReleaseNotes.rst | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index b47de5be8416c..9d14c88606ca7 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -566,7 +566,9 @@ Changes in existing checks <clang-tidy/checks/readability/identifier-naming>` check by ignoring declarations and macros in system headers. The documentation is also improved to differentiate the general options from the specific ones. Options for - fine-grained control over ``constexpr`` variables were added. + fine-grained control over ``constexpr`` variables were added. Added default + options which simplify configs by removing the need to specify each + identifier kind separately. - Improved :doc:`readability-implicit-bool-conversion <clang-tidy/checks/readability/implicit-bool-conversion>` check by correctly @@ -604,11 +606,6 @@ Changes in existing checks comments between the ``if`` condition and the ``then`` block are preserved when applying the fix. -- Improved :doc:`readability-identifier-naming - <clang-tidy/checks/readability/identifier-naming>` by adding option - `DefaultCase` which simplifies configs by removing the need to specify each - identifier kind separately. - Removed checks ^^^^^^^^^^^^^^ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
