diff options
-rw-r--r-- | clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp | 11 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/google-objc-global-variable-declaration.m | 6 |
2 files changed, 13 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp b/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp index a278be14958..7507eae8b6e 100644 --- a/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp @@ -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::registerMatchers(MatchFinder *Finder) { // 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); diff --git a/clang-tools-extra/test/clang-tidy/google-objc-global-variable-declaration.m b/clang-tools-extra/test/clang-tidy/google-objc-global-variable-declaration.m index cbe5017bb25..e2316fab4f9 100644 --- a/clang-tools-extra/test/clang-tidy/google-objc-global-variable-declaration.m +++ b/clang-tools-extra/test/clang-tidy/google-objc-global-variable-declaration.m @@ -30,12 +30,12 @@ static NSString* const k_Alpha = @"SecondNotAlpha"; // 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 |