-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClazy.cpp
351 lines (288 loc) · 11.4 KB
/
Clazy.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
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
345
346
347
348
349
350
351
/*
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 "Utils.h"
#include "StringUtils.h"
#include "clazy_stl.h"
#include "checkbase.h"
#include "checkmanager.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Frontend/FixItRewriter.h"
#include "clang/AST/ParentMap.h"
#include <llvm/Config/llvm-config.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
using namespace clang;
using namespace std;
namespace {
class MyFixItOptions : public FixItOptions
{
public:
MyFixItOptions(const MyFixItOptions &other) = delete;
MyFixItOptions(bool inplace)
{
InPlace = inplace;
FixWhatYouCan = true;
FixOnlyWarnings = true;
Silent = false;
}
std::string RewriteFilename(const std::string &filename, int &fd) override
{
fd = -1;
return InPlace ? filename : filename + "_fixed.cpp";
}
#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 6
// Clang >= 3.7 already has this member.
// We define it for clang <= 3.6 so it builds.
bool InPlace;
#endif
};
static void manuallyPopulateParentMap(ParentMap *map, Stmt *s)
{
if (!s)
return;
for (Stmt *child : s->children()) {
llvm::errs() << "Patching " << child->getStmtClassName() << "\n";
map->setParent(child, s);
manuallyPopulateParentMap(map, child);
}
}
class LazyASTConsumer : public ASTConsumer, public RecursiveASTVisitor<LazyASTConsumer>
{
public:
LazyASTConsumer(CompilerInstance &ci, const RegisteredCheck::List &requestedChecks, bool inplaceFixits)
: m_ci(ci)
, m_rewriter(nullptr)
, m_parentMap(nullptr)
{
m_createdChecks = CheckManager::instance()->createChecks(requestedChecks, ci);
if (CheckManager::instance()->fixitsEnabled())
m_rewriter = new FixItRewriter(ci.getDiagnostics(), m_ci.getSourceManager(), m_ci.getLangOpts(), new MyFixItOptions(inplaceFixits));
}
~LazyASTConsumer()
{
if (m_rewriter) {
m_rewriter->WriteFixedFiles();
delete m_rewriter;
}
delete m_parentMap;
}
void setParentMap(ParentMap *map)
{
assert(map && !m_parentMap);
m_parentMap = map;
for (auto &check : m_createdChecks)
check->setParentMap(map);
}
bool VisitDecl(Decl *decl)
{
for (const auto &check : m_createdChecks) {
check->VisitDeclaration(decl);
}
return true;
}
bool VisitStmt(Stmt *stm)
{
if (!m_parentMap) {
if (m_ci.getDiagnostics().hasUnrecoverableErrorOccurred())
return false; // ParentMap sometimes crashes when there were errors. Doesn't like a botched AST.
setParentMap(new ParentMap(stm));
}
// Workaround llvm bug: Crashes creating a parent map when encountering Catch Statements.
if (lastStm && isa<CXXCatchStmt>(lastStm) && !m_parentMap->hasParent(stm)) {
m_parentMap->setParent(stm, lastStm);
manuallyPopulateParentMap(m_parentMap, stm);
}
lastStm = stm;
// clang::ParentMap takes a root statement, but there's no root statement in the AST, the root is a declaration
// So add to parent map each time we go into a different hierarchy
if (!m_parentMap->hasParent(stm))
m_parentMap->addStmt(stm);
for (const auto &check : m_createdChecks) {
check->VisitStatement(stm);
}
return true;
}
void HandleTranslationUnit(ASTContext &ctx) override
{
TraverseDecl(ctx.getTranslationUnitDecl());
}
Stmt *lastStm = nullptr;
CompilerInstance &m_ci;
FixItRewriter *m_rewriter;
ParentMap *m_parentMap;
CheckBase::List m_createdChecks;
};
//------------------------------------------------------------------------------
static bool parseArgument(const string &arg, vector<string> &args)
{
auto it = clazy_std::find(args, arg);
if (it != args.end()) {
args.erase(it, it + 1);
return true;
}
return false;
}
static CheckLevel parseLevel(vector<std::string> &args)
{
static const vector<string> levels = { "level0", "level1", "level2", "level3", "level4" };
const int numLevels = levels.size();
for (int i = 0; i < numLevels; ++i) {
if (parseArgument(levels.at(i), args)) {
return static_cast<CheckLevel>(i);
}
}
return CheckLevelUndefined;
}
static bool checkLessThan(const RegisteredCheck &c1, const RegisteredCheck &c2)
{
return c1.name < c2.name;
}
static bool checkLessThanByLevel(const RegisteredCheck &c1, const RegisteredCheck &c2)
{
return c1.level < c2.level;
}
class LazyASTAction : public PluginASTAction {
protected:
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) override
{
return llvm::make_unique<LazyASTConsumer>(ci, m_checks, m_inplaceFixits);
}
bool ParseArgs(const CompilerInstance &ci, const std::vector<std::string> &args_) override
{
std::vector<std::string> args = args_;
if (parseArgument("help", args)) {
PrintHelp(llvm::errs());
return true;
}
if (parseArgument("no-inplace-fixits", args)) {
// Unit-tests don't use inplace fixits
m_inplaceFixits = false;
}
// This argument is for debugging purposes
const bool printRequestedChecks = parseArgument("print-requested-checks", args);
auto checkManager = CheckManager::instance();
const CheckLevel requestedLevel = parseLevel(/*by-ref*/args);
if (requestedLevel != CheckLevelUndefined) {
checkManager->setRequestedLevel(requestedLevel);
}
if (parseArgument("enable-all-fixits", args)) {
// This is useful for unit-tests, where we also want to run fixits. Don't use it otherwise.
checkManager->enableAllFixIts();
}
if (args.size() > 1) {
// Too many arguments.
llvm::errs() << "Too many arguments: ";
for (const std::string &a : args)
llvm::errs() << a << ' ';
llvm::errs() << "\n";
PrintHelp(llvm::errs());
return false;
} else if (args.size() == 1) {
m_checks = checkManager->checksForCommaSeparatedString(args[0]);
if (m_checks.empty()) {
llvm::errs() << "Could not find checks in comma separated string " + args[0] + "\n";
PrintHelp(llvm::errs());
return false;
}
}
// Append checks specified from env variable
RegisteredCheck::List checksFromEnv = CheckManager::instance()->requestedChecksThroughEnv();
copy(checksFromEnv.cbegin(), checksFromEnv.cend(), back_inserter(m_checks));
if (m_checks.empty() && requestedLevel == CheckLevelUndefined) {
// No check or level specified, lets use the default level
checkManager->setRequestedLevel(DefaultCheckLevel);
}
// Add checks from requested level
auto checksFromRequestedLevel = checkManager->checksFromRequestedLevel();
clazy_std::append(checksFromRequestedLevel, m_checks);
clazy_std::sort_and_remove_dups(m_checks, checkLessThan);
if (printRequestedChecks) {
llvm::errs() << "Requested checks: ";
const unsigned int numChecks = m_checks.size();
for (unsigned int i = 0; i < numChecks; ++i) {
llvm::errs() << m_checks.at(i).name;
const bool isLast = i == numChecks - 1;
if (!isLast) {
llvm::errs() << ", ";
}
}
llvm::errs() << "\n";
}
return true;
}
void PrintHelp(llvm::raw_ostream &ros)
{
RegisteredCheck::List checks = CheckManager::instance()->availableChecks(MaxCheckLevel);
clazy_std::sort(checks, checkLessThanByLevel);
ros << "Available checks and FixIts:\n\n";
int lastPrintedLevel = -1;
const auto numChecks = checks.size();
for (unsigned int i = 0; i < numChecks; ++i) {
const RegisteredCheck &check = checks[i];
if (lastPrintedLevel < check.level) {
lastPrintedLevel = check.level;
if (check.level > 0)
ros << "\n";
ros << "Checks from level" << to_string(check.level) << ":\n";
}
auto padded = check.name;
padded.insert(padded.end(), 39 - padded.size(), ' ');
ros << " " << check.name;
auto fixits = CheckManager::instance()->availableFixIts(check.name);
if (!fixits.empty()) {
ros << " (";
bool isFirst = true;
for (const auto& fixit : fixits) {
if (isFirst) {
isFirst = false;
} else {
ros << ',';
}
ros << fixit.name;
}
ros << ')';
}
ros << "\n";
}
ros << "\nIf nothing is specified, all checks from level0 and level1 will be run.\n\n";
ros << "To specify which checks to enable set the CLAZY_CHECKS env variable, for example:\n";
ros << " export CLAZY_CHECKS=\"level0\"\n";
ros << " export CLAZY_CHECKS=\"level0,reserve-candidates,qstring-allocations\"\n";
ros << " export CLAZY_CHECKS=\"reserve-candidates\"\n\n";
ros << "or pass as compiler arguments, for example:\n";
ros << " -Xclang -plugin-arg-clang-lazy -Xclang reserve-candidates,qstring-allocations\n";
ros << "\n";
ros << "To enable FixIts for a check, also set the env variable CLAZY_FIXIT, for example:\n";
ros << " export CLAZY_FIXIT=\"fix-qlatin1string-allocations\"\n\n";
ros << "FixIts are experimental and rewrite your code therefore only one FixIt is allowed per build.\nSpecifying a list of different FixIts is not supported.\nBackup your code before running them.\n";
}
private:
RegisteredCheck::List m_checks;
bool m_inplaceFixits = true;
};
}
static FrontendPluginRegistry::Add<LazyASTAction>
X("clang-lazy", "clang lazy plugin");