-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.cpp
More file actions
353 lines (278 loc) · 9.07 KB
/
config.cpp
File metadata and controls
353 lines (278 loc) · 9.07 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
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
352
353
#include "config.h"
#include "picojson.h"
#include <fstream>
#include <vector>
#include <cstring>
#include <cerrno>
// mkdir and access
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
std::string Config::load(const std::filesystem::path &path)
{
// Read config file
std::ifstream ifs(path);
if (ifs.fail())
return std::strerror(errno);
const std::string text(std::istreambuf_iterator<char>(ifs), {});
// Parse JSON
picojson::value data;
const std::string err = picojson::parse(data, text);
if (!err.empty()) {
return err;
}
if (!data.is<picojson::object>())
return "Invalid config format";
const picojson::object &obj = data.get<picojson::object>();
// Read settings
for (const auto &[key, value] : obj) {
if (key == "project_file") {
if (!value.is<std::string>()) {
return "Invalid value type for '" + key + "'";
}
m_projectFilePath = value.get<std::string>();
continue;
}
if (key == "cppcheck") {
if (!value.is<std::string>()) {
return "Invalid value type for '" + key + "'";
}
m_cppcheck = value.get<std::string>();
continue;
}
if (key == "log_file") {
if (!value.is<std::string>()) {
return "Invalid value type for '" + key + "'";
}
m_logFilePath = value.get<std::string>();
continue;
}
if (key == "enable_logging") {
if (!value.is<bool>()) {
return "Invalid value type for '" + key + "'";
}
m_loggingEnabled = value.get<bool>();
continue;
}
if (key == "extra_args") {
if (!value.is<picojson::array>())
return "Invalid value type for '" + key + "'";
for (auto arg : value.get<picojson::array>()) {
if (!arg.is<std::string>())
return "Invalid value type for array element in '" + key + "'";
m_args.push_back(arg.get<std::string>());
}
continue;
}
return "Invalid config key '" + key + "'";
}
return "";
}
std::string Config::command() const
{
std::string cmd;
cmd += "\"" + m_cppcheck + "\"";
if (m_printVersion) {
cmd += " \"--version\"";
} else {
for (const auto &arg : m_args) {
// If arg contains double quotes, escape them
std::string escapedArg = arg;
size_t pos = 0;
while ((pos = escapedArg.find("\"", pos)) != std::string::npos) {
escapedArg.replace(pos, 1, "\\\"");
pos += 2;
}
cmd += " \"" + escapedArg + "\"";
}
if (!m_projectFilePath.empty()) {
cmd += " \"--project=" + m_projectFilePath.string() + "\" \"--file-filter=" + m_filename.string() + "\"";
} else {
cmd += " \"" + m_filename.string() + "\"";
}
}
cmd += " 2>&1";
#ifdef _WIN32
cmd = "\"" + cmd + "\"";
#endif
return cmd;
}
static const char *startsWith(const char *arg, const char *start)
{
if (std::strncmp(arg, start, std::strlen(start)) != 0)
return NULL;
return arg + std::strlen(start);
}
static std::filesystem::path normalizePath(const std::filesystem::path path)
{
std::filesystem::path result;
for (auto component : path) {
if (component.string() == ".")
continue;
if (component.string() == "..") {
result = result.parent_path();
continue;
}
result /= component;
}
return result;
}
std::string Config::matchFilenameFromCompileCommand()
{
if (m_projectFilePath.empty() || m_projectFilePath.extension() != ".json")
return "";
// Read compile_commands file
std::ifstream ifs(m_projectFilePath);
if (ifs.fail())
return std::strerror(errno);
const std::string text(std::istreambuf_iterator<char>(ifs), {});
// Parse JSON
picojson::value compileCommands;
const std::string err = picojson::parse(compileCommands, text);
if (!err.empty()) {
return err;
}
if (!compileCommands.is<picojson::array>())
return "Compilation database is not a JSON array";
for (const picojson::value &fileInfo : compileCommands.get<picojson::array>()) {
picojson::object obj = fileInfo.get<picojson::object>();
if (obj.count("file") && obj["file"].is<std::string>()) {
const std::string file = obj["file"].get<std::string>();
// TODO should we warn if the file is not found?
std::error_code err;
if (std::filesystem::equivalent(file, m_filename, err)) {
return "";
}
}
}
return "";
}
static bool fileExists(const std::filesystem::path &path)
{
#ifdef _WIN32
return !_access(path.string().c_str(), 0);
#else
return !access(path.string().c_str(), F_OK);
#endif
}
std::string Config::parseArgs(int argc, char **argv)
{
(void) argc;
++argv;
for (; *argv; ++argv) {
const char *arg = *argv;
const char *value;
if ((value = startsWith(arg, "--config="))) {
m_configPath = value;
continue;
}
if ((value = startsWith(arg, "--language="))) {
continue;
}
if (std::string(arg) == "--version") {
m_printVersion = true;
continue;
}
if (arg[0] == '-') {
m_args.push_back(arg);
continue;
}
if (!m_filename.empty())
return "Multiple filenames provided";
m_filename = arg;
}
if (!m_printVersion) {
if (m_filename.empty())
return "Missing filename";
if (m_logFilePath.empty()) {
const std::string error = getDefaultLogFilePath(m_logFilePath);
if (!error.empty())
return error;
}
if (m_configPath.empty())
m_configPath = findFile(m_filename, "run-cppcheck-config.json");
if (m_configPath.empty())
return "Failed to find 'run-cppcheck-config.json' in any parent directory of analyzed file";
std::string err = load(m_configPath);
if (!err.empty())
return "Failed to load '" + m_configPath.string() + "': " + err;
if (!m_projectFilePath.empty() && m_projectFilePath.is_relative())
m_projectFilePath = m_configPath.parent_path() / m_projectFilePath;
if (m_filename.is_relative())
m_filename = normalizePath(std::filesystem::current_path() / m_filename);
err = matchFilenameFromCompileCommand();
// Only warn if compile_commands.json is corrupted
if (!err.empty())
return "Failed to process compile_commands.json: " + err;
} else {
// If --version is used there is no input file, so check for config
// in current working directory
if (m_configPath.empty())
m_configPath = std::filesystem::current_path() / "run-cppcheck-config.json";
if (!fileExists(m_configPath))
return "";
std::string err = load(m_configPath);
if (!err.empty())
return "Failed to load '" + m_configPath.string() + "': " + err;
}
return "";
}
// Find config file by recursively searching parent directories of input file
std::filesystem::path Config::findFile(const std::filesystem::path &input_path, const std::string &filename)
{
auto path = input_path;
if (path.is_relative())
path = std::filesystem::current_path() / path;
do {
path = path.parent_path();
const auto config_path = path / filename;
if (std::filesystem::exists(config_path))
return config_path;
} while (path != path.root_path());
return "";
}
static std::string mkdirRecursive(const std::filesystem::path &path)
{
int res;
std::filesystem::path subpath = "";
for (auto dir : path) {
subpath = subpath / dir;
#ifdef _WIN32
res = _access(subpath.string().c_str(), 0) && _mkdir(subpath.string().c_str());
#else
res = access(subpath.string().c_str(), F_OK) && mkdir(subpath.string().c_str(), 0777);
#endif
if (res)
return "Failed to create '" + subpath.string() + "': " + std::strerror(errno);
}
return "";
}
std::string Config::getDefaultLogFilePath(std::filesystem::path &path)
{
path = "";
#ifdef _WIN32
const char *localappdata = std::getenv("LOCALAPPDATA");
if (localappdata) {
path = localappdata;
} else {
return "%LOCALAPPDATA% not set";
}
#else
const char *xdg_state_home = std::getenv("XDG_STATE_HOME");
if (xdg_state_home) {
path = xdg_state_home;
} else {
path = std::string(std::getenv("HOME")) + "/.local/state";
}
#endif
path = path / "run-cppcheck";
const std::string error = mkdirRecursive(path);
if (!error.empty())
return error;
path = path / "log.txt";
return "";
}