annotate clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
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
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
17 namespace clang::tidy::readability {
236
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
18
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
19 const unsigned DefaultMinimumVariableNameLength = 3;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
20 const unsigned DefaultMinimumLoopCounterNameLength = 2;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
21 const unsigned DefaultMinimumExceptionNameLength = 2;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
22 const unsigned DefaultMinimumParameterNameLength = 3;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
23 const char DefaultIgnoredLoopCounterNames[] = "^[ijk_]$";
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
24 const char DefaultIgnoredVariableNames[] = "";
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
25 const char DefaultIgnoredExceptionVariableNames[] = "^[e]$";
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
26 const char DefaultIgnoredParameterNames[] = "^[n]$";
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
27
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
28 const char ErrorMessage[] =
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
29 "%select{variable|exception variable|loop variable|"
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
30 "parameter}0 name %1 is too short, expected at least %2 characters";
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
31
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
32 IdentifierLengthCheck::IdentifierLengthCheck(StringRef Name,
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
33 ClangTidyContext *Context)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
34 : ClangTidyCheck(Name, Context),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
35 MinimumVariableNameLength(Options.get("MinimumVariableNameLength",
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
36 DefaultMinimumVariableNameLength)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
37 MinimumLoopCounterNameLength(Options.get(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
38 "MinimumLoopCounterNameLength", DefaultMinimumLoopCounterNameLength)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
39 MinimumExceptionNameLength(Options.get(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
40 "MinimumExceptionNameLength", DefaultMinimumExceptionNameLength)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
41 MinimumParameterNameLength(Options.get(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
42 "MinimumParameterNameLength", DefaultMinimumParameterNameLength)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
43 IgnoredVariableNamesInput(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
44 Options.get("IgnoredVariableNames", DefaultIgnoredVariableNames)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
45 IgnoredVariableNames(IgnoredVariableNamesInput),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
46 IgnoredLoopCounterNamesInput(Options.get("IgnoredLoopCounterNames",
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
47 DefaultIgnoredLoopCounterNames)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
48 IgnoredLoopCounterNames(IgnoredLoopCounterNamesInput),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
49 IgnoredExceptionVariableNamesInput(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
50 Options.get("IgnoredExceptionVariableNames",
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
51 DefaultIgnoredExceptionVariableNames)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
52 IgnoredExceptionVariableNames(IgnoredExceptionVariableNamesInput),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
53 IgnoredParameterNamesInput(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
54 Options.get("IgnoredParameterNames", DefaultIgnoredParameterNames)),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
55 IgnoredParameterNames(IgnoredParameterNamesInput) {}
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
56
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
57 void IdentifierLengthCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
58 Options.store(Opts, "MinimumVariableNameLength", MinimumVariableNameLength);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
59 Options.store(Opts, "MinimumLoopCounterNameLength",
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
60 MinimumLoopCounterNameLength);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
61 Options.store(Opts, "MinimumExceptionNameLength", MinimumExceptionNameLength);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
62 Options.store(Opts, "MinimumParameterNameLength", MinimumParameterNameLength);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
63 Options.store(Opts, "IgnoredLoopCounterNames", IgnoredLoopCounterNamesInput);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
64 Options.store(Opts, "IgnoredVariableNames", IgnoredVariableNamesInput);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
65 Options.store(Opts, "IgnoredExceptionVariableNames",
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
66 IgnoredExceptionVariableNamesInput);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
67 Options.store(Opts, "IgnoredParameterNames", IgnoredParameterNamesInput);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
68 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
69
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
70 void IdentifierLengthCheck::registerMatchers(MatchFinder *Finder) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
71 if (MinimumLoopCounterNameLength > 1)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
72 Finder->addMatcher(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
73 forStmt(hasLoopInit(declStmt(forEach(varDecl().bind("loopVar"))))),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
74 this);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
75
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
76 if (MinimumExceptionNameLength > 1)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
77 Finder->addMatcher(varDecl(hasParent(cxxCatchStmt())).bind("exceptionVar"),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
78 this);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
79
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
80 if (MinimumParameterNameLength > 1)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
81 Finder->addMatcher(parmVarDecl().bind("paramVar"), this);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
82
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
83 if (MinimumVariableNameLength > 1)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
84 Finder->addMatcher(
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
85 varDecl(unless(anyOf(hasParent(declStmt(hasParent(forStmt()))),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
86 hasParent(cxxCatchStmt()), parmVarDecl())))
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
87 .bind("standaloneVar"),
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
88 this);
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
89 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
90
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
91 void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
92 const auto *StandaloneVar = Result.Nodes.getNodeAs<VarDecl>("standaloneVar");
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
93 if (StandaloneVar) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
94 if (!StandaloneVar->getIdentifier())
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
95 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
96
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
97 StringRef VarName = StandaloneVar->getName();
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
98
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
99 if (VarName.size() >= MinimumVariableNameLength ||
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
100 IgnoredVariableNames.match(VarName))
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
101 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
102
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
103 diag(StandaloneVar->getLocation(), ErrorMessage)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
104 << 0 << StandaloneVar << MinimumVariableNameLength;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
105 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
106
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
107 auto *ExceptionVarName = Result.Nodes.getNodeAs<VarDecl>("exceptionVar");
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
108 if (ExceptionVarName) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
109 if (!ExceptionVarName->getIdentifier())
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
110 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
111
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
112 StringRef VarName = ExceptionVarName->getName();
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
113 if (VarName.size() >= MinimumExceptionNameLength ||
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
114 IgnoredExceptionVariableNames.match(VarName))
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
115 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
116
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
117 diag(ExceptionVarName->getLocation(), ErrorMessage)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
118 << 1 << ExceptionVarName << MinimumExceptionNameLength;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
119 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
120
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
121 const auto *LoopVar = Result.Nodes.getNodeAs<VarDecl>("loopVar");
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
122 if (LoopVar) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
123 if (!LoopVar->getIdentifier())
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
124 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
125
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
126 StringRef VarName = LoopVar->getName();
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
127
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
128 if (VarName.size() >= MinimumLoopCounterNameLength ||
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
129 IgnoredLoopCounterNames.match(VarName))
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
130 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
131
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
132 diag(LoopVar->getLocation(), ErrorMessage)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
133 << 2 << LoopVar << MinimumLoopCounterNameLength;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
134 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
135
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
136 const auto *ParamVar = Result.Nodes.getNodeAs<VarDecl>("paramVar");
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
137 if (ParamVar) {
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
138 if (!ParamVar->getIdentifier())
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
139 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
140
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
141 StringRef VarName = ParamVar->getName();
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
142
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
143 if (VarName.size() >= MinimumParameterNameLength ||
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
144 IgnoredParameterNames.match(VarName))
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
145 return;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
146
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
147 diag(ParamVar->getLocation(), ErrorMessage)
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
148 << 3 << ParamVar << MinimumParameterNameLength;
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
149 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
150 }
c4bab56944e8 LLVM 16
kono
parents:
diff changeset
151
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
152 } // namespace clang::tidy::readability