diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp index 175feea0faae..95a71f63986f 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -449,13 +449,13 @@ static StyleKind findStyleKind( if (const auto *Decl = dyn_cast(D)) { QualType Type = Decl->getType(); - if (!Type.isNull() && Type.isLocalConstQualified() && - NamingStyles[SK_ConstantMember]) - return SK_ConstantMember; + if (!Type.isNull() && Type.isConstQualified()) { + if (NamingStyles[SK_ConstantMember]) + return SK_ConstantMember; - if (!Type.isNull() && Type.isLocalConstQualified() && - NamingStyles[SK_Constant]) - return SK_Constant; + if (NamingStyles[SK_Constant]) + return SK_Constant; + } if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember]) return SK_PrivateMember; @@ -478,13 +478,13 @@ static StyleKind findStyleKind( if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable]) return SK_ConstexprVariable; - if (!Type.isNull() && Type.isLocalConstQualified() && - NamingStyles[SK_ConstantParameter]) - return SK_ConstantParameter; + if (!Type.isNull() && Type.isConstQualified()) { + if (NamingStyles[SK_ConstantParameter]) + return SK_ConstantParameter; - if (!Type.isNull() && Type.isLocalConstQualified() && - NamingStyles[SK_Constant]) - return SK_Constant; + if (NamingStyles[SK_Constant]) + return SK_Constant; + } if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack]) return SK_ParameterPack; @@ -501,29 +501,25 @@ static StyleKind findStyleKind( if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable]) return SK_ConstexprVariable; - if (!Type.isNull() && Type.isLocalConstQualified() && - Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant]) - return SK_ClassConstant; + if (!Type.isNull() && Type.isConstQualified()) { + if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant]) + return SK_ClassConstant; - if (!Type.isNull() && Type.isLocalConstQualified() && - Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant]) - return SK_GlobalConstant; + if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant]) + return SK_GlobalConstant; - if (!Type.isNull() && Type.isLocalConstQualified() && - Decl->isStaticLocal() && NamingStyles[SK_StaticConstant]) - return SK_StaticConstant; + if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant]) + return SK_StaticConstant; - if (!Type.isNull() && Type.isLocalConstQualified() && - Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant]) - return SK_LocalConstant; + if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant]) + return SK_LocalConstant; - if (!Type.isNull() && Type.isLocalConstQualified() && - Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant]) - return SK_LocalConstant; + if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant]) + return SK_LocalConstant; - if (!Type.isNull() && Type.isLocalConstQualified() && - NamingStyles[SK_Constant]) - return SK_Constant; + if (NamingStyles[SK_Constant]) + return SK_Constant; + } if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember]) return SK_ClassMember; @@ -681,8 +677,9 @@ static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures, static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures, const NamedDecl *Decl, SourceRange Range, SourceManager *SourceMgr = nullptr) { - return addUsage(Failures, IdentifierNamingCheck::NamingCheckId( - Decl->getLocation(), Decl->getNameAsString()), + return addUsage(Failures, + IdentifierNamingCheck::NamingCheckId(Decl->getLocation(), + Decl->getNameAsString()), Range, SourceMgr); } @@ -699,7 +696,8 @@ void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) { if (!Init->isWritten() || Init->isInClassMemberInitializer()) continue; if (const auto *FD = Init->getAnyMember()) - addUsage(NamingCheckFailures, FD, SourceRange(Init->getMemberLocation())); + addUsage(NamingCheckFailures, FD, + SourceRange(Init->getMemberLocation())); // Note: delegating constructors and base class initializers are handled // via the "typeLoc" matcher. } diff --git a/clang-tools-extra/test/clang-tidy/readability-identifier-naming.cpp b/clang-tools-extra/test/clang-tidy/readability-identifier-naming.cpp index 60bd69a40909..436e8443ce62 100644 --- a/clang-tools-extra/test/clang-tidy/readability-identifier-naming.cpp +++ b/clang-tools-extra/test/clang-tidy/readability-identifier-naming.cpp @@ -405,6 +405,38 @@ static void static_Function() { using ::FOO_NS::InlineNamespace::CE_function; // CHECK-FIXES: {{^}} using ::foo_ns::inline_namespace::ce_function;{{$}} + + unsigned MY_LOCAL_array[] = {1,2,3}; +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: invalid case style for local variable 'MY_LOCAL_array' +// CHECK-FIXES: {{^}} unsigned my_local_array[] = {1,2,3};{{$}} + + unsigned const MyConstLocal_array[] = {1,2,3}; +// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: invalid case style for local constant 'MyConstLocal_array' +// CHECK-FIXES: {{^}} unsigned const kMyConstLocalArray[] = {1,2,3};{{$}} + + static unsigned MY_STATIC_array[] = {1,2,3}; +// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: invalid case style for static variable 'MY_STATIC_array' +// CHECK-FIXES: {{^}} static unsigned s_myStaticArray[] = {1,2,3};{{$}} + + static unsigned const MyConstStatic_array[] = {1,2,3}; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: invalid case style for static constant 'MyConstStatic_array' +// CHECK-FIXES: {{^}} static unsigned const MY_CONST_STATIC_ARRAY[] = {1,2,3};{{$}} + + char MY_LOCAL_string[] = "123"; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for local variable 'MY_LOCAL_string' +// CHECK-FIXES: {{^}} char my_local_string[] = "123";{{$}} + + char const MyConstLocal_string[] = "123"; +// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: invalid case style for local constant 'MyConstLocal_string' +// CHECK-FIXES: {{^}} char const kMyConstLocalString[] = "123";{{$}} + + static char MY_STATIC_string[] = "123"; +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: invalid case style for static variable 'MY_STATIC_string' +// CHECK-FIXES: {{^}} static char s_myStaticString[] = "123";{{$}} + + static char const MyConstStatic_string[] = "123"; +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for static constant 'MyConstStatic_string' +// CHECK-FIXES: {{^}} static char const MY_CONST_STATIC_STRING[] = "123";{{$}} } #define MY_TEST_Macro(X) X() @@ -418,6 +450,27 @@ void MY_TEST_Macro(function) {} template struct a { typename t_t::template b<> c; + + char const MY_ConstMember_string[4] = "123"; +// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: invalid case style for constant member 'MY_ConstMember_string' +// CHECK-FIXES: {{^}} char const my_const_member_string[4] = "123";{{$}} + + static char const MyConstClass_string[]; +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for class constant 'MyConstClass_string' +// CHECK-FIXES: {{^}} static char const kMyConstClassString[];{{$}} }; +template +char const a::MyConstClass_string[] = "123"; +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for class constant 'MyConstClass_string' +// CHECK-FIXES: {{^}}char const a::kMyConstClassString[] = "123";{{$}} + template