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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
This file is part of the clazy static checker.
Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Sérgio Martins <sergio.martins@kdab.com>
Copyright (C) 2015 Sergio Martins <smartins@kde.org>
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 "checkmanager.h"
#include "clazy_stl.h"
#include "ClazyContext.h"
#include "Checks.h"
#include <llvm/Support/raw_ostream.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <iterator>
#include <memory>
using namespace clang;
using namespace std;
static const char * s_fixitNamePrefix = "fix-";
static const char * s_levelPrefix = "level";
std::mutex CheckManager::m_lock;
CheckManager::CheckManager()
{
m_registeredChecks.reserve(100);
registerChecks();
}
bool CheckManager::checkExists(const string &name) const
{
return checkForName(m_registeredChecks, name) != m_registeredChecks.cend();
}
CheckManager *CheckManager::instance()
{
static CheckManager s_instance;
return &s_instance;
}
void CheckManager::registerCheck(const RegisteredCheck &check)
{
m_registeredChecks.push_back(check);
}
void CheckManager::registerFixIt(int id, const string &fixitName, const string &checkName)
{
if (!clazy::startsWith(fixitName, s_fixitNamePrefix)) {
assert(false);
return;
}
auto &fixits = m_fixitsByCheckName[checkName];
for (const auto& fixit : fixits) {
if (fixit.name == fixitName) {
// It can't exist
assert(false);
return;
}
}
RegisteredFixIt fixit = {id, fixitName};
fixits.push_back(fixit);
m_fixitByName.insert({fixitName, fixit});
}
CheckBase* CheckManager::createCheck(const string &name, ClazyContext *context)
{
for (const auto& rc : m_registeredChecks) {
if (rc.name == name) {
return rc.factory(context);
}
}
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(vector<string> &userDisabledChecks) const
{
static RegisteredCheck::List requestedChecksThroughEnv;
static vector<string> disabledChecksThroughEnv;
if (requestedChecksThroughEnv.empty()) {
const char *checksEnv = getenv("CLAZY_CHECKS");
if (checksEnv) {
const string checksEnvStr = clazy::unquoteString(checksEnv);
requestedChecksThroughEnv = checksEnvStr == "all_checks" ? availableChecks(CheckLevel2)
: checksForCommaSeparatedString(checksEnvStr, /*by-ref=*/ disabledChecksThroughEnv);
}
}
std::copy(disabledChecksThroughEnv.begin(), disabledChecksThroughEnv.end(),
std::back_inserter(userDisabledChecks));
return requestedChecksThroughEnv;
}
RegisteredCheck::List::const_iterator CheckManager::checkForName(const RegisteredCheck::List &checks,
const string &name) const
{
return clazy::find_if(checks, [name](const 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;
}
static bool takeArgument(const string &arg, vector<string> &args)
{
auto it = clazy::find(args, arg);
if (it != args.end()) {
args.erase(it, it + 1);
return true;
}
return false;
}
RegisteredCheck::List CheckManager::requestedChecks(std::vector<std::string> &args, bool qt4Compat)
{
RegisteredCheck::List result;
// #1 Check if a level was specified
static const vector<string> levels = { "level0", "level1", "level2" };
const int numLevels = levels.size();
CheckLevel requestedLevel = CheckLevelUndefined;
for (int i = 0; i < numLevels; ++i) {
if (takeArgument(levels.at(i), args)) {
requestedLevel = static_cast<CheckLevel>(i);
break;
}
}
if (args.size() > 1) // we only expect a level and a comma separated list of arguments
return {};
if (args.size() == 1) {
// #2 Process list of comma separated checks that were passed to compiler
result = checksForCommaSeparatedString(args[0]);
if (result.empty()) // User passed inexisting checks.
return {};
}
// #3 Append checks specified from env variable
vector<string> userDisabledChecks;
RegisteredCheck::List checksFromEnv = requestedChecksThroughEnv(/*by-ref*/ userDisabledChecks);
copy(checksFromEnv.cbegin(), checksFromEnv.cend(), back_inserter(result));
if (result.empty() && requestedLevel == CheckLevelUndefined) {
// No checks or level specified, lets use the default level
requestedLevel = DefaultCheckLevel;
}
// #4 Add checks from requested level
RegisteredCheck::List checksFromRequestedLevel = checksForLevel(requestedLevel);
clazy::append(checksFromRequestedLevel, result);
clazy::sort_and_remove_dups(result, checkLessThan);
CheckManager::removeChecksFromList(result, userDisabledChecks);
if (qt4Compat) {
// #5 Remove Qt4 incompatible checks
result.erase(remove_if(result.begin(), result.end(), [](const RegisteredCheck &c){
return c.options & RegisteredCheck::Option_Qt4Incompatible;
}), result.end());
}
return result;
}
RegisteredCheck::List CheckManager::checksForLevel(int level) const
{
RegisteredCheck::List result;
if (level > CheckLevelUndefined && level <= MaxCheckLevel) {
clazy::append_if(m_registeredChecks, result, [level](const RegisteredCheck &r) {
return r.level <= level;
});
}
return result;
}
std::vector<std::pair<CheckBase*, RegisteredCheck>> CheckManager::createChecks(const RegisteredCheck::List &requestedChecks,
ClazyContext *context)
{
assert(context);
std::vector<std::pair<CheckBase*, RegisteredCheck>> checks;
checks.reserve(requestedChecks.size() + 1);
for (const auto& check : requestedChecks) {
checks.push_back({createCheck(check.name, context), check });
}
return checks;
}
/*static */
void CheckManager::removeChecksFromList(RegisteredCheck::List &list, vector<string> &checkNames)
{
for (auto &name : checkNames) {
list.erase(remove_if(list.begin(), list.end(), [name](const RegisteredCheck &c) {
return c.name == name;
}), list.end());
}
}
RegisteredCheck::List CheckManager::checksForCommaSeparatedString(const string &str) const
{
vector<string> byRefDummy;
return checksForCommaSeparatedString(str, byRefDummy);
}
RegisteredCheck::List CheckManager::checksForCommaSeparatedString(const string &str,
vector<string> &userDisabledChecks) const
{
vector<string> checkNames = clazy::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);
const bool checkDoesntExist = it == m_registeredChecks.cend();
if (checkDoesntExist) {
if (clazy::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::append(levelChecks, result);
} else {
llvm::errs() << "Invalid level: " << name << "\n";
}
} else {
if (clazy::startsWith(name, "no-")) {
string checkName = name;
checkName.erase(0, 3);
if (checkExists(checkName)) {
userDisabledChecks.push_back(checkName);
} else {
llvm::errs() << "Invalid check to disable: " << name << "\n";
}
} else {
llvm::errs() << "Invalid check: " << name << "\n";
}
}
} else {
result.push_back(*it);
}
continue;
} else {
result.push_back(*it);
}
}
removeChecksFromList(result, userDisabledChecks);
return result;
}
vector<string> CheckManager::checksAsErrors() const
{
auto checksAsErrosEnv = getenv("CLAZY_CHECKS_AS_ERRORS");
if (checksAsErrosEnv) {
auto checkNames = clazy::splitString(checksAsErrosEnv, ',');
vector<string> result;
// Check whether all supplied check names are valid
for (const string &name : checkNames) {
auto it = clazy::find_if(m_registeredChecks, [&name](const RegisteredCheck &check)
{
return check.name == name;
});
if (it == m_registeredChecks.end())
llvm::errs() << "Invalid check: " << name << '\n';
else
result.emplace_back(name);
}
return result;
}
else
return {};
}
|