-
Notifications
You must be signed in to change notification settings - Fork 2
/
ColobotLint.cpp
313 lines (252 loc) · 9.17 KB
/
ColobotLint.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
#include "ActionFactories.h"
#include "ColobotLintConfig.h"
#include "Common/Context.h"
#include "Common/OutputPrinter.h"
#include "Common/SourceLocationHelper.h"
#include "Handlers/DiagnosticHandler.h"
#include <llvm/Support/CommandLine.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <iostream>
#include <fstream>
#include <exception>
#include <boost/optional.hpp>
#include <boost/algorithm/string.hpp>
using namespace llvm;
using namespace llvm::cl;
using namespace clang;
using namespace clang::tooling;
using namespace clang::ast_matchers;
namespace
{
OptionCategory g_colobotLintOptionCategory("colobot-lint options");
static cl::list<std::string> g_projectLocalIncludePathsOpt(
"project-local-include-path",
desc("Search path(s) to project local include files"),
value_desc("path"),
cat(g_colobotLintOptionCategory));
static cl::opt<std::string> g_licenseTemplateFileOpt(
"license-template-file",
desc("Template of license that should be present at the beginning of project source files"),
value_desc("filename"),
cat(g_colobotLintOptionCategory));
static cl::list<std::string> g_outputFilterOpt(
"output-filter",
desc("Filter output by the given files and line number range"),
value_desc("file1@start:end"),
cat(g_colobotLintOptionCategory));
static cl::opt<std::string> g_outputFormat(
"output-format",
desc("Format of output: plain text or XML"),
value_desc("plain|xml"),
init("plain"),
cat(g_colobotLintOptionCategory));
static cl::opt<std::string> g_outputFileOpt(
"output-file",
desc("Where to save the XML output; if not given, write to stderr"),
value_desc("filename"),
cat(g_colobotLintOptionCategory));
static cl::opt<bool> g_verboseOpt(
"verbose",
desc("Whether to print verbose output"),
init(false),
cat(g_colobotLintOptionCategory));
static cl::opt<bool> g_debugOpt(
"debug",
desc("Whether to print even more verbose output"),
init(false),
cat(g_colobotLintOptionCategory));
static cl::list<std::string> g_ruleSelectionOpt(
"only-rule",
desc("Run only these rule(s)"),
value_desc("rule-class"),
cat(g_colobotLintOptionCategory));
static cl::opt<std::string> g_generatorSelectionOpt(
"generate-graph",
desc("If used, don't run rule checks, but instead generate given graph in dot format"),
value_desc("graph-type"),
cat(g_colobotLintOptionCategory));
extrahelp g_moreHelp(
"Colobot-lint runs just like any other tool based on Clang's libtooling.\n"
"\n"
"It requires that compilation database is generated for the project, that is to say CMake\n"
"has been executed with -DCMAKE_EXPORT_COMPILE_COMMANDS=1).\n"
"It then uses information from this file to run Clang's code parser with the exact same compile\n"
"options as during normal build, giving it the same \"view\" of code as the compiler.\n"
"\n"
"When Clang code parser generates AST from input source, we can run various rules on them,\n"
"checking the code for coding style violations.\n"
"\n"
"Additionally, normal build diagnostics (i.e. compile warnings and errors) are reported\n"
"alongside our custom rules.\n"
"\n"
"Output is saved to XML file in a cppcheck compatible format in order to be usable with Jenkins\n"
"cppcheck plugin.\n"
);
void PrintColobotLintVersion()
{
std::cout << "colobot-lint version " << COLOBOT_LINT_VERSION_STR << " built with LLVM " << LLVM_VERSION_STRING << std::endl;
std::cout << "(C) 2015 Piotr Dziwinski <piotrdz@gmail.com>" << std::endl;
std::cout << "http://colobot.info http://github.com/colobot/colobot" << std::endl;
}
boost::optional<std::vector<std::string>> ReadLicenseTemplateFile(const std::string& licenseTemplateFileName)
{
if (licenseTemplateFileName.empty())
return std::vector<std::string>({});
std::vector<std::string> licenseFileLines;
std::ifstream str;
str.open(licenseTemplateFileName.c_str());
if (!str.good())
{
std::cerr << "Could not load license template file!" << std::endl;
return boost::none;
}
while (!str.eof())
{
std::string line;
std::getline(str, line);
licenseFileLines.push_back(std::move(line));
}
return licenseFileLines;
}
boost::optional<OutputFormat> ParseOutputFormat(const std::string& outputFormat,
const std::string& generatorSelection)
{
if (!generatorSelection.empty())
return OutputFormat::DotGraph;
if (outputFormat == "xml")
return OutputFormat::XmlReport;
else if (outputFormat == "plain")
return OutputFormat::PlainTextReport;
std::cerr << "Invalid output format!" << std::endl;
return boost::none;
}
boost::optional<OutputFilter> ParseOutputFilter(const std::string& outputFilter)
{
auto printError = [&outputFilter]() -> void
{
std::cerr << "Invalid output filter: \"" << outputFilter << "\"!" << std::endl;
};
std::vector<std::string> components;
boost::split(components, outputFilter, boost::is_any_of(":"));
if (components.size() != 3)
{
printError();
return boost::none;
}
OutputFilter filterRange;
filterRange.fileName = components[0];
{
std::stringstream str;
str.str(components[1]);
str >> filterRange.startLineNumber;
if (str.fail())
{
printError();
return boost::none;
}
}
{
std::stringstream str;
str.str(components[2]);
str >> filterRange.endLineNumber;
if (str.fail())
{
printError();
return boost::none;
}
}
return filterRange;
}
boost::optional<std::vector<OutputFilter>> ParseOutputFilters(cl::list<std::string>& outputFiltersOpt)
{
std::vector<OutputFilter> outputFilters;
for (const auto& outputFilterOpt : outputFiltersOpt)
{
auto outputFilter = ParseOutputFilter(outputFilterOpt);
if (!outputFilter)
return boost::none;
outputFilters.push_back(std::move(*outputFilter));
}
return outputFilters;
}
struct ParsedOptions
{
bool debug = {};
bool verbose = {};
std::set<std::string> rulesSelection = {};
std::string generatorSelection = {};
std::set<std::string> projectLocalIncludePaths = {};
std::string outputFile = {};
OutputFormat outputFormat = {};
std::vector<std::string> licenseTemplateLines = {};
std::vector<OutputFilter> outputFilters = {};
};
boost::optional<ParsedOptions> ParseOptions()
{
ParsedOptions parsedOptions;
parsedOptions.debug = g_debugOpt;
parsedOptions.verbose = g_verboseOpt;
for (const auto& rule : g_ruleSelectionOpt)
parsedOptions.rulesSelection.insert(rule);
for (const auto& path : g_projectLocalIncludePathsOpt)
parsedOptions.projectLocalIncludePaths.insert(path);
parsedOptions.generatorSelection = g_generatorSelectionOpt;
parsedOptions.outputFile = g_outputFileOpt;
auto outputFormat = ParseOutputFormat(g_outputFormat, parsedOptions.generatorSelection);
if (!outputFormat)
{
return boost::none;
}
parsedOptions.outputFormat = std::move(*outputFormat);
auto licenseTemplateLines = ReadLicenseTemplateFile(g_licenseTemplateFileOpt);
if (!licenseTemplateLines)
{
return boost::none;
}
parsedOptions.licenseTemplateLines = std::move(*licenseTemplateLines);
auto outputFilters = ParseOutputFilters(g_outputFilterOpt);
if (!outputFilters)
{
return boost::none;
}
parsedOptions.outputFilters = std::move(*outputFilters);
return parsedOptions;
}
} // anonymous namespace
namespace boost
{
void throw_exception(std::exception const& e)
{
std::cerr << "exception from boost: " << e.what() << std::endl;
std::terminate();
}
} // namespace boost
int main(int argc, const char **argv)
{
SetVersionPrinter(PrintColobotLintVersion);
CommonOptionsParser optionsParser(argc, argv, g_colobotLintOptionCategory);
ClangTool tool(optionsParser.getCompilations(),
optionsParser.getSourcePathList());
auto parsedOptions = ParseOptions();
if (!parsedOptions)
return 1;
SourceLocationHelper sourceLocationHelper;
Context context(sourceLocationHelper,
OutputPrinter::Create(parsedOptions->outputFormat,
parsedOptions->outputFile,
std::move(parsedOptions->outputFilters),
sourceLocationHelper),
std::move(parsedOptions->projectLocalIncludePaths),
std::move(parsedOptions->licenseTemplateLines),
std::move(parsedOptions->rulesSelection),
parsedOptions->generatorSelection,
parsedOptions->verbose,
parsedOptions->debug);
sourceLocationHelper.SetContext(&context);
DiagnosticHandler diagnosticHandler(context);
tool.setDiagnosticConsumer(&diagnosticHandler);
ColobotLintASTFrontendActionFactory factory(context);
int retCode = tool.run(&factory);
context.outputPrinter->Save();
return retCode;
}