https://github.com/NyuB updated https://github.com/llvm/llvm-project/pull/181502
>From 2307afe8c79b6b5b57ab293aff48d2adb48f2d68 Mon Sep 17 00:00:00 2001 From: Brice Decaestecker <[email protected]> Date: Sat, 14 Feb 2026 12:17:30 +0100 Subject: [PATCH] [clang-tidy] Avoid identifier-naming concatenating multiple (pre/su)ffixes, introducing an option to strip out any other known (pre/su)ffix before renaming --- .../readability/IdentifierNamingCheck.cpp | 43 ++- .../readability/IdentifierNamingCheck.h | 5 +- .../checks/readability/identifier-naming.rst | 328 ++++++++++++++---- .../readability/identifier-naming-trim.cpp | 28 ++ 4 files changed, 338 insertions(+), 66 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-trim.cpp diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp index 83fc3727cb5c8..01350fd396b17 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -238,9 +238,11 @@ static StringRef const HungarianNotationUserDefinedTypes[] = { IdentifierNamingCheck::NamingStyle::NamingStyle( std::optional<IdentifierNamingCheck::CaseType> Case, StringRef Prefix, - StringRef Suffix, StringRef IgnoredRegexpStr, HungarianPrefixType HPType) + StringRef Suffix, StringRef IgnoredRegexpStr, HungarianPrefixType HPType, + bool TrimPrefixSuffix) : Case(Case), Prefix(Prefix), Suffix(Suffix), - IgnoredRegexpStr(IgnoredRegexpStr), HPType(HPType) { + IgnoredRegexpStr(IgnoredRegexpStr), HPType(HPType), + TrimPrefixSuffix(TrimPrefixSuffix) { if (!IgnoredRegexpStr.empty()) { IgnoredRegexp = llvm::Regex(llvm::SmallString<128>({"^", IgnoredRegexpStr, "$"})); @@ -259,7 +261,7 @@ IdentifierNamingCheck::FileStyle IdentifierNamingCheck::getFileStyleFromOptions( SmallVector<std::optional<IdentifierNamingCheck::NamingStyle>, 0> Styles; Styles.resize(SK_Count); - SmallString<64> StyleString; + SmallString<128> StyleString; for (unsigned I = 0; I < SK_Count; ++I) { const size_t StyleSize = StyleNames[I].size(); StyleString.assign({StyleNames[I], "HungarianPrefix"}); @@ -269,9 +271,13 @@ IdentifierNamingCheck::FileStyle IdentifierNamingCheck::getFileStyleFromOptions( if (HPTOpt && !HungarianNotation.checkOptionValid(I)) configurationDiag("invalid identifier naming option '%0'") << StyleString; + memcpy(&StyleString[StyleSize], "TrimPrefixSuffix", 16); + StyleString.truncate(StyleSize + 16); + const std::optional<bool> TrimPrefixSuffix = + Options.get<bool>(StyleString); memcpy(&StyleString[StyleSize], "IgnoredRegexp", 13); StyleString.truncate(StyleSize + 13); - const std::optional<StringRef> IgnoredRegexpStr = Options.get(StyleString); + std::optional<StringRef> IgnoredRegexpStr = Options.get(StyleString); memcpy(&StyleString[StyleSize], "Prefix", 6); StyleString.truncate(StyleSize + 6); const std::optional<StringRef> Prefix(Options.get(StyleString)); @@ -286,7 +292,8 @@ IdentifierNamingCheck::FileStyle IdentifierNamingCheck::getFileStyleFromOptions( if (CaseOptional || Prefix || Postfix || IgnoredRegexpStr || HPTOpt) Styles[I].emplace(std::move(CaseOptional), Prefix.value_or(""), Postfix.value_or(""), IgnoredRegexpStr.value_or(""), - HPTOpt.value_or(IdentifierNamingCheck::HPT_Off)); + HPTOpt.value_or(IdentifierNamingCheck::HPT_Off), + TrimPrefixSuffix.value_or(false)); } const bool IgnoreMainLike = Options.get("IgnoreMainLikeFunctions", false); const bool CheckAnonFieldInParent = @@ -1084,9 +1091,30 @@ bool IdentifierNamingCheck::isParamInMainLikeFunction( return Matcher.match(FDecl->getName()); } +static void trimPrefixesAndSuffixes( + StringRef &Mid, + ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles) { + bool LoopWhileToRemove = true; + while (LoopWhileToRemove) { + LoopWhileToRemove = false; + for (unsigned I = 0; I < SK_Count; ++I) { + if (const std::optional<IdentifierNamingCheck::NamingStyle> &OtherStyle = + NamingStyles[I]) { + while (!OtherStyle->Prefix.empty() && + Mid.consume_front(OtherStyle->Prefix)) + LoopWhileToRemove = true; + + while (!OtherStyle->Suffix.empty() && Mid.consume_back(OtherStyle->Suffix)) + LoopWhileToRemove = true; + } + } + } +} + std::string IdentifierNamingCheck::fixupWithStyle( StringRef Type, StringRef Name, const IdentifierNamingCheck::NamingStyle &Style, + ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles, const IdentifierNamingCheck::HungarianNotationOption &HNOption, const Decl *D) const { Name.consume_front(Style.Prefix); @@ -1108,6 +1136,8 @@ std::string IdentifierNamingCheck::fixupWithStyle( } } StringRef Mid = StringRef(Fixed).trim("_"); + if (Style.TrimPrefixSuffix) + trimPrefixesAndSuffixes(Mid, NamingStyles); if (Mid.empty()) Mid = "_"; @@ -1359,7 +1389,8 @@ IdentifierNamingCheck::getFailureInfo( IdentifierNamingCheck::CT_LowerCase); llvm::replace(KindName, '_', ' '); - std::string Fixup = fixupWithStyle(Type, Name, Style, HNOption, ND); + std::string Fixup = + fixupWithStyle(Type, Name, Style, NamingStyles, HNOption, ND); if (StringRef(Fixup) == Name) { if (!IgnoreFailedSplit) { LLVM_DEBUG(Location.print(llvm::dbgs(), SM); diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h index b016556cc2ab8..bdac7f5b1e8b9 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @@ -72,7 +72,7 @@ class IdentifierNamingCheck final : public RenamerClangTidyCheck { NamingStyle(std::optional<CaseType> Case, StringRef Prefix, StringRef Suffix, StringRef IgnoredRegexpStr, - HungarianPrefixType HPType); + HungarianPrefixType HPType, bool TrimPrefixSuffix); NamingStyle(const NamingStyle &O) = delete; NamingStyle &operator=(NamingStyle &&O) = default; NamingStyle(NamingStyle &&O) = default; @@ -86,6 +86,8 @@ class IdentifierNamingCheck final : public RenamerClangTidyCheck { std::string IgnoredRegexpStr; HungarianPrefixType HPType; + + bool TrimPrefixSuffix; }; struct HungarianNotation { @@ -176,6 +178,7 @@ class IdentifierNamingCheck final : public RenamerClangTidyCheck { std::string fixupWithStyle(StringRef Type, StringRef Name, const IdentifierNamingCheck::NamingStyle &Style, + ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles, const IdentifierNamingCheck::HungarianNotationOption &HNOption, const Decl *D) const; 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 c8f87dcba8c0a..7d6a1e5e813f9 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 @@ -89,166 +89,211 @@ The available options are summarized below: **Specific options** - :option:`DefaultCase`, :option:`DefaultPrefix`, - :option:`DefaultSuffix`, :option:`DefaultIgnoredRegexp`, - :option:`DefaultHungarianPrefix` + :option:`DefaultSuffix`, :option:`DefaultTrimPrefixSuffix`, + :option:`DefaultIgnoredRegexp`, :option:`DefaultHungarianPrefix` - :option:`AbstractClassCase`, :option:`AbstractClassPrefix`, - :option:`AbstractClassSuffix`, :option:`AbstractClassIgnoredRegexp`, - :option:`AbstractClassHungarianPrefix` + :option:`AbstractClassSuffix`, :option:`AbstractClassTrimPrefixSuffix`, + :option:`AbstractClassIgnoredRegexp`, :option:`AbstractClassHungarianPrefix` - :option:`ClassCase`, :option:`ClassPrefix`, :option:`ClassSuffix`, - :option:`ClassIgnoredRegexp`, :option:`ClassHungarianPrefix` + :option:`ClassTrimPrefixSuffix`, :option:`ClassIgnoredRegexp`, + :option:`ClassHungarianPrefix` - :option:`ClassConstexprCase`, :option:`ClassConstexprPrefix`, - :option:`ClassConstexprSuffix`, :option:`ClassConstexprIgnoredRegexp`, + :option:`ClassConstexprSuffix`, :option:`ClassConstexprTrimPrefixSuffix`, + :option:`ClassConstexprIgnoredRegexp`, :option:`ClassConstexprHungarianPrefix` - :option:`ClassConstantCase`, :option:`ClassConstantPrefix`, - :option:`ClassConstantSuffix`, :option:`ClassConstantIgnoredRegexp`, - :option:`ClassConstantHungarianPrefix` + :option:`ClassConstantSuffix`, :option:`ClassConstantTrimPrefixSuffix`, + :option:`ClassConstantIgnoredRegexp`, :option:`ClassConstantHungarianPrefix` - :option:`ClassMemberCase`, :option:`ClassMemberPrefix`, - :option:`ClassMemberSuffix`, :option:`ClassMemberIgnoredRegexp`, - :option:`ClassMemberHungarianPrefix` + :option:`ClassMemberSuffix`, :option:`ClassMemberTrimPrefixSuffix`, + :option:`ClassMemberIgnoredRegexp`, :option:`ClassMemberHungarianPrefix` - :option:`ClassMethodCase`, :option:`ClassMethodPrefix`, - :option:`ClassMethodSuffix`, :option:`ClassMethodIgnoredRegexp` + :option:`ClassMethodSuffix`, :option:`ClassMethodTrimPrefixSuffix`, + :option:`ClassMethodIgnoredRegexp` - :option:`ConceptCase`, :option:`ConceptPrefix`, :option:`ConceptSuffix`, - :option:`ConceptIgnoredRegexp` + :option:`ConceptTrimPrefixSuffix`, :option:`ConceptIgnoredRegexp` - :option:`ConstantCase`, :option:`ConstantPrefix`, :option:`ConstantSuffix`, - :option:`ConstantIgnoredRegexp`, :option:`ConstantHungarianPrefix` + :option:`ConstantTrimPrefixSuffix`, :option:`ConstantIgnoredRegexp`, + :option:`ConstantHungarianPrefix` - :option:`ConstantMemberCase`, :option:`ConstantMemberPrefix`, - :option:`ConstantMemberSuffix`, :option:`ConstantMemberIgnoredRegexp`, + :option:`ConstantMemberSuffix`, + :option:`ConstantMemberTrimPrefixSuffix`, + :option:`ConstantMemberIgnoredRegexp`, :option:`ConstantMemberHungarianPrefix` - :option:`ConstantParameterCase`, :option:`ConstantParameterPrefix`, - :option:`ConstantParameterSuffix`, :option:`ConstantParameterIgnoredRegexp`, + :option:`ConstantParameterSuffix`, + :option:`ConstantParameterTrimPrefixSuffix`, + :option:`ConstantParameterIgnoredRegexp`, :option:`ConstantParameterHungarianPrefix` - :option:`ConstantPointerParameterCase`, :option:`ConstantPointerParameterPrefix`, :option:`ConstantPointerParameterSuffix`, + :option:`ConstantPointerParameterTrimPrefixSuffix`, :option:`ConstantPointerParameterIgnoredRegexp`, :option:`ConstantPointerParameterHungarianPrefix` - :option:`ConstexprFunctionCase`, :option:`ConstexprFunctionPrefix`, - :option:`ConstexprFunctionSuffix`, :option:`ConstexprFunctionIgnoredRegexp` + :option:`ConstexprFunctionSuffix`, + :option:`ConstexprFunctionTrimPrefixSuffix`, + :option:`ConstexprFunctionIgnoredRegexp` - :option:`ConstexprMethodCase`, :option:`ConstexprMethodPrefix`, - :option:`ConstexprMethodSuffix`, :option:`ConstexprMethodIgnoredRegexp` + :option:`ConstexprMethodSuffix`, + :option:`ConstexprMethodTrimPrefixSuffix`, + :option:`ConstexprMethodIgnoredRegexp` - :option:`ConstexprVariableCase`, :option:`ConstexprVariablePrefix`, - :option:`ConstexprVariableSuffix`, :option:`ConstexprVariableIgnoredRegexp`, + :option:`ConstexprVariableSuffix`, + :option:`ConstexprVariableTrimPrefixSuffix`, + :option:`ConstexprVariableIgnoredRegexp`, :option:`ConstexprVariableHungarianPrefix` - :option:`EnumCase`, :option:`EnumPrefix`, :option:`EnumSuffix`, - :option:`EnumIgnoredRegexp` + :option:`EnumTrimPrefixSuffix`, :option:`EnumIgnoredRegexp` - :option:`EnumConstantCase`, :option:`EnumConstantPrefix`, - :option:`EnumConstantSuffix`, :option:`EnumConstantIgnoredRegexp`, - :option:`EnumConstantHungarianPrefix` + :option:`EnumConstantSuffix`, :option:`EnumConstantTrimPrefixSuffix`, + :option:`EnumConstantIgnoredRegexp`, :option:`EnumConstantHungarianPrefix` - :option:`FunctionCase`, :option:`FunctionPrefix`, :option:`FunctionSuffix`, - :option:`FunctionIgnoredRegexp` + :option:`FunctionTrimPrefixSuffix`, :option:`FunctionIgnoredRegexp` - :option:`GlobalConstexprVariableCase`, :option:`GlobalConstexprVariablePrefix`, :option:`GlobalConstexprVariableSuffix`, + :option:`GlobalConstexprVariableTrimPrefixSuffix`, :option:`GlobalConstexprVariableIgnoredRegexp`, :option:`GlobalConstexprVariableHungarianPrefix` - :option:`GlobalConstantCase`, :option:`GlobalConstantPrefix`, - :option:`GlobalConstantSuffix`, :option:`GlobalConstantIgnoredRegexp`, + :option:`GlobalConstantSuffix`, :option:`GlobalConstantTrimPrefixSuffix`, + :option:`GlobalConstantIgnoredRegexp`, :option:`GlobalConstantHungarianPrefix` - :option:`GlobalConstantPointerCase`, :option:`GlobalConstantPointerPrefix`, :option:`GlobalConstantPointerSuffix`, + :option:`GlobalConstantPointerTrimPrefixSuffix`, :option:`GlobalConstantPointerIgnoredRegexp`, :option:`GlobalConstantPointerHungarianPrefix` - :option:`GlobalFunctionCase`, :option:`GlobalFunctionPrefix`, - :option:`GlobalFunctionSuffix`, :option:`GlobalFunctionIgnoredRegexp` + :option:`GlobalFunctionSuffix`, :option:`GlobalFunctionTrimPrefixSuffix`, + :option:`GlobalFunctionIgnoredRegexp` - :option:`GlobalPointerCase`, :option:`GlobalPointerPrefix`, - :option:`GlobalPointerSuffix`, :option:`GlobalPointerIgnoredRegexp`, - :option:`GlobalPointerHungarianPrefix` + :option:`GlobalPointerSuffix`, :option:`GlobalPointerTrimPrefixSuffix`, + :option:`GlobalPointerIgnoredRegexp`, :option:`GlobalPointerHungarianPrefix` - :option:`GlobalVariableCase`, :option:`GlobalVariablePrefix`, - :option:`GlobalVariableSuffix`, :option:`GlobalVariableIgnoredRegexp`, + :option:`GlobalVariableSuffix`, :option:`GlobalVariableTrimPrefixSuffix`, + :option:`GlobalVariableIgnoredRegexp`, :option:`GlobalVariableHungarianPrefix` - :option:`InlineNamespaceCase`, :option:`InlineNamespacePrefix`, - :option:`InlineNamespaceSuffix`, :option:`InlineNamespaceIgnoredRegexp` + :option:`InlineNamespaceSuffix`, :option:`InlineNamespaceTrimPrefixSuffix`, + :option:`InlineNamespaceIgnoredRegexp` - :option:`LocalConstexprVariableCase`, :option:`LocalConstexprVariablePrefix`, :option:`LocalConstexprVariableSuffix`, + :option:`LocalConstexprVariableTrimPrefixSuffix`, :option:`LocalConstexprVariableIgnoredRegexp`, :option:`LocalConstexprVariableHungarianPrefix` - :option:`LocalConstantCase`, :option:`LocalConstantPrefix`, - :option:`LocalConstantSuffix`, :option:`LocalConstantIgnoredRegexp`, - :option:`LocalConstantHungarianPrefix` + :option:`LocalConstantSuffix`, :option:`LocalConstantTrimPrefixSuffix`, + :option:`LocalConstantIgnoredRegexp`, :option:`LocalConstantHungarianPrefix` - :option:`LocalConstantPointerCase`, :option:`LocalConstantPointerPrefix`, :option:`LocalConstantPointerSuffix`, + :option:`LocalConstantPointerTrimPrefixSuffix`, :option:`LocalConstantPointerIgnoredRegexp`, :option:`LocalConstantPointerHungarianPrefix` - :option:`LocalPointerCase`, :option:`LocalPointerPrefix`, - :option:`LocalPointerSuffix`, :option:`LocalPointerIgnoredRegexp`, - :option:`LocalPointerHungarianPrefix` + :option:`LocalPointerSuffix`, :option:`LocalPointerTrimPrefixSuffix`, + :option:`LocalPointerIgnoredRegexp`, :option:`LocalPointerHungarianPrefix` - :option:`LocalVariableCase`, :option:`LocalVariablePrefix`, - :option:`LocalVariableSuffix`, :option:`LocalVariableIgnoredRegexp`, - :option:`LocalVariableHungarianPrefix` + :option:`LocalVariableSuffix`, :option:`LocalVariableTrimPrefixSuffix`, + :option:`LocalVariableIgnoredRegexp`, :option:`LocalVariableHungarianPrefix` - :option:`MacroDefinitionCase`, :option:`MacroDefinitionPrefix`, - :option:`MacroDefinitionSuffix`, :option:`MacroDefinitionIgnoredRegexp` + :option:`MacroDefinitionSuffix`, :option:`MacroDefinitionTrimPrefixSuffix`, + :option:`MacroDefinitionIgnoredRegexp` - :option:`MemberCase`, :option:`MemberPrefix`, :option:`MemberSuffix`, - :option:`MemberIgnoredRegexp`, :option:`MemberHungarianPrefix` + :option:`MemberTrimPrefixSuffix`, :option:`MemberIgnoredRegexp`, + :option:`MemberHungarianPrefix` - :option:`MethodCase`, :option:`MethodPrefix`, :option:`MethodSuffix`, - :option:`MethodIgnoredRegexp` + :option:`MethodTrimPrefixSuffix`, :option:`MethodIgnoredRegexp` - :option:`NamespaceCase`, :option:`NamespacePrefix`, - :option:`NamespaceSuffix`, :option:`NamespaceIgnoredRegexp` + :option:`NamespaceSuffix`, :option:`NamespaceTrimPrefixSuffix`, + :option:`NamespaceIgnoredRegexp` - :option:`ParameterCase`, :option:`ParameterPrefix`, - :option:`ParameterSuffix`, :option:`ParameterIgnoredRegexp`, - :option:`ParameterHungarianPrefix` + :option:`ParameterSuffix`, :option:`ParameterTrimPrefixSuffix`, + :option:`ParameterIgnoredRegexp`, :option:`ParameterHungarianPrefix` - :option:`ParameterPackCase`, :option:`ParameterPackPrefix`, - :option:`ParameterPackSuffix`, :option:`ParameterPackIgnoredRegexp` + :option:`ParameterPackSuffix`, :option:`ParameterPackTrimPrefixSuffix`, + :option:`ParameterPackIgnoredRegexp` - :option:`PointerParameterCase`, :option:`PointerParameterPrefix`, - :option:`PointerParameterSuffix`, :option:`PointerParameterIgnoredRegexp`, + :option:`PointerParameterSuffix`, + :option:`PointerParameterTrimPrefixSuffix`, + :option:`PointerParameterIgnoredRegexp`, :option:`PointerParameterHungarianPrefix` - :option:`PrivateMemberCase`, :option:`PrivateMemberPrefix`, - :option:`PrivateMemberSuffix`, :option:`PrivateMemberIgnoredRegexp`, - :option:`PrivateMemberHungarianPrefix` + :option:`PrivateMemberSuffix`, :option:`PrivateMemberTrimPrefixSuffix`, + :option:`PrivateMemberIgnoredRegexp`, :option:`PrivateMemberHungarianPrefix` - :option:`PrivateMethodCase`, :option:`PrivateMethodPrefix`, - :option:`PrivateMethodSuffix`, :option:`PrivateMethodIgnoredRegexp` + :option:`PrivateMethodSuffix`, :option:`PrivateMethodTrimPrefixSuffix`, + :option:`PrivateMethodIgnoredRegexp` - :option:`ProtectedMemberCase`, :option:`ProtectedMemberPrefix`, - :option:`ProtectedMemberSuffix`, :option:`ProtectedMemberIgnoredRegexp`, + :option:`ProtectedMemberSuffix`, :option:`ProtectedMemberTrimPrefixSuffix`, + :option:`ProtectedMemberIgnoredRegexp`, :option:`ProtectedMemberHungarianPrefix` - :option:`ProtectedMethodCase`, :option:`ProtectedMethodPrefix`, - :option:`ProtectedMethodSuffix`, :option:`ProtectedMethodIgnoredRegexp` + :option:`ProtectedMethodSuffix`, :option:`ProtectedMethodTrimPrefixSuffix`, + :option:`ProtectedMethodIgnoredRegexp` - :option:`PublicMemberCase`, :option:`PublicMemberPrefix`, - :option:`PublicMemberSuffix`, :option:`PublicMemberIgnoredRegexp`, - :option:`PublicMemberHungarianPrefix` + :option:`PublicMemberSuffix`, :option:`PublicMemberTrimPrefixSuffix`, + :option:`PublicMemberIgnoredRegexp`, :option:`PublicMemberHungarianPrefix` - :option:`PublicMethodCase`, :option:`PublicMethodPrefix`, - :option:`PublicMethodSuffix`, :option:`PublicMethodIgnoredRegexp` + :option:`PublicMethodSuffix`, :option:`PublicMethodTrimPrefixSuffix`, + :option:`PublicMethodIgnoredRegexp` - :option:`ScopedEnumConstantCase`, :option:`ScopedEnumConstantPrefix`, :option:`ScopedEnumConstantSuffix`, + :option:`ScopedEnumConstantTrimPrefixSuffix`, :option:`ScopedEnumConstantIgnoredRegexp` - :option:`StaticConstexprVariableCase`, :option:`StaticConstexprVariablePrefix`, :option:`StaticConstexprVariableSuffix`, + :option:`StaticConstexprVariableTrimPrefixSuffix`, :option:`StaticConstexprVariableIgnoredRegexp`, :option:`StaticConstexprVariableHungarianPrefix` - :option:`StaticConstantCase`, :option:`StaticConstantPrefix`, - :option:`StaticConstantSuffix`, :option:`StaticConstantIgnoredRegexp`, + :option:`StaticConstantSuffix`, :option:`StaticConstantTrimPrefixSuffix`, + :option:`StaticConstantIgnoredRegexp`, :option:`StaticConstantHungarianPrefix` - :option:`StaticVariableCase`, :option:`StaticVariablePrefix`, - :option:`StaticVariableSuffix`, :option:`StaticVariableIgnoredRegexp`, + :option:`StaticVariableSuffix`, :option:`StaticVariableTrimPrefixSuffix`, + :option:`StaticVariableIgnoredRegexp`, :option:`StaticVariableHungarianPrefix` - :option:`StructCase`, :option:`StructPrefix`, :option:`StructSuffix`, - :option:`StructIgnoredRegexp` + :option:`StructTrimPrefixSuffix`, :option:`StructIgnoredRegexp` - :option:`TemplateParameterCase`, :option:`TemplateParameterPrefix`, - :option:`TemplateParameterSuffix`, :option:`TemplateParameterIgnoredRegexp` + :option:`TemplateParameterSuffix`, + :option:`TemplateParameterTrimPrefixSuffix`, + :option:`TemplateParameterIgnoredRegexp` - :option:`TemplateTemplateParameterCase`, :option:`TemplateTemplateParameterPrefix`, :option:`TemplateTemplateParameterSuffix`, + :option:`TemplateTemplateParameterTrimPrefixSuffix`, :option:`TemplateTemplateParameterIgnoredRegexp` - :option:`TypeAliasCase`, :option:`TypeAliasPrefix`, - :option:`TypeAliasSuffix`, :option:`TypeAliasIgnoredRegexp` + :option:`TypeAliasSuffix`, :option:`TypeAliasTrimPrefixSuffix`, + :option:`TypeAliasIgnoredRegexp` - :option:`TypedefCase`, :option:`TypedefPrefix`, :option:`TypedefSuffix`, - :option:`TypedefIgnoredRegexp` + :option:`TypedefTrimPrefixSuffix`, :option:`TypedefIgnoredRegexp` - :option:`TypeTemplateParameterCase`, :option:`TypeTemplateParameterPrefix`, :option:`TypeTemplateParameterSuffix`, + :option:`TypeTemplateParameterTrimPrefixSuffix`, :option:`TypeTemplateParameterIgnoredRegexp` - :option:`UnionCase`, :option:`UnionPrefix`, :option:`UnionSuffix`, - :option:`UnionIgnoredRegexp` + :option:`UnionTrimPrefixSuffix`, :option:`UnionIgnoredRegexp` - :option:`ValueTemplateParameterCase`, :option:`ValueTemplateParameterPrefix`, :option:`ValueTemplateParameterSuffix`, + :option:`ValueTemplateParameterTrimPrefixSuffix`, :option:`ValueTemplateParameterIgnoredRegexp` - - :option:`VariableCase`, :option:`VariablePrefix`, :option:`VariableSuffix`, + - :option:`VariableCase`, :option:`VariablePrefix`, + :option:`VariableSuffix`, :option:`VariableTrimPrefixSuffix`, :option:`VariableIgnoredRegexp`, :option:`VariableHungarianPrefix` - :option:`VirtualMethodCase`, :option:`VirtualMethodPrefix`, - :option:`VirtualMethodSuffix`, :option:`VirtualMethodIgnoredRegexp` + :option:`VirtualMethodSuffix`, :option:`VirtualMethodTrimPrefixSuffix`, + :option:`VirtualMethodIgnoredRegexp` Options description @@ -276,6 +321,11 @@ A detailed description of each option is presented below: When defined, the check will ensure all names by default will add the suffix with the given value (regardless of casing). +.. option:: DefaultTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: DefaultHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -301,6 +351,11 @@ A detailed description of each option is presented below: When defined, the check will ensure abstract class names will add the suffix with the given value (regardless of casing). +.. option:: AbstractClassTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: AbstractClassHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -438,6 +493,11 @@ and thus no warnings will be emitted. When defined, the check will ensure class names will add the suffix with the given value (regardless of casing). +.. option:: ClassTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ClassHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -492,6 +552,11 @@ After: When defined, the check will ensure class ``constexpr`` names will add the suffix with the given value (regardless of casing). +.. option:: ClassConstexprTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ClassConstexprHungarianPrefix When enabled, the check ensures that the declared identifier will have a @@ -544,6 +609,11 @@ After: When defined, the check will ensure class constant names will add the suffix with the given value (regardless of casing). +.. option:: ClassConstantTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ClassConstantHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -596,6 +666,11 @@ After: When defined, the check will ensure class member names will add the suffix with the given value (regardless of casing). +.. option:: ClassMemberTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ClassMemberHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -734,6 +809,11 @@ After: When defined, the check will ensure constant names will add the suffix with the given value (regardless of casing). +.. option:: ConstantTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ConstantHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -780,6 +860,11 @@ After: When defined, the check will ensure constant member names will add the suffix with the given value (regardless of casing). +.. option:: ConstantMemberTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ConstantMemberHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -830,6 +915,11 @@ After: When defined, the check will ensure constant parameter names will add the suffix with the given value (regardless of casing). +.. option:: ConstantParameterTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ConstantParameterHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -876,6 +966,11 @@ After: When defined, the check will ensure constant pointer parameter names will add the suffix with the given value (regardless of casing). +.. option:: ConstantPointerParameterTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ConstantPointerParameterHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1008,6 +1103,11 @@ After: When defined, the check will ensure constexpr variable names will add the suffix with the given value (regardless of casing). +.. option:: ConstexprVariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ConstexprVariableHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1094,6 +1194,11 @@ After: When defined, the check will ensure enumeration constant names will add the suffix with the given value (regardless of casing). +.. option:: EnumConstantTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: EnumConstantHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1187,6 +1292,11 @@ After: When defined, the check will ensure global ``constexpr`` variable names will add the suffix with the given value (regardless of casing). +.. option:: GlobalConstexprVariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: GlobalConstexprVariableHungarianPrefix When enabled, the check ensures that the declared identifier will have a @@ -1233,6 +1343,11 @@ After: When defined, the check will ensure global constant names will add the suffix with the given value (regardless of casing). +.. option:: GlobalConstantTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: GlobalConstantHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1279,6 +1394,11 @@ After: When defined, the check will ensure global constant pointer names will add the suffix with the given value (regardless of casing). +.. option:: GlobalConstantPointerTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: GlobalConstantPointerHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1365,6 +1485,11 @@ After: When defined, the check will ensure global pointer names will add the suffix with the given value (regardless of casing). +.. option:: GlobalPointerTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: GlobalPointerHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1411,6 +1536,11 @@ After: When defined, the check will ensure global variable names will add the suffix with the given value (regardless of casing). +.. option:: GlobalVariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: GlobalVariableHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1511,6 +1641,11 @@ After: When defined, the check will ensure local ``constexpr`` variable names will add the suffix with the given value (regardless of casing). +.. option:: LocalConstexprVariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: LocalConstexprVariableHungarianPrefix When enabled, the check ensures that the declared identifier will have a @@ -1557,6 +1692,11 @@ After: When defined, the check will ensure local constant names will add the suffix with the given value (regardless of casing). +.. option:: LocalConstantTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: LocalConstantHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1603,6 +1743,11 @@ After: When defined, the check will ensure local constant pointer names will add the suffix with the given value (regardless of casing). +.. option:: LocalConstantPointerTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: LocalConstantPointerHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1649,6 +1794,11 @@ After: When defined, the check will ensure local pointer names will add the suffix with the given value (regardless of casing). +.. option:: LocalPointerTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: LocalPointerHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1703,6 +1853,11 @@ camel case check applied to other variables. When defined, the check will ensure local variable names will add the suffix with the given value (regardless of casing). +.. option:: LocalVariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: LocalVariableHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1792,6 +1947,11 @@ command line using the ``-D`` flag. When defined, the check will ensure member names will add the suffix with the given value (regardless of casing). +.. option:: MemberTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: MemberHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -1930,6 +2090,11 @@ After: When defined, the check will ensure parameter names will add the suffix with the given value (regardless of casing). +.. option:: ParameterTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ParameterHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2020,6 +2185,11 @@ After: When defined, the check will ensure pointer parameter names will add the suffix with the given value (regardless of casing). +.. option:: PointerParameterTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: PointerParameterHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2066,6 +2236,11 @@ After: When defined, the check will ensure private member names will add the suffix with the given value (regardless of casing). +.. option:: PrivateMemberTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: PrivateMemberHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2164,6 +2339,11 @@ After: When defined, the check will ensure protected member names will add the suffix with the given value (regardless of casing). +.. option:: ProtectedMemberTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ProtectedMemberHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2262,6 +2442,11 @@ After: When defined, the check will ensure public member names will add the suffix with the given value (regardless of casing). +.. option:: PublicMemberTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: PublicMemberHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2360,6 +2545,11 @@ After: When defined, the check will ensure scoped enum constant names will add the suffix with the given value (regardless of casing). +.. option:: ScopedEnumConstantTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: ScopedEnumConstantHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2406,6 +2596,11 @@ After: When defined, the check will ensure static ``constexpr`` variable names will add the suffix with the given value (regardless of casing). +.. option:: StaticConstexprVariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: StaticConstexprVariableHungarianPrefix When enabled, the check ensures that the declared identifier will have a @@ -2452,6 +2647,11 @@ After: When defined, the check will ensure static constant names will add the suffix with the given value (regardless of casing). +.. option:: StaticConstantTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: StaticConstantHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2498,6 +2698,11 @@ After: When defined, the check will ensure static variable names will add the suffix with the given value (regardless of casing). +.. option:: StaticVariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: StaticVariableHungarianPrefix When enabled, the check ensures that the declared identifier will @@ -2882,6 +3087,11 @@ After: When defined, the check will ensure variable names will add the suffix with the given value (regardless of casing). +.. option:: VariableTrimPrefixSuffix + + When enabled, the check ensures that the declared identifier does not + start or end with any other prefix or suffix defined for this check + .. option:: VariableHungarianPrefix When enabled, the check ensures that the declared identifier will diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-trim.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-trim.cpp new file mode 100644 index 0000000000000..58ee64b8a7b30 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-trim.cpp @@ -0,0 +1,28 @@ +// RUN: %check_clang_tidy -std=c++20 %s readability-identifier-naming %t -- \ +// RUN: -config='{CheckOptions: { \ +// RUN: readability-identifier-naming.MemberPrefix: 'm_', \ +// RUN: readability-identifier-naming.MemberSuffix: '_', \ +// RUN: readability-identifier-naming.MemberTrimPrefixSuffix: 1, \ +// RUN: readability-identifier-naming.ParameterPrefix: 'p_', \ +// RUN: readability-identifier-naming.ParameterTrimPrefixSuffix: 1, \ +// RUN: }}' \ +// RUN: -header-filter='' \ +// RUN: -- -fno-delayed-template-parsing -Dbad_macro \ +// RUN: -I%S/Inputs/identifier-naming \ +// RUN: -isystem %S/Inputs/identifier-naming/system + +// clang-format off + +struct Triple { + Triple(int m_wrong_, int missing, int p_ok): p_wrong_(m_wrong_), missing(missing), m_ok_(p_ok) {} + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: invalid case style for parameter 'm_wrong_' + // CHECK-MESSAGES: :[[@LINE-2]]:30: warning: invalid case style for parameter 'missing' + // CHECK-FIXES: Triple(int p_wrong, int p_missing, int p_ok): m_wrong_(p_wrong), m_missing_(p_missing), m_ok_(p_ok) {} + int p_wrong_; + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for member 'p_wrong_' + // CHECK-FIXES: int m_wrong_; + int missing; + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for member 'missing' + // CHECK-FIXES: int m_missing_; + int m_ok_; +}; \ No newline at end of file _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
