-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckmanager.cpp
306 lines (253 loc) · 9.26 KB
/
checkmanager.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
This file is part of the clazy static checker.
Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
Author: Sérgio Martins <[email protected]>
Copyright (C) 2015 Sergio Martins <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "clazy_stl.h"
#include "checkmanager.h"
#include "Utils.h"
#include "StringUtils.h"
#include <stdlib.h>
using namespace clang;
using namespace std;
static const char * s_fixitNamePrefix = "fix-";
static const char * s_levelPrefix = "level";
CheckManager *CheckManager::instance()
{
static CheckManager s_instance;
return &s_instance;
}
CheckManager::CheckManager()
: m_enableAllFixits(false)
, m_requestedLevel(CheckLevelUndefined)
, m_extraOptions(clazy_std::splitString(getenv("CLAZY_EXTRA_OPTIONS"), ','))
{
m_registeredChecks.reserve(30);
const char *fixitsEnv = getenv("CLAZY_FIXIT");
if (fixitsEnv) {
if (string(fixitsEnv) == string("all_fixits")) {
m_enableAllFixits = true;
} else {
m_requestedFixitName = string(fixitsEnv);
}
}
}
bool CheckManager::isReservedCheckName(const string &name) const
{
static const vector<string> names = { "clazy" };
if (clazy_std::contains(names, name))
return true;
// level0, level1, etc are not allowed
if (clazy_std::startsWith(name, s_levelPrefix))
return true;
// These are fixit names
if (clazy_std::startsWith(name, s_fixitNamePrefix))
return true;
return false;
}
int CheckManager::registerCheck(const std::string &name, CheckLevel level, FactoryFunction factory)
{
assert(factory != nullptr);
assert(!name.empty());
if (isReservedCheckName(name)) {
llvm::errs() << "Check name not allowed" << name;
assert(false);
} else {
m_registeredChecks.push_back({name, level, factory});
}
return 0;
}
int CheckManager::registerFixIt(int id, const string &fixitName, const string &checkName)
{
if (fixitName.empty() || checkName.empty()) {
assert(false);
return 0;
}
if (!clazy_std::startsWith(fixitName, s_fixitNamePrefix)) {
assert(false);
return 0;
}
auto &fixits = m_fixitsByCheckName[checkName];
for (const auto& fixit : fixits) {
if (fixit.name == fixitName) {
// It can't exist
assert(false);
return 0;
}
}
RegisteredFixIt fixit = {id, fixitName};
fixits.push_back(fixit);
m_fixitByName.insert({fixitName, fixit});
return 0;
}
unique_ptr<CheckBase> CheckManager::createCheck(const string &name, const CompilerInstance &ci)
{
for (const auto& rc : m_registeredChecks) {
if (rc.name == name) {
return unique_ptr<CheckBase>(rc.factory(ci));
}
}
llvm::errs() << "Invalid check name " << name << "\n";
return nullptr;
}
string CheckManager::checkNameForFixIt(const string &fixitName) const
{
if (fixitName.empty())
return {};
for (auto ®isteredCheck : m_registeredChecks) {
auto it = m_fixitsByCheckName.find(registeredCheck.name);
if (it != m_fixitsByCheckName.end()) {
auto &fixits = (*it).second;
for (const RegisteredFixIt &fixit : fixits) {
if (fixit.name == fixitName)
return (*it).first;
}
}
}
return {};
}
RegisteredCheck::List CheckManager::availableChecks(CheckLevel maxLevel) const
{
RegisteredCheck::List checks = m_registeredChecks;
checks.erase(remove_if(checks.begin(), checks.end(),
[maxLevel](const RegisteredCheck &r) { return r.level > maxLevel; }), checks.end());
return checks;
}
RegisteredCheck::List CheckManager::requestedChecksThroughEnv() const
{
static RegisteredCheck::List requestedChecksThroughEnv;
if (requestedChecksThroughEnv.empty()) {
const char *checksEnv = getenv("CLAZY_CHECKS");
if (checksEnv) {
requestedChecksThroughEnv = string(checksEnv) == "all_checks" ? availableChecks(CheckLevel2)
: checksForCommaSeparatedString(checksEnv);
}
const string checkName = checkNameForFixIt(m_requestedFixitName);
if (!checkName.empty() && checkForName(requestedChecksThroughEnv, checkName) == requestedChecksThroughEnv.cend()) {
requestedChecksThroughEnv.push_back(*checkForName(m_registeredChecks, checkName));
}
}
return requestedChecksThroughEnv;
}
RegisteredCheck::List::const_iterator CheckManager::checkForName(const RegisteredCheck::List &checks,
const string &name) const
{
return clazy_std::find_if(checks, [name](RegisteredCheck r) {
return r.name == name;
} );
}
RegisteredFixIt::List CheckManager::availableFixIts(const string &checkName) const
{
auto it = m_fixitsByCheckName.find(checkName);
return it == m_fixitsByCheckName.end() ? RegisteredFixIt::List() : (*it).second;
}
RegisteredCheck::List CheckManager::checksFromRequestedLevel() const
{
return checksForLevel(m_requestedLevel);
}
RegisteredCheck::List CheckManager::checksForLevel(int level) const
{
RegisteredCheck::List result;
if (level > CheckLevelUndefined && level <= MaxCheckLevel) {
clazy_std::append_if(m_registeredChecks, result, [level](const RegisteredCheck &r) {
return r.level <= level;
});
}
return result;
}
CheckBase::List CheckManager::createChecks(RegisteredCheck::List requestedChecks,
const CompilerInstance &ci)
{
const string fixitCheckName = checkNameForFixIt(m_requestedFixitName);
RegisteredFixIt fixit = m_fixitByName[m_requestedFixitName];
CheckBase::List checks;
checks.reserve(requestedChecks.size() + 1);
for (const auto& check : requestedChecks) {
checks.push_back(createCheck(check.name, ci));
if (check.name == fixitCheckName) {
checks.back()->setEnabledFixits(fixit.id);
}
}
if (!m_requestedFixitName.empty()) {
// We have one fixit enabled, we better have the check instance too.
if (!fixitCheckName.empty()) {
if (checkForName(requestedChecks, fixitCheckName) == requestedChecks.cend()) {
checks.push_back(createCheck(fixitCheckName, ci));
checks.back()->setEnabledFixits(fixit.id);
}
}
}
return checks;
}
bool CheckManager::fixitsEnabled() const
{
return !m_requestedFixitName.empty() || m_enableAllFixits;
}
void CheckManager::enableAllFixIts()
{
m_enableAllFixits = true;
}
bool CheckManager::allFixitsEnabled() const
{
return m_enableAllFixits;
}
bool CheckManager::isOptionSet(const string &optionName) const
{
return clazy_std::contains(m_extraOptions, optionName);
}
RegisteredCheck::List CheckManager::checksForCommaSeparatedString(const string &str) const
{
vector<string> checkNames = clazy_std::splitString(str, ',');
RegisteredCheck::List result;
for (const string &name : checkNames) {
if (checkForName(result, name) != result.cend())
continue; // Already added. Duplicate check specified. continue.
auto it = checkForName(m_registeredChecks, name);
if (it == m_registeredChecks.cend()) {
// Unknown, but might be a fixit name
const string checkName = checkNameForFixIt(name);
auto it = checkForName(m_registeredChecks, checkName);
if (it == m_registeredChecks.cend()) {
if (clazy_std::startsWith(name, s_levelPrefix) && name.size() == strlen(s_levelPrefix) + 1) {
auto lastChar = name.back();
const int digit = lastChar - '0';
if (digit > CheckLevelUndefined && digit <= MaxCheckLevel) {
RegisteredCheck::List levelChecks = checksForLevel(digit);
clazy_std::append(levelChecks, result);
} else {
llvm::errs() << "Invalid check: " << name << "\n";
}
} else {
llvm::errs() << "Invalid check: " << name << "\n";
}
} else {
result.push_back(*it);
}
continue;
} else {
result.push_back(*it);
}
}
return result;
}
void CheckManager::setRequestedLevel(CheckLevel level)
{
m_requestedLevel = level;
}
CheckLevel CheckManager::requestedLevel() const
{
return m_requestedLevel;
}