10#include "llvm/ADT/StringRef.h"
11#include "llvm/ADT/StringSet.h"
12#include "llvm/Support/YAMLParser.h"
19 : CheckName(CheckName), Context(Context),
20 Options(CheckName, Context->getOptions().CheckOptions, Context) {
21 assert(Context !=
nullptr);
22 assert(!CheckName.empty());
26 StringRef Description,
27 DiagnosticIDs::Level Level) {
28 return Context->
diag(CheckName,
Loc, Description, Level);
32 DiagnosticIDs::Level Level) {
33 return Context->
diag(CheckName, Description, Level);
38 DiagnosticIDs::Level Level)
const {
42void ClangTidyCheck::run(
const ast_matchers::MatchFinder::MatchResult &Result) {
52 : NamePrefix((CheckName +
".").str()), CheckOptions(CheckOptions),
55std::optional<StringRef>
59 const auto &Iter = CheckOptions.find((NamePrefix + LocalName).str());
60 if (Iter != CheckOptions.end())
61 return StringRef(Iter->getValue().Value);
70static ClangTidyOptions::OptionMap::const_iterator
72 StringRef NamePrefix, StringRef LocalName,
74 llvm::StringSet<> *Collector = Context->getOptionsCollector();
76 Collector->insert((NamePrefix + LocalName).str());
77 Collector->insert(LocalName);
79 auto IterLocal =
Options.find((NamePrefix + LocalName).str());
80 auto IterGlobal =
Options.find(LocalName);
85 Context->configurationDiag(
86 "global option '%0' is deprecated, please use '%1%0' instead.")
87 << LocalName << NamePrefix;
90 if (IterGlobal ==
Options.end())
92 if (IterLocal->getValue().Priority >= IterGlobal->getValue().Priority)
97std::optional<StringRef>
100 if (Iter != CheckOptions.end())
101 return StringRef(Iter->getValue().Value);
106 const llvm::Twine &LookupName) {
108 if (std::optional<bool> Parsed = llvm::yaml::parseBool(Value))
113 if (!Value.getAsInteger(10,
Number))
120ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName)
const {
121 if (std::optional<StringRef> ValueOr = get(LocalName)) {
122 if (
auto Result =
getAsBool(*ValueOr, NamePrefix + LocalName))
124 diagnoseBadBooleanOption(NamePrefix + LocalName, *ValueOr);
131ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName)
const {
133 if (Iter != CheckOptions.end()) {
134 if (
auto Result =
getAsBool(Iter->getValue().Value, Iter->getKey()))
136 diagnoseBadBooleanOption(Iter->getKey(), Iter->getValue().Value);
143 StringRef Value)
const {
144 Options[(NamePrefix + LocalName).str()] = Value;
149 int64_t Value)
const {
150 store(
Options, LocalName, llvm::itostr(Value));
153void ClangTidyCheck::OptionsView::storeUnsigned(
155 uint64_t Value)
const {
156 store(
Options, LocalName, llvm::utostr(Value));
160void ClangTidyCheck::OptionsView::store<bool>(
163 store(
Options, LocalName, Value ? StringRef(
"true") : StringRef(
"false"));
166std::optional<int64_t>
167ClangTidyCheck::OptionsView::getEnumInt(StringRef LocalName,
168 ArrayRef<NameAndValue> Mapping,
169 bool CheckGlobal)
const {
174 : CheckOptions.find((NamePrefix + LocalName).str());
175 if (Iter == CheckOptions.end())
178 StringRef Value = Iter->getValue().Value;
180 unsigned EditDistance = 3;
181 for (
const auto &NameAndEnum : Mapping) {
182 if (Value == NameAndEnum.second) {
183 return NameAndEnum.first;
185 if (Value.equals_insensitive(NameAndEnum.second)) {
186 Closest = NameAndEnum.second;
191 Value.edit_distance(NameAndEnum.second,
true, EditDistance);
192 if (Distance < EditDistance) {
193 EditDistance = Distance;
194 Closest = NameAndEnum.second;
197 if (EditDistance < 3)
198 diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value, Closest);
200 diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value);
205 "invalid configuration value '%0' for option '%1'%select{|; expected a "
206 "bool|; expected an integer|; did you mean '%3'?}2");
208void ClangTidyCheck::OptionsView::diagnoseBadBooleanOption(
209 const Twine &Lookup, StringRef Unparsed)
const {
210 SmallString<64> Buffer;
212 << Unparsed << Lookup.toStringRef(Buffer) << 1;
215void ClangTidyCheck::OptionsView::diagnoseBadIntegerOption(
216 const Twine &Lookup, StringRef Unparsed)
const {
217 SmallString<64> Buffer;
219 << Unparsed << Lookup.toStringRef(Buffer) << 2;
222void ClangTidyCheck::OptionsView::diagnoseBadEnumOption(
223 const Twine &Lookup, StringRef Unparsed, StringRef Suggestion)
const {
224 SmallString<64> Buffer;
226 << Unparsed << Lookup.toStringRef(Buffer);
227 if (Suggestion.empty())
230 Diag << 3 << Suggestion;
234 StringRef Default)
const {
235 return get(LocalName).value_or(Default);
240 StringRef Default)
const {
241 return getLocalOrGlobal(LocalName).value_or(Default);
std::optional< StringRef > get(StringRef LocalName) const
Read a named option from the Context.
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions, ClangTidyContext *Context)
Initializes the instance using CheckName + "." as a prefix.
std::optional< StringRef > getLocalOrGlobal(StringRef LocalName) const
Read a named option from the Context.
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
Initializes the check with CheckName and Context.
virtual void check(const ast_matchers::MatchFinder::MatchResult &Result)
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::StringSet * getOptionsCollector() const
DiagnosticBuilder configurationDiag(StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors to do with reading the configuration using this method.
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
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 const llvm::StringSet DeprecatedGlobalOptions
static std::optional< bool > getAsBool(StringRef Value, const llvm::Twine &LookupName)
llvm::StringMap< ClangTidyValue > OptionMap