[PATCH] D43640: add support for constants with appropriate prefix. Start with 'k' is not the only option. This is according to http://google.github.io/styleguide/objcguide.html#constants

Yan Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 22 11:10:37 PST 2018


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);
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43640.135482.patch
Type: text/x-patch
Size: 3758 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180222/7127e39a/attachment.bin>


More information about the cfe-commits mailing list