11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "llvm/ADT/StringExtras.h" 14 #include "llvm/ADT/StringRef.h" 28 return Node.isLocalVarDecl();
31 FixItHint generateFixItHint(
const VarDecl *Decl,
bool IsConst) {
32 char FC = Decl->getName()[0];
33 if (!llvm::isAlpha(FC) || Decl->getName().size() == 1) {
40 char SC = Decl->getName()[1];
41 if ((FC ==
'k' || FC ==
'g') && !llvm::isAlpha(SC)) {
47 auto NewName = (IsConst ?
"k" :
"g") +
48 llvm::StringRef(std::string(1, FC)).upper() +
49 Decl->getName().substr(1).str();
50 return FixItHint::CreateReplacement(
51 CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())),
52 llvm::StringRef(NewName));
56 void GlobalVariableDeclarationCheck::registerMatchers(MatchFinder *Finder) {
58 if (!getLangOpts().ObjC)
69 varDecl(hasGlobalStorage(), unless(hasType(isConstQualified())),
70 unless(isLocalVariable()), unless(matchesName(
"::g[A-Z]")))
73 Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
74 unless(isLocalVariable()),
75 unless(matchesName(
"::(k[A-Z]|[A-Z]{2,})")))
76 .bind(
"global_const"),
80 void GlobalVariableDeclarationCheck::check(
81 const MatchFinder::MatchResult &
Result) {
82 if (
const auto *Decl = Result.Nodes.getNodeAs<VarDecl>(
"global_var")) {
83 diag(Decl->getLocation(),
84 "non-const global variable '%0' must have a name which starts with " 86 << Decl->getName() << generateFixItHint(Decl,
false);
88 if (
const auto *Decl = Result.Nodes.getNodeAs<VarDecl>(
"global_const")) {
89 diag(Decl->getLocation(),
90 "const global variable '%0' must have a name which starts with " 91 "an appropriate prefix")
92 << Decl->getName() << generateFixItHint(Decl,
true);
AST_MATCHER(BinaryOperator, isAssignmentOperator)
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//