stephanemoore updated this revision to Diff 135595. stephanemoore marked 2 inline comments as done. stephanemoore retitled this revision from "[clang-tidy/google] Fix the Objective-C global variable declaration check 🔧" to "[clang-tidy/google] Improve the Objective-C global variable declaration check 🔧". stephanemoore edited the summary of this revision. stephanemoore added a comment.
I had to make two more fixes to get the tests working: • Updated the CHECK-MESSAGES in `test/clang-tidy/google-objc-global-variable-declaration.m` to the revised diagnostic message. • Removed NS_ENUM and NSInteger from the additions to `test/clang-tidy/google-objc-global-variable-declaration.m` because their declarations were missing and I assumed that replication of the Foundation macro and type alias was not meaningfully important to this test case. I verified the changes with the following commands: $ ${LLVM_ROOT}/tools/clang/tools/extra/test/clang-tidy/check_clang_tidy.py ${LLVM_ROOT}/tools/clang/tools/extra/test/clang-tidy/google-objc-global-variable-declaration.m google-objc-global-variable-declaration /tmp/test && make check-all Is that satisfactory verification of these changes? Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D43581 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 a name which starts with an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})' [google-objc-global-variable-declaration] // CHECK-FIXES: static NSString* const kMyConstString = @"hello"; static NSString* MyString = @"hi"; @@ -22,16 +22,24 @@ // 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 a name which starts with an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})' [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 a name which starts with an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})' [google-objc-global-variable-declaration] // CHECK-FIXES: static NSString* const k_Alpha = @"SecondNotAlpha"; static NSString* const kGood = @"hello"; +static NSString* const XYGood = @"hello"; static NSString* gMyIntGood = 0; +extern NSString* const GTLServiceErrorDomain; + +enum GTLServiceError { + GTLServiceErrorQueryResultMissing = -3000, + GTLServiceErrorWaitTimedOut = -3001, +}; + @implementation Foo - (void)f { int x = 0; Index: clang-tidy/google/GlobalVariableDeclarationCheck.cpp =================================================================== --- clang-tidy/google/GlobalVariableDeclarationCheck.cpp +++ clang-tidy/google/GlobalVariableDeclarationCheck.cpp @@ -72,7 +72,7 @@ this); Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()), unless(isLocalVariable()), - unless(matchesName("::k[A-Z]"))) + unless(matchesName("::(k[A-Z]|[A-Z]{2,})"))) .bind("global_const"), this); } @@ -88,7 +88,7 @@ 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]'") + "an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})'") << 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 a name which starts with an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})' [google-objc-global-variable-declaration] // CHECK-FIXES: static NSString* const kMyConstString = @"hello"; static NSString* MyString = @"hi"; @@ -22,16 +22,24 @@ // 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 a name which starts with an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})' [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 a name which starts with an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})' [google-objc-global-variable-declaration] // CHECK-FIXES: static NSString* const k_Alpha = @"SecondNotAlpha"; static NSString* const kGood = @"hello"; +static NSString* const XYGood = @"hello"; static NSString* gMyIntGood = 0; +extern NSString* const GTLServiceErrorDomain; + +enum GTLServiceError { + GTLServiceErrorQueryResultMissing = -3000, + GTLServiceErrorWaitTimedOut = -3001, +}; + @implementation Foo - (void)f { int x = 0; Index: clang-tidy/google/GlobalVariableDeclarationCheck.cpp =================================================================== --- clang-tidy/google/GlobalVariableDeclarationCheck.cpp +++ clang-tidy/google/GlobalVariableDeclarationCheck.cpp @@ -72,7 +72,7 @@ this); Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()), unless(isLocalVariable()), - unless(matchesName("::k[A-Z]"))) + unless(matchesName("::(k[A-Z]|[A-Z]{2,})"))) .bind("global_const"), this); } @@ -88,7 +88,7 @@ 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]'") + "an appropriate prefix matching '(k[A-Z]|[A-Z]{2,})'") << Decl->getName() << generateFixItHint(Decl, true); } }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits