Wizard created this revision.
Herald added subscribers: cfe-commits, klimek.
Repository:
rCTE Clang Tools Extra
https://reviews.llvm.org/D43640
Files:
clang-tidy/google/GlobalVariableDeclarationCheck.cpp
test/clang-tidy/google-objc-global-variable-declaration.m
Index: test/clang-tidy/google-objc-global-variable-declaration.m
===================================================================
--- test/clang-tidy/google-objc-global-variable-declaration.m
+++ test/clang-tidy/google-objc-global-variable-declaration.m
@@ -2,7 +2,7 @@
@class NSString;
static NSString* const myConstString = @"hello";
-// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable
'myConstString' must have a name which starts with 'k[A-Z]'
[google-objc-global-variable-declaration]
+// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable
'myConstString' must have an appropriate prefix or a name which starts with
'k[A-Z]' [google-objc-global-variable-declaration]
// CHECK-FIXES: static NSString* const kMyConstString = @"hello";
static NSString* MyString = @"hi";
@@ -22,14 +22,15 @@
// CHECK-FIXES: static NSString* gNoDef;
static NSString* const _notAlpha = @"NotBeginWithAlpha";
-// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable '_notAlpha'
must have a name which starts with 'k[A-Z]'
[google-objc-global-variable-declaration]
+// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable '_notAlpha'
must have an appropriate prefix or a name which starts with 'k[A-Z]'
[google-objc-global-variable-declaration]
// CHECK-FIXES: static NSString* const _notAlpha = @"NotBeginWithAlpha";
static NSString* const k_Alpha = @"SecondNotAlpha";
-// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'k_Alpha'
must have a name which starts with 'k[A-Z]'
[google-objc-global-variable-declaration]
+// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'k_Alpha'
must have an appropriate prefix or a name which starts with 'k[A-Z]'
[google-objc-global-variable-declaration]
// CHECK-FIXES: static NSString* const k_Alpha = @"SecondNotAlpha";
static NSString* const kGood = @"hello";
+static NSString* const ABCGood = @"I have a prefix";
static NSString* gMyIntGood = 0;
@implementation Foo
Index: clang-tidy/google/GlobalVariableDeclarationCheck.cpp
===================================================================
--- clang-tidy/google/GlobalVariableDeclarationCheck.cpp
+++ clang-tidy/google/GlobalVariableDeclarationCheck.cpp
@@ -24,15 +24,13 @@
namespace {
-AST_MATCHER(VarDecl, isLocalVariable) {
- return Node.isLocalVarDecl();
-}
+AST_MATCHER(VarDecl, isLocalVariable) { return Node.isLocalVarDecl(); }
FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
char FC = Decl->getName()[0];
if (!llvm::isAlpha(FC) || Decl->getName().size() == 1) {
// No fix available if first character is not alphabetical character, or it
- // is a single-character variable, since it is difficult to determine the
+ // is a single-character variable, since it is difficult to determine the
// proper fix in this case. Users should create a proper variable name by
// their own.
return FixItHint();
@@ -72,7 +70,7 @@
this);
Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
unless(isLocalVariable()),
- unless(matchesName("::k[A-Z]")))
+ unless(matchesName("::(k|[A-Z]{2,})[A-Z]")))
.bind("global_const"),
this);
}
@@ -87,8 +85,8 @@
}
if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_const")) {
diag(Decl->getLocation(),
- "const global variable '%0' must have a name which starts with "
- "'k[A-Z]'")
+ "const global variable '%0' must have an appropriate prefix or a name
"
+ "which starts with 'k[A-Z]'")
<< Decl->getName() << generateFixItHint(Decl, true);
}
}
Index: test/clang-tidy/google-objc-global-variable-declaration.m
===================================================================
--- test/clang-tidy/google-objc-global-variable-declaration.m
+++ test/clang-tidy/google-objc-global-variable-declaration.m
@@ -2,7 +2,7 @@
@class NSString;
static NSString* const myConstString = @"hello";
-// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'myConstString' must have a name which starts with 'k[A-Z]' [google-objc-global-variable-declaration]
+// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'myConstString' must have an appropriate prefix or a name which starts with 'k[A-Z]' [google-objc-global-variable-declaration]
// CHECK-FIXES: static NSString* const kMyConstString = @"hello";
static NSString* MyString = @"hi";
@@ -22,14 +22,15 @@
// CHECK-FIXES: static NSString* gNoDef;
static NSString* const _notAlpha = @"NotBeginWithAlpha";
-// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable '_notAlpha' must have a name which starts with 'k[A-Z]' [google-objc-global-variable-declaration]
+// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable '_notAlpha' must have an appropriate prefix or a name which starts with 'k[A-Z]' [google-objc-global-variable-declaration]
// CHECK-FIXES: static NSString* const _notAlpha = @"NotBeginWithAlpha";
static NSString* const k_Alpha = @"SecondNotAlpha";
-// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'k_Alpha' must have a name which starts with 'k[A-Z]' [google-objc-global-variable-declaration]
+// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'k_Alpha' must have an appropriate prefix or a name which starts with 'k[A-Z]' [google-objc-global-variable-declaration]
// CHECK-FIXES: static NSString* const k_Alpha = @"SecondNotAlpha";
static NSString* const kGood = @"hello";
+static NSString* const ABCGood = @"I have a prefix";
static NSString* gMyIntGood = 0;
@implementation Foo
Index: clang-tidy/google/GlobalVariableDeclarationCheck.cpp
===================================================================
--- clang-tidy/google/GlobalVariableDeclarationCheck.cpp
+++ clang-tidy/google/GlobalVariableDeclarationCheck.cpp
@@ -24,15 +24,13 @@
namespace {
-AST_MATCHER(VarDecl, isLocalVariable) {
- return Node.isLocalVarDecl();
-}
+AST_MATCHER(VarDecl, isLocalVariable) { return Node.isLocalVarDecl(); }
FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
char FC = Decl->getName()[0];
if (!llvm::isAlpha(FC) || Decl->getName().size() == 1) {
// No fix available if first character is not alphabetical character, or it
- // is a single-character variable, since it is difficult to determine the
+ // is a single-character variable, since it is difficult to determine the
// proper fix in this case. Users should create a proper variable name by
// their own.
return FixItHint();
@@ -72,7 +70,7 @@
this);
Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
unless(isLocalVariable()),
- unless(matchesName("::k[A-Z]")))
+ unless(matchesName("::(k|[A-Z]{2,})[A-Z]")))
.bind("global_const"),
this);
}
@@ -87,8 +85,8 @@
}
if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_const")) {
diag(Decl->getLocation(),
- "const global variable '%0' must have a name which starts with "
- "'k[A-Z]'")
+ "const global variable '%0' must have an appropriate prefix or a name "
+ "which starts with 'k[A-Z]'")
<< Decl->getName() << generateFixItHint(Decl, true);
}
}
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits