llvm-project/clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCh...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.7 KiB
C++
Raw Normal View History

//===--- GlobalVariableDeclarationCheck.cpp - clang-tidy-------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "GlobalVariableDeclarationCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include <string>
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace objc {
namespace {
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
// 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.
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
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(isLocalVariable()), unless(matchesName("::g[A-Z]")))
.bind("global_var"),
this);
Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
unless(isLocalVariable()),
unless(matchesName("::(k[A-Z])|([A-Z][A-Z0-9])")))
.bind("global_const"),
this);
}
void GlobalVariableDeclarationCheck::check(
const MatchFinder::MatchResult &Result) {
if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_var")) {
if (Decl->isStaticDataMember())
return;
diag(Decl->getLocation(),
"non-const global variable '%0' must have a name which starts with "
"'g[A-Z]'")
<< Decl->getName() << generateFixItHint(Decl, false);
}
if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_const")) {
if (Decl->isStaticDataMember())
return;
diag(Decl->getLocation(),
"const global variable '%0' must have a name which starts with "
[clang-tidy/google] Improve the Objective-C global variable declaration check 🔧 Summary: The current Objective-C global variable declaration check restricts naming that is permitted by the Google Objective-C style guide. The Objective-C style guide states the following: "Global and file scope constants should have an appropriate prefix. [...] Constants may use a lowercase k prefix when appropriate" http://google.github.io/styleguide/objcguide#constants This change fixes the check to allow two or more capital letters as an appropriate prefix. This change intentionally avoids making a decision regarding whether to flag constants that use a two letter prefix (two letter prefixes are reserved by Apple¹ but many projects seem to violate this guideline). This change eliminates an important category of false positives (constants prefixed with '[A-Z]{2,}') at the cost of introducing a less important category of false negatives (constants prefixed with only '[A-Z]'). The false positives are observed in standard recommended code while the false negatives occur in non-standard unrecommended code. The number of eliminated false positives is expected to be significantly larger than the number of exposed false negatives. ❧ (1) "Two-letter prefixes like these are reserved by Apple for use in framework classes." https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html Reviewers: aaron.ballman, Wizard, hokein, benhamilton Reviewed By: aaron.ballman, Wizard Subscribers: aaron.ballman, cfe-commits Differential Revision: https://reviews.llvm.org/D43581 llvm-svn: 326046
2018-02-25 12:11:26 +08:00
"an appropriate prefix")
<< Decl->getName() << generateFixItHint(Decl, true);
}
}
} // namespace objc
} // namespace google
} // namespace tidy
} // namespace clang