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 const auto *StandaloneVar =
142 Result.Nodes.getNodeAs<ValueDecl>(
"standaloneVar");
144 if (!StandaloneVar->getIdentifier())
147 const StringRef VarName = StandaloneVar->getName();
149 if (VarName.size() >= MinimumVariableNameLength ||
150 IgnoredVariableNames.match(VarName))
153 if (
isShortLived(StandaloneVar, Result.SourceManager, Result.Context,
158 << 0 << StandaloneVar << MinimumVariableNameLength;
161 const auto *BindingVar = Result.Nodes.getNodeAs<ValueDecl>(
"bindingVar");
163 if (!BindingVar->getIdentifier())
166 const StringRef VarName = BindingVar->getName();
168 if (VarName.size() >= MinimumBindingNameLength ||
169 IgnoredBindingNames.match(VarName))
172 if (
isShortLived(BindingVar, Result.SourceManager, Result.Context,
177 << 1 << BindingVar << MinimumBindingNameLength;
180 auto *ExceptionVarName = Result.Nodes.getNodeAs<ValueDecl>(
"exceptionVar");
181 if (ExceptionVarName) {
182 if (!ExceptionVarName->getIdentifier())
185 const StringRef VarName = ExceptionVarName->getName();
186 if (VarName.size() >= MinimumExceptionNameLength ||
187 IgnoredExceptionVariableNames.match(VarName))
190 if (
isShortLived(ExceptionVarName, Result.SourceManager, Result.Context,
195 << 2 << ExceptionVarName << MinimumExceptionNameLength;
198 const auto *LoopVar = Result.Nodes.getNodeAs<ValueDecl>(
"loopVar");
200 if (!LoopVar->getIdentifier())
203 const StringRef VarName = LoopVar->getName();
205 if (VarName.size() >= MinimumLoopCounterNameLength ||
206 IgnoredLoopCounterNames.match(VarName))
209 if (
isShortLived(LoopVar, Result.SourceManager, Result.Context,
214 << 3 << LoopVar << MinimumLoopCounterNameLength;
217 const auto *ParamVar = Result.Nodes.getNodeAs<ValueDecl>(
"paramVar");
219 if (!ParamVar->getIdentifier())
222 const StringRef VarName = ParamVar->getName();
224 if (VarName.size() >= MinimumParameterNameLength ||
225 IgnoredParameterNames.match(VarName))
228 if (
isShortLived(ParamVar, Result.SourceManager, Result.Context,
233 << 4 << ParamVar << MinimumParameterNameLength;
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.