ClangTidyCheck.cpp
7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//===--- ClangTidyCheck.cpp - clang-tidy ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ClangTidyCheck.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
namespace clang {
namespace tidy {
char MissingOptionError::ID;
char UnparseableEnumOptionError::ID;
char UnparseableIntegerOptionError::ID;
std::string MissingOptionError::message() const {
llvm::SmallString<128> Buffer;
llvm::raw_svector_ostream Output(Buffer);
Output << "option not found '" << OptionName << '\'';
return std::string(Buffer);
}
std::string UnparseableEnumOptionError::message() const {
llvm::SmallString<128> Buffer;
llvm::raw_svector_ostream Output(Buffer);
Output << "invalid configuration value '" << LookupValue << "' for option '"
<< LookupName << '\'';
if (SuggestedValue)
Output << "; did you mean '" << *SuggestedValue << "'?";
return std::string(Buffer);
}
std::string UnparseableIntegerOptionError::message() const {
llvm::SmallString<128> Buffer;
llvm::raw_svector_ostream Output(Buffer);
Output << "invalid configuration value '" << LookupValue << "' for option '"
<< LookupName << "'; expected "
<< (IsBoolean ? "a bool" : "an integer value");
return std::string(Buffer);
}
ClangTidyCheck::ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
: CheckName(CheckName), Context(Context),
Options(CheckName, Context->getOptions().CheckOptions) {
assert(Context != nullptr);
assert(!CheckName.empty());
}
DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message,
DiagnosticIDs::Level Level) {
return Context->diag(CheckName, Loc, Message, Level);
}
void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
// For historical reasons, checks don't implement the MatchFinder run()
// callback directly. We keep the run()/check() distinction to avoid interface
// churn, and to allow us to add cross-cutting logic in the future.
check(Result);
}
ClangTidyCheck::OptionsView::OptionsView(StringRef CheckName,
const ClangTidyOptions::OptionMap &CheckOptions)
: NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {}
llvm::Expected<std::string>
ClangTidyCheck::OptionsView::get(StringRef LocalName) const {
const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
if (Iter != CheckOptions.end())
return Iter->second.Value;
return llvm::make_error<MissingOptionError>((NamePrefix + LocalName).str());
}
static ClangTidyOptions::OptionMap::const_iterator
findPriorityOption(const ClangTidyOptions::OptionMap &Options, StringRef NamePrefix,
StringRef LocalName) {
auto IterLocal = Options.find((NamePrefix + LocalName).str());
auto IterGlobal = Options.find(LocalName.str());
if (IterLocal == Options.end())
return IterGlobal;
if (IterGlobal == Options.end())
return IterLocal;
if (IterLocal->second.Priority >= IterGlobal->second.Priority)
return IterLocal;
return IterGlobal;
}
llvm::Expected<std::string>
ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const {
auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName);
if (Iter != CheckOptions.end())
return Iter->second.Value;
return llvm::make_error<MissingOptionError>((NamePrefix + LocalName).str());
}
static llvm::Expected<bool> getAsBool(StringRef Value,
const llvm::Twine &LookupName) {
if (Value == "true")
return true;
if (Value == "false")
return false;
bool Result;
if (!Value.getAsInteger(10, Result))
return Result;
return llvm::make_error<UnparseableIntegerOptionError>(LookupName.str(),
Value.str(), true);
}
template <>
llvm::Expected<bool>
ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName) const {
llvm::Expected<std::string> ValueOr = get(LocalName);
if (ValueOr)
return getAsBool(*ValueOr, NamePrefix + LocalName);
return ValueOr.takeError();
}
template <>
bool ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName,
bool Default) const {
llvm::Expected<bool> ValueOr = get<bool>(LocalName);
if (ValueOr)
return *ValueOr;
logErrToStdErr(ValueOr.takeError());
return Default;
}
template <>
llvm::Expected<bool>
ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName) const {
auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName);
if (Iter != CheckOptions.end())
return getAsBool(Iter->second.Value, Iter->first);
return llvm::make_error<MissingOptionError>((NamePrefix + LocalName).str());
}
template <>
bool ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName,
bool Default) const {
llvm::Expected<bool> ValueOr = getLocalOrGlobal<bool>(LocalName);
if (ValueOr)
return *ValueOr;
logErrToStdErr(ValueOr.takeError());
return Default;
}
void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
StringRef LocalName,
StringRef Value) const {
Options[NamePrefix + LocalName.str()] = Value;
}
void ClangTidyCheck::OptionsView::storeInt(ClangTidyOptions::OptionMap &Options,
StringRef LocalName,
int64_t Value) const {
store(Options, LocalName, llvm::itostr(Value));
}
template <>
void ClangTidyCheck::OptionsView::store<bool>(
ClangTidyOptions::OptionMap &Options, StringRef LocalName,
bool Value) const {
store(Options, LocalName, Value ? StringRef("true") : StringRef("false"));
}
llvm::Expected<int64_t>
ClangTidyCheck::OptionsView::getEnumInt(StringRef LocalName,
ArrayRef<NameAndValue> Mapping,
bool CheckGlobal, bool IgnoreCase) {
auto Iter = CheckGlobal
? findPriorityOption(CheckOptions, NamePrefix, LocalName)
: CheckOptions.find((NamePrefix + LocalName).str());
if (Iter == CheckOptions.end())
return llvm::make_error<MissingOptionError>((NamePrefix + LocalName).str());
StringRef Value = Iter->second.Value;
StringRef Closest;
unsigned EditDistance = -1;
for (const auto &NameAndEnum : Mapping) {
if (IgnoreCase) {
if (Value.equals_lower(NameAndEnum.second))
return NameAndEnum.first;
} else if (Value.equals(NameAndEnum.second)) {
return NameAndEnum.first;
} else if (Value.equals_lower(NameAndEnum.second)) {
Closest = NameAndEnum.second;
EditDistance = 0;
continue;
}
unsigned Distance = Value.edit_distance(NameAndEnum.second);
if (Distance < EditDistance) {
EditDistance = Distance;
Closest = NameAndEnum.second;
}
}
if (EditDistance < 3)
return llvm::make_error<UnparseableEnumOptionError>(
Iter->first, Iter->second.Value, std::string(Closest));
return llvm::make_error<UnparseableEnumOptionError>(Iter->first,
Iter->second.Value);
}
void ClangTidyCheck::OptionsView::logErrToStdErr(llvm::Error &&Err) {
llvm::logAllUnhandledErrors(
llvm::handleErrors(std::move(Err),
[](const MissingOptionError &) -> llvm::Error {
return llvm::Error::success();
}),
llvm::errs(), "warning: ");
}
} // namespace tidy
} // namespace clang