10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/StringExtras.h"
13#include "llvm/ADT/StringRef.h"
25FixItHint generateFixItHint(
const VarDecl *
Decl,
bool IsConst) {
26 if (IsConst && (
Decl->getStorageClass() != SC_Static)) {
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)) {
48 auto NewName = (IsConst ?
"k" :
"g") +
49 llvm::StringRef(std::string(1, FC)).upper() +
50 Decl->getName().substr(1).str();
52 return FixItHint::CreateReplacement(
53 CharSourceRange::getTokenRange(SourceRange(
Decl->getLocation())),
54 llvm::StringRef(NewName));
67 varDecl(hasGlobalStorage(), unless(hasType(isConstQualified())),
68 unless(isLocalVariable()), unless(matchesName(
"::g[A-Z]")))
71 Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
72 unless(isLocalVariable()),
73 unless(matchesName(
"::(k[A-Z])|([A-Z][A-Z0-9])")))
74 .bind(
"global_const"),
79 const MatchFinder::MatchResult &Result) {
80 if (
const auto *
Decl = Result.Nodes.getNodeAs<VarDecl>(
"global_var")) {
81 if (
Decl->isStaticDataMember())
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 if (
Decl->isStaticDataMember())
92 "const global variable '%0' must have a name which starts with "
93 "an appropriate prefix")
94 <<
Decl->getName() << generateFixItHint(
Decl,
true);
const FunctionDecl * Decl
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER(Expr, isMacroID)