10#include "llvm/ADT/SmallString.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/Support/Error.h"
13#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);
65static ClangTidyOptions::OptionMap::const_iterator
67 StringRef NamePrefix, StringRef LocalName,
68 llvm::StringSet<> *Collector) {
70 Collector->insert((NamePrefix + LocalName).str());
71 Collector->insert(LocalName);
73 auto IterLocal =
Options.find((NamePrefix + LocalName).str());
74 auto IterGlobal =
Options.find(LocalName);
77 if (IterGlobal ==
Options.end())
79 if (IterLocal->getValue().Priority >= IterGlobal->getValue().Priority)
84std::optional<StringRef>
88 if (Iter != CheckOptions.end())
89 return StringRef(Iter->getValue().Value);
93static std::optional<bool>
getAsBool(StringRef Value,
94 const llvm::Twine &LookupName) {
96 if (std::optional<bool> Parsed = llvm::yaml::parseBool(Value))
101 if (!Value.getAsInteger(10,
Number))
108ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName)
const {
109 if (std::optional<StringRef> ValueOr = get(LocalName)) {
110 if (
auto Result =
getAsBool(*ValueOr, NamePrefix + LocalName))
112 diagnoseBadBooleanOption(NamePrefix + LocalName, *ValueOr);
119ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName)
const {
122 if (Iter != CheckOptions.end()) {
123 if (
auto Result =
getAsBool(Iter->getValue().Value, Iter->getKey()))
125 diagnoseBadBooleanOption(Iter->getKey(), Iter->getValue().Value);
132 StringRef Value)
const {
133 Options[(NamePrefix + LocalName).str()] = Value;
138 int64_t Value)
const {
139 store(
Options, LocalName, llvm::itostr(Value));
142void ClangTidyCheck::OptionsView::storeUnsigned(
144 uint64_t Value)
const {
145 store(
Options, LocalName, llvm::utostr(Value));
149void ClangTidyCheck::OptionsView::store<bool>(
152 store(
Options, LocalName, Value ? StringRef(
"true") : StringRef(
"false"));
155std::optional<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
156 StringRef LocalName, ArrayRef<NameAndValue> Mapping,
bool CheckGlobal,
157 bool IgnoreCase)
const {
160 auto Iter = CheckGlobal
163 : CheckOptions.find((NamePrefix + LocalName).str());
164 if (Iter == CheckOptions.end())
167 StringRef Value = Iter->getValue().Value;
169 unsigned EditDistance = 3;
170 for (
const auto &NameAndEnum : Mapping) {
172 if (Value.equals_insensitive(NameAndEnum.second))
173 return NameAndEnum.first;
174 }
else if (Value == NameAndEnum.second) {
175 return NameAndEnum.first;
176 }
else if (Value.equals_insensitive(NameAndEnum.second)) {
177 Closest = NameAndEnum.second;
182 Value.edit_distance(NameAndEnum.second,
true, EditDistance);
183 if (Distance < EditDistance) {
184 EditDistance = Distance;
185 Closest = NameAndEnum.second;
188 if (EditDistance < 3)
189 diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value, Closest);
191 diagnoseBadEnumOption(Iter->getKey(), Iter->getValue().Value);
196 "invalid configuration value '%0' for option '%1'%select{|; expected a "
197 "bool|; expected an integer|; did you mean '%3'?}2");
199void ClangTidyCheck::OptionsView::diagnoseBadBooleanOption(
200 const Twine &Lookup, StringRef Unparsed)
const {
201 SmallString<64> Buffer;
203 << Unparsed << Lookup.toStringRef(Buffer) << 1;
206void ClangTidyCheck::OptionsView::diagnoseBadIntegerOption(
207 const Twine &Lookup, StringRef Unparsed)
const {
208 SmallString<64> Buffer;
210 << Unparsed << Lookup.toStringRef(Buffer) << 2;
213void ClangTidyCheck::OptionsView::diagnoseBadEnumOption(
214 const Twine &Lookup, StringRef Unparsed, StringRef Suggestion)
const {
215 SmallString<64> Buffer;
217 << Unparsed << Lookup.toStringRef(Buffer);
218 if (Suggestion.empty())
221 Diag << 3 << Suggestion;
225 StringRef Default)
const {
226 return get(LocalName).value_or(Default);
231 StringRef Default)
const {
232 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, llvm::StringSet<> *Collector)
static std::optional< bool > getAsBool(StringRef Value, const llvm::Twine &LookupName)
llvm::StringMap< ClangTidyValue > OptionMap