clang-tools 22.0.0git
ClangTidyCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ClangTidyCheck.h"
10#include "llvm/ADT/StringRef.h"
11#include "llvm/ADT/StringSet.h"
12#include "llvm/Support/YAMLParser.h"
13#include <optional>
14#include <string>
15
16namespace clang::tidy {
17
18ClangTidyCheck::ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
19 : CheckName(CheckName), Context(Context),
20 Options(CheckName, Context->getOptions().CheckOptions, Context) {
21 assert(Context != nullptr);
22 assert(!CheckName.empty());
23}
24
25DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc,
26 StringRef Description,
27 DiagnosticIDs::Level Level) {
28 return Context->diag(CheckName, Loc, Description, Level);
29}
30
31DiagnosticBuilder ClangTidyCheck::diag(StringRef Description,
32 DiagnosticIDs::Level Level) {
33 return Context->diag(CheckName, Description, Level);
34}
35
36DiagnosticBuilder
37ClangTidyCheck::configurationDiag(StringRef Description,
38 DiagnosticIDs::Level Level) const {
39 return Context->configurationDiag(Description, Level);
40}
41
42void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
43 // For historical reasons, checks don't implement the MatchFinder run()
44 // callback directly. We keep the run()/check() distinction to avoid interface
45 // churn, and to allow us to add cross-cutting logic in the future.
46 check(Result);
47}
48
49ClangTidyCheck::OptionsView::OptionsView(
50 StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions,
51 ClangTidyContext *Context)
52 : NamePrefix((CheckName + ".").str()), CheckOptions(CheckOptions),
53 Context(Context) {}
54
55std::optional<StringRef>
56ClangTidyCheck::OptionsView::get(StringRef LocalName) const {
57 if (Context->getOptionsCollector())
58 Context->getOptionsCollector()->insert((NamePrefix + LocalName).str());
59 const auto &Iter = CheckOptions.find((NamePrefix + LocalName).str());
60 if (Iter != CheckOptions.end())
61 return StringRef(Iter->getValue().Value);
62 return std::nullopt;
63}
64
65static ClangTidyOptions::OptionMap::const_iterator
67 StringRef NamePrefix, StringRef LocalName,
68 ClangTidyContext *Context) {
69 llvm::StringSet<> *Collector = Context->getOptionsCollector();
70 if (Collector) {
71 Collector->insert((NamePrefix + LocalName).str());
72 Collector->insert(LocalName);
73 }
74 auto IterLocal = Options.find((NamePrefix + LocalName).str());
75 auto IterGlobal = Options.find(LocalName);
76 if (IterLocal == Options.end())
77 return IterGlobal;
78 if (IterGlobal == Options.end())
79 return IterLocal;
80 if (IterLocal->getValue().Priority >= IterGlobal->getValue().Priority)
81 return IterLocal;
82 return IterGlobal;
83}
84
85std::optional<StringRef>
86ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const {
87 auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, Context);
88 if (Iter != CheckOptions.end())
89 return StringRef(Iter->getValue().Value);
90 return std::nullopt;
91}
92
93static std::optional<bool> getAsBool(StringRef Value,
94 const llvm::Twine &LookupName) {
95
96 if (std::optional<bool> Parsed = llvm::yaml::parseBool(Value))
97 return Parsed;
98 // To maintain backwards compatability, we support parsing numbers as
99 // booleans, even though its not supported in YAML.
100 long long Number = 0;
101 if (!Value.getAsInteger(10, Number))
102 return Number != 0;
103 return std::nullopt;
104}
105
106template <>
107std::optional<bool>
108ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName) const {
109 if (std::optional<StringRef> ValueOr = get(LocalName)) {
110 if (auto Result = getAsBool(*ValueOr, NamePrefix + LocalName))
111 return Result;
112 diagnoseBadBooleanOption(NamePrefix + LocalName, *ValueOr);
113 }
114 return std::nullopt;
115}
116
117template <>
118std::optional<bool>
119ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName) const {
120 auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, Context);
121 if (Iter != CheckOptions.end()) {
122 if (auto Result = getAsBool(Iter->getValue().Value, Iter->getKey()))
123 return Result;
124 diagnoseBadBooleanOption(Iter->getKey(), Iter->getValue().Value);
125 }
126 return std::nullopt;
127}
128
129void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
130 StringRef LocalName,
131 StringRef Value) const {
132 Options[(NamePrefix + LocalName).str()] = Value;
133}
134
135void ClangTidyCheck::OptionsView::storeInt(ClangTidyOptions::OptionMap &Options,
136 StringRef LocalName,
137 int64_t Value) const {
138 store(Options, LocalName, llvm::itostr(Value));
139}
140
141void ClangTidyCheck::OptionsView::storeUnsigned(
142 ClangTidyOptions::OptionMap &Options, StringRef LocalName,
143 uint64_t Value) const {
144 store(Options, LocalName, llvm::utostr(Value));
145}
146
147template <>
148void ClangTidyCheck::OptionsView::store<bool>(
149 ClangTidyOptions::OptionMap &Options, StringRef LocalName,
150 bool Value) const {
151 store(Options, LocalName, Value ? StringRef("true") : StringRef("false"));
152}
153
154std::optional<int64_t>
155ClangTidyCheck::OptionsView::getEnumInt(StringRef LocalName,
156 ArrayRef<NameAndValue> Mapping,
157 bool CheckGlobal) const {
158 if (!CheckGlobal && Context->getOptionsCollector())
159 Context->getOptionsCollector()->insert((NamePrefix + LocalName).str());
160 auto Iter = CheckGlobal ? findPriorityOption(CheckOptions, NamePrefix,
161 LocalName, Context)
162 : CheckOptions.find((NamePrefix + LocalName).str());
163 if (Iter == CheckOptions.end())
164 return std::nullopt;
165
166 StringRef Value = Iter->getValue().Value;
167 StringRef Closest;
168 unsigned EditDistance = 3;
169 for (const auto &NameAndEnum : Mapping) {
170 if (Value == NameAndEnum.second) {
171 return NameAndEnum.first;
172 }
173 if (Value.equals_insensitive(NameAndEnum.second)) {
174 Closest = NameAndEnum.second;
175 EditDistance = 0;
176 continue;
177 }
178 unsigned Distance =
179 Value.edit_distance(NameAndEnum.second, true, EditDistance);
180 if (Distance < EditDistance) {
181 EditDistance = Distance;
182 Closest = NameAndEnum.second;
183 }
184 }
185 if (EditDistance < 3)
186 diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value, Closest);
187 else
188 diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value);
189 return std::nullopt;
190}
191
192static constexpr llvm::StringLiteral ConfigWarning(
193 "invalid configuration value '%0' for option '%1'%select{|; expected a "
194 "bool|; expected an integer|; did you mean '%3'?}2");
195
196void ClangTidyCheck::OptionsView::diagnoseBadBooleanOption(
197 const Twine &Lookup, StringRef Unparsed) const {
198 SmallString<64> Buffer;
199 Context->configurationDiag(ConfigWarning)
200 << Unparsed << Lookup.toStringRef(Buffer) << 1;
201}
202
203void ClangTidyCheck::OptionsView::diagnoseBadIntegerOption(
204 const Twine &Lookup, StringRef Unparsed) const {
205 SmallString<64> Buffer;
206 Context->configurationDiag(ConfigWarning)
207 << Unparsed << Lookup.toStringRef(Buffer) << 2;
208}
209
210void ClangTidyCheck::OptionsView::diagnoseBadEnumOption(
211 const Twine &Lookup, StringRef Unparsed, StringRef Suggestion) const {
212 SmallString<64> Buffer;
213 auto Diag = Context->configurationDiag(ConfigWarning)
214 << Unparsed << Lookup.toStringRef(Buffer);
215 if (Suggestion.empty())
216 Diag << 0;
217 else
218 Diag << 3 << Suggestion;
219}
220
221StringRef ClangTidyCheck::OptionsView::get(StringRef LocalName,
222 StringRef Default) const {
223 return get(LocalName).value_or(Default);
224}
225
226StringRef
227ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName,
228 StringRef Default) const {
229 return getLocalOrGlobal(LocalName).value_or(Default);
230}
231} // namespace clang::tidy
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::StringSet * getOptionsCollector() const
bool check(llvm::StringRef File, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts)
Definition Check.cpp:462
static constexpr llvm::StringLiteral ConfigWarning("invalid configuration value '%0' for option '%1'%select{|; expected a " "bool|; expected an integer|; did you mean '%3'?}2")
static ClangTidyOptions::OptionMap::const_iterator findPriorityOption(const ClangTidyOptions::OptionMap &Options, StringRef NamePrefix, StringRef LocalName, ClangTidyContext *Context)
static std::optional< bool > getAsBool(StringRef Value, const llvm::Twine &LookupName)
llvm::StringMap< ClangTidyValue > OptionMap
llvm::StringMap< ClangTidyValue > OptionMap