diff --git a/clang-tools-extra/clang-tidy/google/CMakeLists.txt b/clang-tools-extra/clang-tidy/google/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/google/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/google/CMakeLists.txt @@ -14,6 +14,7 @@ IntegerTypesCheck.cpp NonConstReferences.cpp OverloadedUnaryAndCheck.cpp + TestCheck.cpp TodoCommentCheck.cpp UnnamedNamespaceInHeaderCheck.cpp UsingNamespaceDirectiveCheck.cpp diff --git a/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp b/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp --- a/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp @@ -23,34 +23,40 @@ namespace { -AST_MATCHER(VarDecl, isLocalVariable) { - return Node.isLocalVarDecl(); -} +AST_MATCHER(VarDecl, isLocalVariable) { return Node.isLocalVarDecl(); } + +FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst, bool IsExtern) { + if (IsConst && IsExtern) { + // No fix available if it is a extern global constant, since it is difficult + // to determine the proper fix in this case. + return FixItHint(); + } -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(); } char SC = Decl->getName()[1]; if ((FC == 'k' || FC == 'g') && !llvm::isAlpha(SC)) { - // No fix available if the prefix is correct but the second character is not - // alphabetical, since it is difficult to determine the proper fix in this - // case. + // No fix available if the prefix is correct but the second character is + // not alphabetical, since it is difficult to determine the proper fix in + // this case. return FixItHint(); } + auto NewName = (IsConst ? "k" : "g") + llvm::StringRef(std::string(1, FC)).upper() + Decl->getName().substr(1).str(); + return FixItHint::CreateReplacement( CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())), llvm::StringRef(NewName)); } -} // namespace +} // namespace void GlobalVariableDeclarationCheck::registerMatchers(MatchFinder *Finder) { // The relevant Style Guide rule only applies to Objective-C. @@ -71,7 +77,7 @@ this); Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()), unless(isLocalVariable()), - unless(matchesName("::(k[A-Z]|[A-Z]{2,})"))) + unless(matchesName("::(k[A-Z])|([A-Z][A-Z0-9])"))) .bind("global_const"), this); } @@ -84,7 +90,8 @@ diag(Decl->getLocation(), "non-const global variable '%0' must have a name which starts with " "'g[A-Z]'") - << Decl->getName() << generateFixItHint(Decl, false); + << Decl->getName() + << generateFixItHint(Decl, false, Decl->hasExternalStorage()); } if (const auto *Decl = Result.Nodes.getNodeAs("global_const")) { if (Decl->isStaticDataMember()) @@ -92,11 +99,12 @@ diag(Decl->getLocation(), "const global variable '%0' must have a name which starts with " "an appropriate prefix") - << Decl->getName() << generateFixItHint(Decl, true); + << Decl->getName() + << generateFixItHint(Decl, true, Decl->hasExternalStorage()); } } -} // namespace objc -} // namespace google -} // namespace tidy -} // namespace clang +} // namespace objc +} // namespace google +} // namespace tidy +} // namespace clang 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 --- 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 @@ -1,49 +1,58 @@ // RUN: %check_clang_tidy %s google-objc-global-variable-declaration %t @class NSString; -static NSString* const myConstString = @"hello"; + +static NSString *const myConstString = @"hello"; // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'myConstString' must have a name which starts with an appropriate prefix [google-objc-global-variable-declaration] -// CHECK-FIXES: static NSString* const kMyConstString = @"hello"; +// CHECK-FIXES: static NSString *const kMyConstString = @"hello"; + +extern NSString *const GlobalConstant = @"hey"; +// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'GlobalConstant' must have a name which starts with an appropriate prefix [google-objc-global-variable-declaration] -static NSString* MyString = @"hi"; +static NSString *MyString = @"hi"; // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: non-const global variable 'MyString' must have a name which starts with 'g[A-Z]' [google-objc-global-variable-declaration] -// CHECK-FIXES: static NSString* gMyString = @"hi"; +// CHECK-FIXES: static NSString *gMyString = @"hi"; -NSString* globalString = @"test"; +NSString *globalString = @"test"; // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: non-const global variable 'globalString' must have a name which starts with 'g[A-Z]' [google-objc-global-variable-declaration] -// CHECK-FIXES: NSString* gGlobalString = @"test"; +// CHECK-FIXES: NSString *gGlobalString = @"test"; -static NSString* a = @"too simple"; +static NSString *a = @"too simple"; // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: non-const global variable 'a' must have a name which starts with 'g[A-Z]' [google-objc-global-variable-declaration] -// CHECK-FIXES: static NSString* a = @"too simple"; +// CHECK-FIXES: static NSString *a = @"too simple"; -static NSString* noDef; +static NSString *noDef; // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: non-const global variable 'noDef' must have a name which starts with 'g[A-Z]' [google-objc-global-variable-declaration] -// CHECK-FIXES: static NSString* gNoDef; +// CHECK-FIXES: static NSString *gNoDef; -static NSString* const _notAlpha = @"NotBeginWithAlpha"; +static NSString *const _notAlpha = @"NotBeginWithAlpha"; // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable '_notAlpha' must have a name which starts with an appropriate prefix [google-objc-global-variable-declaration] -// CHECK-FIXES: static NSString* const _notAlpha = @"NotBeginWithAlpha"; -static NSString* const k_Alpha = @"SecondNotAlpha"; +static NSString *const notCap = @"NotBeginWithCap"; +// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'notCap' must have a name which starts with an appropriate prefix [google-objc-global-variable-declaration] + +static NSString *const k_Alpha = @"SecondNotAlpha"; // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'k_Alpha' must have a name which starts with an appropriate prefix [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; +static NSString *const SecondNotCap = @"SecondNotCapOrNumber"; +// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: const global variable 'SecondNotCap' must have a name which starts with an appropriate prefix [google-objc-global-variable-declaration] + +static NSString *const kGood = @"hello"; +static NSString *const XYGood = @"hello"; +static NSString *const X1Good = @"hello"; +static NSString *gMyIntGood = 0; -extern NSString* const GTLServiceErrorDomain; +extern NSString *const GTLServiceErrorDomain; enum GTLServiceError { GTLServiceErrorQueryResultMissing = -3000, - GTLServiceErrorWaitTimedOut = -3001, + GTLServiceErrorWaitTimedOut = -3001, }; @implementation Foo - (void)f { - int x = 0; - static int bar; - static const int baz = 42; + int x = 0; + static int bar; + static const int baz = 42; } @end