clang-tools 20.0.0git
IdentifierLengthCheck.cpp
Go to the documentation of this file.
1//===--- IdentifierLengthCheck.cpp - clang-tidy
2//-----------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14
15using namespace clang::ast_matchers;
16
18
23const char DefaultIgnoredLoopCounterNames[] = "^[ijk_]$";
26const char DefaultIgnoredParameterNames[] = "^[n]$";
27
28const char ErrorMessage[] =
29 "%select{variable|exception variable|loop variable|"
30 "parameter}0 name %1 is too short, expected at least %2 characters";
31
33 ClangTidyContext *Context)
34 : ClangTidyCheck(Name, Context),
35 MinimumVariableNameLength(Options.get("MinimumVariableNameLength",
37 MinimumLoopCounterNameLength(Options.get(
38 "MinimumLoopCounterNameLength", DefaultMinimumLoopCounterNameLength)),
39 MinimumExceptionNameLength(Options.get(
40 "MinimumExceptionNameLength", DefaultMinimumExceptionNameLength)),
41 MinimumParameterNameLength(Options.get(
42 "MinimumParameterNameLength", DefaultMinimumParameterNameLength)),
43 IgnoredVariableNamesInput(
44 Options.get("IgnoredVariableNames", DefaultIgnoredVariableNames)),
45 IgnoredVariableNames(IgnoredVariableNamesInput),
46 IgnoredLoopCounterNamesInput(Options.get("IgnoredLoopCounterNames",
48 IgnoredLoopCounterNames(IgnoredLoopCounterNamesInput),
49 IgnoredExceptionVariableNamesInput(
50 Options.get("IgnoredExceptionVariableNames",
52 IgnoredExceptionVariableNames(IgnoredExceptionVariableNamesInput),
53 IgnoredParameterNamesInput(
54 Options.get("IgnoredParameterNames", DefaultIgnoredParameterNames)),
55 IgnoredParameterNames(IgnoredParameterNamesInput) {}
56
58 Options.store(Opts, "MinimumVariableNameLength", MinimumVariableNameLength);
59 Options.store(Opts, "MinimumLoopCounterNameLength",
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);
65 Options.store(Opts, "IgnoredExceptionVariableNames",
66 IgnoredExceptionVariableNamesInput);
67 Options.store(Opts, "IgnoredParameterNames", IgnoredParameterNamesInput);
68}
69
70void IdentifierLengthCheck::registerMatchers(MatchFinder *Finder) {
71 if (MinimumLoopCounterNameLength > 1)
72 Finder->addMatcher(
73 forStmt(hasLoopInit(declStmt(forEach(varDecl().bind("loopVar"))))),
74 this);
75
76 if (MinimumExceptionNameLength > 1)
77 Finder->addMatcher(varDecl(hasParent(cxxCatchStmt())).bind("exceptionVar"),
78 this);
79
80 if (MinimumParameterNameLength > 1)
81 Finder->addMatcher(parmVarDecl().bind("paramVar"), this);
82
83 if (MinimumVariableNameLength > 1)
84 Finder->addMatcher(
85 varDecl(unless(anyOf(hasParent(declStmt(hasParent(forStmt()))),
86 hasParent(cxxCatchStmt()), parmVarDecl())))
87 .bind("standaloneVar"),
88 this);
89}
90
91void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
92 const auto *StandaloneVar = Result.Nodes.getNodeAs<VarDecl>("standaloneVar");
93 if (StandaloneVar) {
94 if (!StandaloneVar->getIdentifier())
95 return;
96
97 StringRef VarName = StandaloneVar->getName();
98
99 if (VarName.size() >= MinimumVariableNameLength ||
100 IgnoredVariableNames.match(VarName))
101 return;
102
103 diag(StandaloneVar->getLocation(), ErrorMessage)
104 << 0 << StandaloneVar << MinimumVariableNameLength;
105 }
106
107 auto *ExceptionVarName = Result.Nodes.getNodeAs<VarDecl>("exceptionVar");
108 if (ExceptionVarName) {
109 if (!ExceptionVarName->getIdentifier())
110 return;
111
112 StringRef VarName = ExceptionVarName->getName();
113 if (VarName.size() >= MinimumExceptionNameLength ||
114 IgnoredExceptionVariableNames.match(VarName))
115 return;
116
117 diag(ExceptionVarName->getLocation(), ErrorMessage)
118 << 1 << ExceptionVarName << MinimumExceptionNameLength;
119 }
120
121 const auto *LoopVar = Result.Nodes.getNodeAs<VarDecl>("loopVar");
122 if (LoopVar) {
123 if (!LoopVar->getIdentifier())
124 return;
125
126 StringRef VarName = LoopVar->getName();
127
128 if (VarName.size() >= MinimumLoopCounterNameLength ||
129 IgnoredLoopCounterNames.match(VarName))
130 return;
131
132 diag(LoopVar->getLocation(), ErrorMessage)
133 << 2 << LoopVar << MinimumLoopCounterNameLength;
134 }
135
136 const auto *ParamVar = Result.Nodes.getNodeAs<VarDecl>("paramVar");
137 if (ParamVar) {
138 if (!ParamVar->getIdentifier())
139 return;
140
141 StringRef VarName = ParamVar->getName();
142
143 if (VarName.size() >= MinimumParameterNameLength ||
144 IgnoredParameterNames.match(VarName))
145 return;
146
147 diag(ParamVar->getLocation(), ErrorMessage)
148 << 3 << ParamVar << MinimumParameterNameLength;
149 }
150}
151
152} // namespace clang::tidy::readability
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 unsigned DefaultMinimumParameterNameLength
const unsigned DefaultMinimumExceptionNameLength
const unsigned DefaultMinimumLoopCounterNameLength
const unsigned DefaultMinimumVariableNameLength
llvm::StringMap< ClangTidyValue > OptionMap