[clang-tidy] Correctly classify constant arrays and constant strings as constants when checking identifiers naming

Summary:
They are not locally const qualified so they weren't classified as
constants by the readability-identifier-naming check.

Reviewers: alexfh

Reviewed By: alexfh

Subscribers: klimek, cfe-commits, xazax.hun

Patch by Beren Minor!

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

llvm-svn: 320406
This commit is contained in:
Alexander Kornienko 2017-12-11 19:02:26 +00:00
parent b1c8f45249
commit ba874922a2
2 changed files with 84 additions and 33 deletions

View File

@ -449,13 +449,13 @@ static StyleKind findStyleKind(
if (const auto *Decl = dyn_cast<FieldDecl>(D)) { if (const auto *Decl = dyn_cast<FieldDecl>(D)) {
QualType Type = Decl->getType(); QualType Type = Decl->getType();
if (!Type.isNull() && Type.isLocalConstQualified() && if (!Type.isNull() && Type.isConstQualified()) {
NamingStyles[SK_ConstantMember]) if (NamingStyles[SK_ConstantMember])
return SK_ConstantMember; return SK_ConstantMember;
if (!Type.isNull() && Type.isLocalConstQualified() && if (NamingStyles[SK_Constant])
NamingStyles[SK_Constant]) return SK_Constant;
return SK_Constant; }
if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember]) if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
return SK_PrivateMember; return SK_PrivateMember;
@ -478,13 +478,13 @@ static StyleKind findStyleKind(
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable]) if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
return SK_ConstexprVariable; return SK_ConstexprVariable;
if (!Type.isNull() && Type.isLocalConstQualified() && if (!Type.isNull() && Type.isConstQualified()) {
NamingStyles[SK_ConstantParameter]) if (NamingStyles[SK_ConstantParameter])
return SK_ConstantParameter; return SK_ConstantParameter;
if (!Type.isNull() && Type.isLocalConstQualified() && if (NamingStyles[SK_Constant])
NamingStyles[SK_Constant]) return SK_Constant;
return SK_Constant; }
if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack]) if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack])
return SK_ParameterPack; return SK_ParameterPack;
@ -501,29 +501,25 @@ static StyleKind findStyleKind(
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable]) if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
return SK_ConstexprVariable; return SK_ConstexprVariable;
if (!Type.isNull() && Type.isLocalConstQualified() && if (!Type.isNull() && Type.isConstQualified()) {
Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant]) if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
return SK_ClassConstant; return SK_ClassConstant;
if (!Type.isNull() && Type.isLocalConstQualified() && if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant]) return SK_GlobalConstant;
return SK_GlobalConstant;
if (!Type.isNull() && Type.isLocalConstQualified() && if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
Decl->isStaticLocal() && NamingStyles[SK_StaticConstant]) return SK_StaticConstant;
return SK_StaticConstant;
if (!Type.isNull() && Type.isLocalConstQualified() && if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant]) return SK_LocalConstant;
return SK_LocalConstant;
if (!Type.isNull() && Type.isLocalConstQualified() && if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant]) return SK_LocalConstant;
return SK_LocalConstant;
if (!Type.isNull() && Type.isLocalConstQualified() && if (NamingStyles[SK_Constant])
NamingStyles[SK_Constant]) return SK_Constant;
return SK_Constant; }
if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember]) if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
return SK_ClassMember; return SK_ClassMember;
@ -681,8 +677,9 @@ static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures, static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
const NamedDecl *Decl, SourceRange Range, const NamedDecl *Decl, SourceRange Range,
SourceManager *SourceMgr = nullptr) { SourceManager *SourceMgr = nullptr) {
return addUsage(Failures, IdentifierNamingCheck::NamingCheckId( return addUsage(Failures,
Decl->getLocation(), Decl->getNameAsString()), IdentifierNamingCheck::NamingCheckId(Decl->getLocation(),
Decl->getNameAsString()),
Range, SourceMgr); Range, SourceMgr);
} }
@ -699,7 +696,8 @@ void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
if (!Init->isWritten() || Init->isInClassMemberInitializer()) if (!Init->isWritten() || Init->isInClassMemberInitializer())
continue; continue;
if (const auto *FD = Init->getAnyMember()) 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 // Note: delegating constructors and base class initializers are handled
// via the "typeLoc" matcher. // via the "typeLoc" matcher.
} }

View File

@ -405,6 +405,38 @@ static void static_Function() {
using ::FOO_NS::InlineNamespace::CE_function; using ::FOO_NS::InlineNamespace::CE_function;
// CHECK-FIXES: {{^}} using ::foo_ns::inline_namespace::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() #define MY_TEST_Macro(X) X()
@ -418,6 +450,27 @@ void MY_TEST_Macro(function) {}
template <typename t_t> struct a { template <typename t_t> struct a {
typename t_t::template b<> c; 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<typename t_t>
char const a<t_t>::MyConstClass_string[] = "123";
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for class constant 'MyConstClass_string'
// CHECK-FIXES: {{^}}char const a<t_t>::kMyConstClassString[] = "123";{{$}}
template <template <typename> class A> struct b { A<int> c; }; 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'
// CHECK-FIXES: {{^}}unsigned g_my_global_array[] = {1,2,3};{{$}}
unsigned const MyConstGlobal_array[] = {1,2,3};
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: invalid case style for global constant 'MyConstGlobal_array'
// CHECK-FIXES: {{^}}unsigned const MY_CONST_GLOBAL_ARRAY[] = {1,2,3};{{$}}