-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileCommands.cpp
More file actions
381 lines (298 loc) · 7.99 KB
/
Copy pathCompileCommands.cpp
File metadata and controls
381 lines (298 loc) · 7.99 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
*
* @file CompileCommands.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
* compile_commands.json parser
*
*/
#include <vix/engine/CompileCommands.hpp>
#include <fstream>
#include <sstream>
#include <system_error>
#include <nlohmann/json.hpp>
namespace vix::engine
{
namespace
{
static std::string read_text_file_or_empty(const fs::path &path)
{
std::ifstream in(path, std::ios::binary);
if (!in)
return {};
std::ostringstream out;
out << in.rdbuf();
return out.str();
}
static bool file_exists_regular(const fs::path &path)
{
std::error_code ec;
return fs::exists(path, ec) && fs::is_regular_file(path, ec);
}
static fs::path normalize_path(const fs::path &path)
{
return path.lexically_normal();
}
static fs::path compile_database_directory(const fs::path &sourcePath)
{
if (sourcePath.empty())
return fs::current_path();
const fs::path parent = sourcePath.parent_path();
if (parent.empty())
return fs::current_path();
return fs::absolute(parent).lexically_normal();
}
static bool json_string_value(
const nlohmann::json &object,
const char *key,
std::string &out)
{
if (!object.contains(key))
return false;
const auto &value = object.at(key);
if (!value.is_string())
return false;
out = value.get<std::string>();
return true;
}
static bool json_arguments_value(
const nlohmann::json &object,
std::vector<std::string> &out)
{
if (!object.contains("arguments"))
return false;
const auto &value = object.at("arguments");
if (!value.is_array())
return false;
std::vector<std::string> args;
args.reserve(value.size());
for (const auto &item : value)
{
if (!item.is_string())
return false;
args.push_back(item.get<std::string>());
}
out = std::move(args);
return true;
}
static bool is_space(char c)
{
return c == ' ' ||
c == '\t' ||
c == '\n' ||
c == '\r' ||
c == '\f' ||
c == '\v';
}
static void push_token_if_not_empty(
std::vector<std::string> &tokens,
std::string ¤t)
{
if (current.empty())
return;
tokens.push_back(current);
current.clear();
}
static fs::path source_path_from_entry(
const nlohmann::json &object,
const fs::path &directory)
{
std::string file;
if (!json_string_value(object, "file", file))
return {};
if (file.empty())
return {};
return resolve_compile_command_path(directory, fs::path(file));
}
static fs::path output_path_from_entry(
const nlohmann::json &object,
const std::vector<std::string> &arguments,
const fs::path &directory)
{
std::string output;
if (json_string_value(object, "output", output) && !output.empty())
return resolve_compile_command_path(directory, fs::path(output));
return extract_compile_output_path(arguments, directory);
}
static CompileCommandEntry parse_one_entry(
const nlohmann::json &object,
const fs::path &databaseDir)
{
CompileCommandEntry entry;
if (!object.is_object())
return entry;
std::string directory;
if (json_string_value(object, "directory", directory) && !directory.empty())
entry.directory = resolve_compile_command_path(databaseDir, fs::path(directory));
else
entry.directory = databaseDir;
if (!json_arguments_value(object, entry.arguments))
{
std::string command;
if (!json_string_value(object, "command", command))
return entry;
entry.rawCommand = command;
entry.arguments = split_compile_command(command);
}
entry.source = source_path_from_entry(object, entry.directory);
entry.output = output_path_from_entry(object, entry.arguments, entry.directory);
if (!entry.output.empty())
entry.output = resolve_compile_command_path(entry.directory, entry.output);
return entry;
}
} // namespace
bool CompileCommandEntry::valid() const
{
return !directory.empty() &&
!source.empty() &&
!arguments.empty();
}
bool CompileCommandEntry::has_output() const
{
return !output.empty();
}
fs::path default_compile_commands_path(const fs::path &buildDir)
{
return buildDir / "compile_commands.json";
}
std::vector<std::string> split_compile_command(const std::string &command)
{
std::vector<std::string> tokens;
std::string current;
bool inSingleQuote = false;
bool inDoubleQuote = false;
bool escaped = false;
for (char c : command)
{
if (escaped)
{
current.push_back(c);
escaped = false;
continue;
}
if (c == '\\')
{
escaped = true;
continue;
}
if (inSingleQuote)
{
if (c == '\'')
inSingleQuote = false;
else
current.push_back(c);
continue;
}
if (inDoubleQuote)
{
if (c == '"')
inDoubleQuote = false;
else
current.push_back(c);
continue;
}
if (c == '\'')
{
inSingleQuote = true;
continue;
}
if (c == '"')
{
inDoubleQuote = true;
continue;
}
if (is_space(c))
{
push_token_if_not_empty(tokens, current);
continue;
}
current.push_back(c);
}
if (escaped)
current.push_back('\\');
push_token_if_not_empty(tokens, current);
return tokens;
}
std::optional<std::vector<CompileCommandEntry>> parse_compile_commands_text(
const std::string &text,
const fs::path &sourcePath)
{
if (text.empty())
return std::nullopt;
nlohmann::json root;
try
{
root = nlohmann::json::parse(text);
}
catch (...)
{
return std::nullopt;
}
if (!root.is_array())
return std::nullopt;
const fs::path databaseDir = compile_database_directory(sourcePath);
std::vector<CompileCommandEntry> entries;
entries.reserve(root.size());
for (const auto &item : root)
{
CompileCommandEntry entry = parse_one_entry(item, databaseDir);
if (!entry.valid())
continue;
entries.push_back(std::move(entry));
}
return entries;
}
std::optional<std::vector<CompileCommandEntry>> read_compile_commands(
const fs::path &path)
{
if (!file_exists_regular(path))
return std::nullopt;
return parse_compile_commands_text(
read_text_file_or_empty(path),
path);
}
fs::path resolve_compile_command_path(
const fs::path &base,
const fs::path &path)
{
if (path.empty())
return {};
if (path.is_absolute())
return normalize_path(path);
fs::path root = base;
if (root.empty())
root = fs::current_path();
return fs::absolute(root / path).lexically_normal();
}
fs::path extract_compile_output_path(
const std::vector<std::string> &arguments,
const fs::path &workingDirectory)
{
for (std::size_t i = 0; i < arguments.size(); ++i)
{
const std::string &arg = arguments[i];
if (arg == "-o")
{
if (i + 1 >= arguments.size())
return {};
return resolve_compile_command_path(
workingDirectory,
fs::path(arguments[i + 1]));
}
if (arg.rfind("-o", 0) == 0 && arg.size() > 2)
{
return resolve_compile_command_path(
workingDirectory,
fs::path(arg.substr(2)));
}
}
return {};
}
} // namespace vix::engine