36 MinimumVariableNameLength(Options.get(
"MinimumVariableNameLength",
38 MinimumBindingNameLength(Options.get(
"MinimumBindingNameLength",
40 MinimumLoopCounterNameLength(Options.get(
42 MinimumExceptionNameLength(Options.get(
44 MinimumParameterNameLength(Options.get(
46 IgnoredVariableNamesInput(
48 IgnoredVariableNames(IgnoredVariableNamesInput),
49 IgnoredBindingNamesInput(
51 IgnoredBindingNames(IgnoredBindingNamesInput),
52 IgnoredLoopCounterNamesInput(Options.get(
"IgnoredLoopCounterNames",
54 IgnoredLoopCounterNames(IgnoredLoopCounterNamesInput),
55 IgnoredExceptionVariableNamesInput(
56 Options.get(
"IgnoredExceptionVariableNames",
58 IgnoredExceptionVariableNames(IgnoredExceptionVariableNamesInput),
59 IgnoredParameterNamesInput(
61 IgnoredParameterNames(IgnoredParameterNamesInput),
66 Options.store(Opts,
"MinimumVariableNameLength", MinimumVariableNameLength);
67 Options.store(Opts,
"MinimumBindingNameLength", MinimumBindingNameLength);
68 Options.store(Opts,
"MinimumLoopCounterNameLength",
69 MinimumLoopCounterNameLength);
70 Options.store(Opts,
"MinimumExceptionNameLength", MinimumExceptionNameLength);
71 Options.store(Opts,
"MinimumParameterNameLength", MinimumParameterNameLength);
72 Options.store(Opts,
"IgnoredVariableNames", IgnoredVariableNamesInput);
73 Options.store(Opts,
"IgnoredBindingNames", IgnoredBindingNamesInput);
74 Options.store(Opts,
"IgnoredLoopCounterNames", IgnoredLoopCounterNamesInput);
75 Options.store(Opts,
"IgnoredExceptionVariableNames",
76 IgnoredExceptionVariableNamesInput);
77 Options.store(Opts,
"IgnoredParameterNames", IgnoredParameterNamesInput);
78 Options.store(Opts,
"LineCountThreshold", LineCountThreshold);
82 if (MinimumLoopCounterNameLength > 1)
84 forStmt(hasLoopInit(declStmt(forEach(varDecl().bind(
"loopVar"))))),
87 if (MinimumExceptionNameLength > 1)
88 Finder->addMatcher(varDecl(hasParent(cxxCatchStmt())).bind(
"exceptionVar"),
91 if (MinimumParameterNameLength > 1)
92 Finder->addMatcher(parmVarDecl().bind(
"paramVar"),
this);
94 if (MinimumBindingNameLength > 1)
95 Finder->addMatcher(bindingDecl().bind(
"bindingVar"),
this);
97 if (MinimumVariableNameLength > 1)
99 varDecl(unless(anyOf(hasParent(declStmt(hasParent(forStmt()))),
100 hasParent(cxxCatchStmt()), parmVarDecl())))
101 .bind(
"standaloneVar"),
106 const SourceManager *SrcMgr,
108 const auto *ParentScope = llvm::dyn_cast<FunctionDecl>(Var->getDeclContext());
109 if (ParentScope ==
nullptr)
116 llvm::map_range(AllRefs, [&](
const DeclRefExpr *RefToVar) ->
unsigned {
117 return SrcMgr->getSpellingLineNumber(RefToVar->getLocation());
120 const unsigned DeclLine = SrcMgr->getSpellingLineNumber(Var->getLocation());
121 const unsigned LastUseLine =
122 AllRefLines.empty() ? DeclLine
123 : std::max(DeclLine, *llvm::max_element(AllRefLines));
125 return LastUseLine - DeclLine + 1;
141 auto WarnIfTooShort = [&](
const ValueDecl *Var,
unsigned MinNameLength,
142 const llvm::Regex &IgnoredNames,
unsigned VarKind) {
143 if (!Var->getIdentifier())
146 const StringRef VarName = Var->getName();
147 if (VarName.size() >= MinNameLength || IgnoredNames.match(VarName))
150 if (
isShortLived(Var, Result.SourceManager, Result.Context,
154 diag(Var->getLocation(),
ErrorMessage) << VarKind << Var << MinNameLength;
157 if (
const auto *StandaloneVar =
158 Result.Nodes.getNodeAs<ValueDecl>(
"standaloneVar")) {
159 WarnIfTooShort(StandaloneVar, MinimumVariableNameLength,
160 IgnoredVariableNames, 0);
164 if (
const auto *BindingVar =
165 Result.Nodes.getNodeAs<ValueDecl>(
"bindingVar")) {
166 WarnIfTooShort(BindingVar, MinimumBindingNameLength, IgnoredBindingNames,
171 if (
const auto *ExceptionVar =
172 Result.Nodes.getNodeAs<ValueDecl>(
"exceptionVar")) {
173 WarnIfTooShort(ExceptionVar, MinimumExceptionNameLength,
174 IgnoredExceptionVariableNames, 2);
178 if (
const auto *LoopVar = Result.Nodes.getNodeAs<ValueDecl>(
"loopVar")) {
179 WarnIfTooShort(LoopVar, MinimumLoopCounterNameLength,
180 IgnoredLoopCounterNames, 3);
184 if (
const auto *ParamVar = Result.Nodes.getNodeAs<ValueDecl>(
"paramVar")) {
185 WarnIfTooShort(ParamVar, MinimumParameterNameLength, IgnoredParameterNames,
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.