-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccdb_query.cpp
More file actions
198 lines (165 loc) · 5.41 KB
/
Copy pathccdb_query.cpp
File metadata and controls
198 lines (165 loc) · 5.41 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
#include <CLI/CLI.hpp>
#include <clang/Tooling/CompilationDatabase.h>
#include <algorithm>
#include <cctype>
#include <expected>
#include <filesystem>
#include <format>
#include <iostream>
#include <memory>
#include <ranges>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
namespace {
using namespace std::string_view_literals;
namespace fs = std::filesystem;
struct cli_path {
fs::path value;
};
bool lexical_cast(const std::string &arg, cli_path &out) {
out = {.value = fs::path{arg}};
return true;
}
std::string shell_quote(std::string_view arg) {
if (arg.empty())
return "''";
const bool needs_quote = std::ranges::any_of(arg, [](const char c) {
return std::isspace(static_cast<unsigned char>(c))
|| "'\"\\$`|&;<>()[{}]*?!#"sv.contains(c);
});
const auto escaped = arg //
| std::views::transform([](const char &c) -> std::string_view {
return '\\' == c //
? "'\\\\'"sv
: '\'' == c //
? "'\\''"sv
: std::string_view{&c, 1};
}) //
| std::views::join //
| std::ranges::to<std::string>();
return needs_quote ? std::format("'{}'", escaped) : escaped;
}
std::expected<std::unique_ptr<clang::tooling::CompilationDatabase>, std::string>
load_compile_db(const fs::path &db_path) {
std::string err;
std::unique_ptr loaded =
clang::tooling::CompilationDatabase::loadFromDirectory(
db_path.parent_path().generic_string(),
err);
if (!loaded)
return std::unexpected(std::format("failed to load compilation db {}: {}",
db_path.generic_string(),
err));
return loaded;
}
struct options {
fs::path compile_commands;
fs::path source;
std::string output_contains;
};
std::expected<clang::tooling::CompileCommand, std::string> resolve_command(
const options &o) {
std::expected db = load_compile_db(o.compile_commands);
if (!db)
return std::unexpected(std::move(db).error());
const std::vector commands =
(*db)->getCompileCommands(o.source.generic_string());
// CMake may use either path separator in Windows compile databases.
const auto normalize_path_separators = [](std::string_view path) {
return path //
| std::views::transform(
[](const char c) { return '\\' == c ? '/' : c; }) //
| std::ranges::to<std::string>();
};
const std::string output_contains =
normalize_path_separators(o.output_contains);
std::vector resolved = commands //
| std::views::filter([&output_contains, normalize_path_separators](
const clang::tooling::CompileCommand &c) {
return normalize_path_separators(c.Output).contains(output_contains);
})
| std::ranges::to<std::vector>();
if (1 == resolved.size())
return std::move(resolved.front());
if (resolved.empty()) {
return std::unexpected(
std::format("{}: no compile command from {} candidate(s) matches output "
"substring `{}`",
o.source.generic_string(),
commands.size(),
o.output_contains));
}
return std::unexpected(
std::format("{}: ambiguous compile command: {} command(s) from {} "
"candidate(s) match output substring `{}`",
o.source.generic_string(),
resolved.size(),
commands.size(),
o.output_contains));
}
std::expected<options, std::string> parse(int argc, const char *const *argv) {
CLI::App app{
"\nQuery one command from compile_commands.json."
"\n"
"\nUsage: ccdb_query <compile_commands.json> <source.cpp> [output-contains]",
};
app.allow_windows_style_options(false);
CLI::Option *compile_commands =
app.add_option("compile_commands", "Path to compile_commands.json.")
->type_name("FILE")
->check(CLI::ExistingFile)
->required();
CLI::Option *source = //
app.add_option("source", "Source file to query.")
->type_name("FILE")
->required();
CLI::Option *output_contains = //
app
.add_option("output_contains",
"Substring used to disambiguate compile command output.")
->type_name("TEXT")
->default_val(std::string{});
try {
app.parse(argc, argv);
} catch (const CLI::CallForHelp &e) {
app.exit(e);
return std::unexpected(std::string{});
} catch (const CLI::ParseError &e) {
return std::unexpected(std::string{e.what()});
}
fs::path parsed_source =
fs::absolute(source->as<cli_path>().value).lexically_normal();
if (!fs::exists(parsed_source)) {
return std::unexpected(
std::format("source does not exist: {}", parsed_source.generic_string()));
}
return options{
.compile_commands =
fs::absolute(compile_commands->as<cli_path>().value).lexically_normal(),
.source = std::move(parsed_source),
.output_contains = output_contains->as<std::string>(),
};
}
} // namespace
int main(int argc, const char *const *argv) {
return parse(argc, argv)
.and_then(resolve_command)
.transform([](const clang::tooling::CompileCommand &c) {
std::cout << std::format("{} -working-directory {}\n",
c.CommandLine //
| std::views::transform(shell_quote) //
| std::views::join_with(" "sv) //
| std::ranges::to<std::string>(),
shell_quote(c.Directory));
return 0;
})
.or_else(
[](const std::string &error) -> std::expected<int, std::monostate> {
if (!error.empty())
std::cerr << error << '\n';
return error.empty() ? 0 : -1;
})
.value();
}