11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
29 "%select{variable|exception variable|loop variable|"
30 "parameter}0 name %1 is too short, expected at least %2 characters";
35 MinimumVariableNameLength(Options.get(
"MinimumVariableNameLength",
37 MinimumLoopCounterNameLength(Options.get(
39 MinimumExceptionNameLength(Options.get(
41 MinimumParameterNameLength(Options.get(
43 IgnoredVariableNamesInput(
45 IgnoredVariableNames(IgnoredVariableNamesInput),
46 IgnoredLoopCounterNamesInput(Options.get(
"IgnoredLoopCounterNames",
48 IgnoredLoopCounterNames(IgnoredLoopCounterNamesInput),
49 IgnoredExceptionVariableNamesInput(
50 Options.get(
"IgnoredExceptionVariableNames",
52 IgnoredExceptionVariableNames(IgnoredExceptionVariableNamesInput),
53 IgnoredParameterNamesInput(
55 IgnoredParameterNames(IgnoredParameterNamesInput) {}
58 Options.
store(Opts,
"MinimumVariableNameLength", MinimumVariableNameLength);
60 MinimumLoopCounterNameLength);
61 Options.
store(Opts,
"MinimumExceptionNameLength", MinimumExceptionNameLength);
62 Options.
store(Opts,
"MinimumParameterNameLength", MinimumParameterNameLength);
63 Options.
store(Opts,
"IgnoredLoopCounterNames", IgnoredLoopCounterNamesInput);
64 Options.
store(Opts,
"IgnoredVariableNames", IgnoredVariableNamesInput);
66 IgnoredExceptionVariableNamesInput);
67 Options.
store(Opts,
"IgnoredParameterNames", IgnoredParameterNamesInput);
71 if (MinimumLoopCounterNameLength > 1)
73 forStmt(hasLoopInit(declStmt(forEach(varDecl().bind(
"loopVar"))))),
76 if (MinimumExceptionNameLength > 1)
77 Finder->addMatcher(varDecl(hasParent(cxxCatchStmt())).bind(
"exceptionVar"),
80 if (MinimumParameterNameLength > 1)
81 Finder->addMatcher(parmVarDecl().bind(
"paramVar"),
this);
83 if (MinimumVariableNameLength > 1)
85 varDecl(unless(anyOf(hasParent(declStmt(hasParent(forStmt()))),
86 hasParent(cxxCatchStmt()), parmVarDecl())))
87 .bind(
"standaloneVar"),
92 const auto *StandaloneVar = Result.Nodes.getNodeAs<VarDecl>(
"standaloneVar");
94 if (!StandaloneVar->getIdentifier())
97 StringRef VarName = StandaloneVar->getName();
99 if (VarName.size() >= MinimumVariableNameLength ||
100 IgnoredVariableNames.match(VarName))
104 << 0 << StandaloneVar << MinimumVariableNameLength;
107 auto *ExceptionVarName = Result.Nodes.getNodeAs<VarDecl>(
"exceptionVar");
108 if (ExceptionVarName) {
109 if (!ExceptionVarName->getIdentifier())
112 StringRef VarName = ExceptionVarName->getName();
113 if (VarName.size() >= MinimumExceptionNameLength ||
114 IgnoredExceptionVariableNames.match(VarName))
118 << 1 << ExceptionVarName << MinimumExceptionNameLength;
121 const auto *LoopVar = Result.Nodes.getNodeAs<VarDecl>(
"loopVar");
123 if (!LoopVar->getIdentifier())
126 StringRef VarName = LoopVar->getName();
128 if (VarName.size() >= MinimumLoopCounterNameLength ||
129 IgnoredLoopCounterNames.match(VarName))
133 << 2 << LoopVar << MinimumLoopCounterNameLength;
136 const auto *ParamVar = Result.Nodes.getNodeAs<VarDecl>(
"paramVar");
138 if (!ParamVar->getIdentifier())
141 StringRef VarName = ParamVar->getName();
143 if (VarName.size() >= MinimumParameterNameLength ||
144 IgnoredParameterNames.match(VarName))
148 << 3 << ParamVar << MinimumParameterNameLength;
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
IdentifierLengthCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
const char DefaultIgnoredExceptionVariableNames[]
const char DefaultIgnoredLoopCounterNames[]
const unsigned DefaultMinimumParameterNameLength
const unsigned DefaultMinimumExceptionNameLength
const unsigned DefaultMinimumLoopCounterNameLength
const char DefaultIgnoredVariableNames[]
const unsigned DefaultMinimumVariableNameLength
const char ErrorMessage[]
const char DefaultIgnoredParameterNames[]
llvm::StringMap< ClangTidyValue > OptionMap