From 9ac757bf09a35d87eee7e0d12695e4f87bb8eaac Mon Sep 17 00:00:00 2001 From: Stephane Moore Date: Wed, 29 May 2019 02:23:32 +0000 Subject: [PATCH] =?UTF-8?q?Rollback=20"Revise=20the=20google-objc-global-v?= =?UTF-8?q?ariable-declaration=20check=20to=20match=20the=20style=20guide.?= =?UTF-8?q?"=20=F0=9F=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The change introduced new test failures. Phabricator URL of original commit: https://reviews.llvm.org/rG12e3726fadb0b2a4d8aeed0a2817b5159f9d029d llvm-svn: 361914 --- .../google/GlobalVariableDeclarationCheck.cpp | 22 +++++++------------ .../google-objc-global-variable-declaration.m | 20 +++-------------- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp b/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp index 30ab04c08c00..ce833906dd5c 100644 --- a/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp @@ -23,35 +23,29 @@ namespace objc { namespace { -AST_MATCHER(VarDecl, isLocalVariable) { return Node.isLocalVarDecl(); } +AST_MATCHER(VarDecl, isLocalVariable) { + return Node.isLocalVarDecl(); +} FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) { - if (IsConst && (Decl->getStorageClass() != SC_Static)) { - // No fix available if it is not a static constant, since it is difficult - // to determine the proper fix in this case. - return FixItHint(); - } - 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)); @@ -77,7 +71,7 @@ void GlobalVariableDeclarationCheck::registerMatchers(MatchFinder *Finder) { this); Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()), unless(isLocalVariable()), - unless(matchesName("::(k[A-Z])|([A-Z][A-Z0-9])"))) + unless(matchesName("::(k[A-Z]|[A-Z]{2,})"))) .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 32af3533f3f1..346ddeca7db9 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 @@ -1,14 +1,10 @@ // RUN: %check_clang_tidy %s google-objc-global-variable-declaration %t @class NSString; - 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"; -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"; // 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"; @@ -29,23 +25,13 @@ 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 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] -// CHECK-FIXES: static NSString* const kNotCap = @"NotBeginWithCap"; - 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 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] -// CHECK-FIXES: static NSString* const kSecondNotCap = @"SecondNotCapOrNumber"; - static NSString* const kGood = @"hello"; static NSString* const XYGood = @"hello"; -static NSString* const X1Good = @"hello"; static NSString* gMyIntGood = 0; -extern NSString* Y2Good; extern NSString* const GTLServiceErrorDomain; @@ -56,8 +42,8 @@ enum GTLServiceError { @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