[clang-tools-extra] r321914 - [clang-tidy] Function-scoped static variables should not trigger google-objc-global-variable-declaration

Ben Hamilton via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 5 15:26:07 PST 2018


Author: benhamilton
Date: Fri Jan  5 15:26:06 2018
New Revision: 321914

URL: http://llvm.org/viewvc/llvm-project?rev=321914&view=rev
Log:
[clang-tidy] Function-scoped static variables should not trigger google-objc-global-variable-declaration

Summary:
google-objc-global-variable-declaration currently triggers on
valid code like:

  - (void)foo {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{ /* ... */ });
  }

The Google Objective-C style guide says:

http://google.github.io/styleguide/objcguide.html#common-variable-names

> File scope or global variables (as opposed to constants) declared
> outside the scope of a method or function should be rare, and should
> have the prefix g.

which is meant to insinuate that static variables inside a method or
function don't need a special name.

Test Plan: `make -j12 check-clang-tools`

Reviewers: Wizard, hokein, klimek

Reviewed By: Wizard

Subscribers: xazax.hun, cfe-commits

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

Modified:
    clang-tools-extra/trunk/clang-tidy/google/GlobalVariableDeclarationCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/google-objc-global-variable-declaration.m

Modified: clang-tools-extra/trunk/clang-tidy/google/GlobalVariableDeclarationCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/GlobalVariableDeclarationCheck.cpp?rev=321914&r1=321913&r2=321914&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/GlobalVariableDeclarationCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/GlobalVariableDeclarationCheck.cpp Fri Jan  5 15:26:06 2018
@@ -24,6 +24,10 @@ namespace objc {
 
 namespace {
 
+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) {
@@ -57,12 +61,17 @@ void GlobalVariableDeclarationCheck::reg
   // need to add two matchers since we need to bind different ids to distinguish
   // constants and variables. Since bind() can only be called on node matchers,
   // we cannot make it in one matcher.
+  //
+  // Note that hasGlobalStorage() matches static variables declared locally
+  // inside a function or method, so we need to exclude those with
+  // isLocalVariable().
   Finder->addMatcher(
       varDecl(hasGlobalStorage(), unless(hasType(isConstQualified())),
-              unless(matchesName("::g[A-Z]")))
+              unless(isLocalVariable()), unless(matchesName("::g[A-Z]")))
           .bind("global_var"),
       this);
   Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
+                             unless(isLocalVariable()),
                              unless(matchesName("::k[A-Z]")))
                          .bind("global_const"),
                      this);

Modified: clang-tools-extra/trunk/test/clang-tidy/google-objc-global-variable-declaration.m
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-objc-global-variable-declaration.m?rev=321914&r1=321913&r2=321914&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-objc-global-variable-declaration.m (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-objc-global-variable-declaration.m Fri Jan  5 15:26:06 2018
@@ -30,12 +30,12 @@ static NSString* const k_Alpha = @"Secon
 // CHECK-FIXES: static NSString* const k_Alpha = @"SecondNotAlpha";
 
 static NSString* const kGood = @"hello";
-// CHECK-MESSAGES-NOT: :[[@LINE-1]]:24: warning: const global variable 'kGood' must have a name which starts with 'k[A-Z]' [google-objc-global-variable-declaration]
 static NSString* gMyIntGood = 0;
-// CHECK-MESSAGES-NOT: :[[@LINE-1]]:18: warning: non-const global variable 'gMyIntGood' must have a name which starts with 'g[A-Z]' [google-objc-global-variable-declaration]
+
 @implementation Foo
 - (void)f {
     int x = 0;
-// CHECK-MESSAGES-NOT: :[[@LINE-1]]:9: warning: non-const global variable 'gMyIntGood' must have a name which starts with 'g[A-Z]' [google-objc-global-variable-declaration]
+    static int bar;
+    static const int baz = 42;
 }
 @end




More information about the cfe-commits mailing list