annotate clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp @ 236:c4bab56944e8 llvm-original

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