MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Scan.cpp
Go to the documentation of this file.
1#include "Scan.hpp"
2
3#include <clang/AST/ASTConsumer.h>
4#include <clang/Frontend/CompilerInstance.h>
5#include <clang/Frontend/FrontendAction.h>
6#include <clang/Frontend/Utils.h>
7
9
10namespace {
11
12 constexpr const char* marker_source = R"(#define MAYAFLUX_REFLECT_SCAN
13#ifdef MF_DESCRIBE
14#undef MF_DESCRIBE
15#endif
16#define MF_REFLECT_JOIN_IMPL(a, b) a##b
17#define MF_REFLECT_JOIN(a, b) MF_REFLECT_JOIN_IMPL(a, b)
18#define MF_DESCRIBE(...) using MF_REFLECT_JOIN(mf_reflection_request_, __COUNTER__) = __VA_ARGS__;
19#ifdef MF_DESCRIBE_FUNCTION
20#undef MF_DESCRIBE_FUNCTION
21#endif
22#define MF_DESCRIBE_FUNCTION(...) inline constexpr auto MF_REFLECT_JOIN(mf_signature_request_, __COUNTER__) = __VA_ARGS__;
23)";
24
25 std::filesystem::path absolute_of(const std::string& file)
26 {
27 std::error_code code;
28 auto resolved = std::filesystem::weakly_canonical(file, code);
29 return code ? std::filesystem::path(file) : resolved;
30 }
31
32 std::filesystem::path select_translation_unit(
33 const clang::tooling::CompilationDatabase& database,
34 const ScanInput& input)
35 {
36 if (!input.translation_unit.empty()) {
37 return absolute_of(input.translation_unit.string());
38 }
39 const std::vector<std::string> files = database.getAllFiles();
40 for (const std::string& file : files) {
41 const std::filesystem::path candidate = absolute_of(file);
42 for (const auto& root : input.roots) {
43 const auto relative = candidate.lexically_relative(root);
44 if (!relative.empty() && *relative.begin() != "..") {
45 return candidate;
46 }
47 }
48 }
49 return files.empty() ? std::filesystem::path {} : absolute_of(files.front());
50 }
51
52 std::string scan_source(const std::vector<std::filesystem::path>& headers)
53 {
54 std::string source = marker_source;
55 for (const auto& header : headers) {
56 source += "#include \"" + header.generic_string() + "\"\n";
57 }
58 return source;
59 }
60
61 clang::tooling::ArgumentsAdjuster make_adjuster(const ScanInput& input)
62 {
63 return [resource = input.resource_dir, extra = input.extra_args](
64 const clang::tooling::CommandLineArguments& arguments, llvm::StringRef) {
65 clang::tooling::CommandLineArguments adjusted;
66 adjusted.reserve(arguments.size() + extra.size() + 2);
67 bool has_resource = false;
68 for (std::size_t i = 0; i < arguments.size(); ++i) {
69 const std::string& argument = arguments[i];
70 if (argument == "-Xclang" && i + 3 < arguments.size() && arguments[i + 1] == "-include-pch") {
71 i += 3;
72 continue;
73 }
74 if (argument == "-include-pch") {
75 ++i;
76 continue;
77 }
78#ifdef _WIN32
79 if (argument.size() >= 3 && (argument.front() == '/' || argument.front() == '-')) {
80 const std::string_view option = std::string_view(argument).substr(1);
81 if (option.starts_with("Yu") || option.starts_with("Yc") || option.starts_with("Fp")) {
82 if (option.size() == 2 && i + 1 < arguments.size()
83 && !arguments[i + 1].empty()
84 && arguments[i + 1].front() != '/'
85 && arguments[i + 1].front() != '-') {
86 ++i;
87 }
88 continue;
89 }
90 }
91#endif
92 has_resource = has_resource || argument == "-resource-dir";
93 adjusted.push_back(argument);
94 }
95 const auto insert_at = adjusted.begin() + (adjusted.empty() ? 0 : 1);
96 std::vector<std::string> added;
97 if (!has_resource && !resource.empty() && llvm::sys::fs::is_directory(resource)) {
98 added.emplace_back("-resource-dir");
99 added.push_back(resource);
100 }
101 added.insert(added.end(), extra.begin(), extra.end());
102 adjusted.insert(insert_at, added.begin(), added.end());
103 return adjusted;
104 };
105 }
106
107 struct TemporaryDirectory {
108 std::filesystem::path path;
109
110 explicit TemporaryDirectory(std::filesystem::path directory)
111 : path(std::move(directory))
112 {
113 }
114
115 TemporaryDirectory(const TemporaryDirectory&) = delete;
116 TemporaryDirectory& operator=(const TemporaryDirectory&) = delete;
117 TemporaryDirectory(TemporaryDirectory&&) = delete;
118 TemporaryDirectory& operator=(TemporaryDirectory&&) = delete;
119
120 ~TemporaryDirectory()
121 {
122 std::error_code code;
123 std::filesystem::remove_all(path, code);
124 }
125 };
126
127 class ScanDatabase : public clang::tooling::CompilationDatabase {
128 public:
129 explicit ScanDatabase(clang::tooling::CompileCommand command)
130 : m_command(std::move(command))
131 {
132 }
133
134 [[nodiscard]] std::vector<clang::tooling::CompileCommand> getCompileCommands(llvm::StringRef file) const override
135 {
136 if (file == m_command.Filename) {
137 return { m_command };
138 }
139 return {};
140 }
141
142 [[nodiscard]] std::vector<std::string> getAllFiles() const override { return { m_command.Filename }; }
143
144 [[nodiscard]] std::vector<clang::tooling::CompileCommand> getAllCompileCommands() const override
145 {
146 return { m_command };
147 }
148
149 private:
150 clang::tooling::CompileCommand m_command;
151 };
152
153 bool retarget(clang::tooling::CompileCommand& command, const std::filesystem::path& unit,
154 const std::filesystem::path& scan_file)
155 {
156 bool replaced = false;
157 for (std::string& argument : command.CommandLine) {
158 if (argument.empty() || argument.front() == '-') {
159 continue;
160 }
161 std::filesystem::path candidate(argument);
162 if (candidate.is_relative()) {
163 candidate = std::filesystem::path(command.Directory) / candidate;
164 }
165 if (absolute_of(candidate.string()) == unit) {
166 argument = scan_file.string();
167 replaced = true;
168 }
169 }
170 command.Filename = scan_file.string();
171 return replaced;
172 }
173
174}
175
176bool scan(const ScanInput& input, RequestCollector& collector, std::string& error)
177{
178 return scan(input, [&collector](clang::ast_matchers::MatchFinder& finder) { collector.attach(finder); }, error);
179}
180
181bool scan(const ScanInput& input, const AttachMatchers& attach, std::string& error)
182{
183 if (input.build_path.empty()) {
184 error = "a compile database directory is required (-p)";
185 return false;
186 }
187
188 std::string load_error;
189 const auto database = clang::tooling::CompilationDatabase::loadFromDirectory(input.build_path.string(), load_error);
190 if (!database) {
191 error = "cannot load compile_commands.json from " + input.build_path.string() + ": " + load_error;
192 return false;
193 }
194
195 const std::filesystem::path unit = select_translation_unit(*database, input);
196 const auto commands = unit.empty() ? std::vector<clang::tooling::CompileCommand> {}
197 : database->getCompileCommands(unit.string());
198 if (commands.empty()) {
199 error = "no compile command found for translation unit " + unit.string();
200 return false;
201 }
202
203 llvm::SmallString<128> temporary;
204 if (llvm::sys::fs::createUniqueDirectory("mayaflux-reflect", temporary)) {
205 error = "cannot create a temporary directory for the scan source";
206 return false;
207 }
208 const TemporaryDirectory cleanup(std::filesystem::path(temporary.str().str()));
209
210 const std::filesystem::path scan_file = cleanup.path / "mf_reflect_scan.cpp";
211 {
212 std::ofstream source(scan_file, std::ios::binary | std::ios::trunc);
213 source << scan_source(input.headers);
214 if (!source) {
215 error = "cannot write " + scan_file.string();
216 return false;
217 }
218 }
219
220 ScanInput effective = input;
221 if (!input.stubs.empty()) {
222 for (const std::string& stub : input.stubs) {
223 std::ofstream file(cleanup.path / stub, std::ios::binary | std::ios::trunc);
224 file << "#pragma once\n";
225 if (!file) {
226 error = "cannot write " + (cleanup.path / stub).string();
227 return false;
228 }
229 }
230 effective.extra_args.push_back("-I" + cleanup.path.generic_string());
231 }
232
233 clang::tooling::CompileCommand command = commands.front();
234 if (!retarget(command, unit, scan_file)) {
235 error = "the compile command of " + unit.string() + " does not name its source file";
236 return false;
237 }
238
239 std::cerr << "Scanning " << input.headers.size() << " header(s) with the compile command of " << unit.string() << '\n';
240
241 const ScanDatabase scan_database(std::move(command));
242 clang::tooling::ClangTool tool(scan_database, { scan_file.string() });
243 tool.appendArgumentsAdjuster(make_adjuster(effective));
244
245 clang::ast_matchers::MatchFinder finder;
246 attach(finder);
247
248 if (tool.run(clang::tooling::newFrontendActionFactory(&finder).get()) != 0) {
249 error = "Clang could not scan the selected headers (translation unit " + unit.string() + ")";
250 return false;
251 }
252 return true;
253}
254
255namespace {
256
257 class RecordingAction : public clang::ASTFrontendAction {
258 public:
259 RecordingAction(clang::ast_matchers::MatchFinder& finder, clang::DependencyCollector& collector)
260 : m_finder(finder)
261 , m_collector(collector)
262 {
263 }
264
265 protected:
266 std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
267 clang::CompilerInstance& instance, llvm::StringRef) override
268 {
269 m_collector.attachToPreprocessor(instance.getPreprocessor());
270 return m_finder.newASTConsumer();
271 }
272
273 private:
274 clang::ast_matchers::MatchFinder& m_finder;
275 clang::DependencyCollector& m_collector;
276 };
277
278 class RecordingFactory : public clang::tooling::FrontendActionFactory {
279 public:
280 RecordingFactory(clang::ast_matchers::MatchFinder& finder, clang::DependencyCollector& collector)
281 : m_finder(finder)
282 , m_collector(collector)
283 {
284 }
285
286 std::unique_ptr<clang::FrontendAction> create() override
287 {
288 return std::make_unique<RecordingAction>(m_finder, m_collector);
289 }
290
291 private:
292 clang::ast_matchers::MatchFinder& m_finder;
293 clang::DependencyCollector& m_collector;
294 };
295
296 bool borrowed_command(const ScanInput& input, const std::filesystem::path& scan_file,
297 clang::tooling::CompileCommand& command, std::string& error)
298 {
299 if (input.build_path.empty()) {
300 error = "a compile database directory is required (-p) when no --compile-arg is given";
301 return false;
302 }
303
304 std::string load_error;
305 const auto database = clang::tooling::CompilationDatabase::loadFromDirectory(input.build_path.string(), load_error);
306 if (!database) {
307 error = "cannot load compile_commands.json from " + input.build_path.string() + ": " + load_error;
308 return false;
309 }
310
311 const std::filesystem::path unit = select_translation_unit(*database, input);
312 const auto commands = unit.empty() ? std::vector<clang::tooling::CompileCommand> {}
313 : database->getCompileCommands(unit.string());
314 if (commands.empty()) {
315 error = "no compile command found for translation unit " + unit.string();
316 return false;
317 }
318
319 command = commands.front();
320 if (!retarget(command, unit, scan_file)) {
321 error = "the compile command of " + unit.string() + " does not name its source file";
322 return false;
323 }
324 std::cerr << "Scanning " << input.headers.size() << " header(s) with the compile command of " << unit.string() << '\n';
325 return true;
326 }
327
328}
329
330bool scan_for_build(const BuildScanInput& input, const AttachMatchers& attach,
331 std::vector<std::filesystem::path>& dependencies, std::string& error)
332{
333 llvm::SmallString<128> temporary;
334 if (llvm::sys::fs::createUniqueDirectory("mayaflux-reflect", temporary)) {
335 error = "cannot create a temporary directory for the scan source";
336 return false;
337 }
338 const TemporaryDirectory cleanup(std::filesystem::path(temporary.str().str()));
339
340 const std::filesystem::path scan_file = cleanup.path / "mf_reflect_scan.cpp";
341 {
342 std::ofstream source(scan_file, std::ios::binary | std::ios::trunc);
343 source << scan_source(input.scan.headers);
344 if (!source) {
345 error = "cannot write " + scan_file.string();
346 return false;
347 }
348 }
349
350 ScanInput effective = input.scan;
351 for (const std::string& stub : input.scan.stubs) {
352 std::ofstream file(cleanup.path / stub, std::ios::binary | std::ios::trunc);
353 file << "#pragma once\n";
354 if (!file) {
355 error = "cannot write " + (cleanup.path / stub).string();
356 return false;
357 }
358 }
359 if (!input.scan.stubs.empty()) {
360 effective.extra_args.push_back("-I" + cleanup.path.generic_string());
361 }
362
363 clang::tooling::CompileCommand command;
364 if (input.compile_args.empty()) {
365 if (!borrowed_command(input.scan, scan_file, command, error)) {
366 return false;
367 }
368 } else {
369 std::error_code code;
370 command.Directory = std::filesystem::current_path(code).string();
371 command.Filename = scan_file.string();
372 command.CommandLine.emplace_back("clang++");
373 command.CommandLine.insert(command.CommandLine.end(), input.compile_args.begin(), input.compile_args.end());
374 command.CommandLine.push_back(scan_file.string());
375 std::cerr << "Scanning " << input.scan.headers.size() << " header(s) with explicit compile arguments\n";
376 }
377
378 const std::filesystem::path directory(command.Directory);
379 const ScanDatabase scan_database(std::move(command));
380 clang::tooling::ClangTool tool(scan_database, { scan_file.string() });
381 tool.appendArgumentsAdjuster(make_adjuster(effective));
382
383 clang::ast_matchers::MatchFinder finder;
384 attach(finder);
385
386 clang::DependencyCollector collector;
387 RecordingFactory factory(finder, collector);
388 if (tool.run(&factory) != 0) {
389 error = "Clang could not scan the selected headers";
390 return false;
391 }
392
393 const std::filesystem::path temporary_root = absolute_of(cleanup.path.string());
394 for (const std::string& file : collector.getDependencies()) {
395 std::filesystem::path path(file);
396 if (path.is_relative()) {
397 path = directory / path;
398 }
399 path = absolute_of(path.string());
400 const auto relative = path.lexically_relative(temporary_root);
401 if (!relative.empty() && *relative.begin() != "..") {
402 continue;
403 }
404 dependencies.push_back(std::move(path));
405 }
406 std::ranges::sort(dependencies);
407 dependencies.erase(std::ranges::unique(dependencies).begin(), dependencies.end());
408 return true;
409}
410
411}
void attach(clang::ast_matchers::MatchFinder &finder)
Registers the matcher for the marker aliases on finder.
Turns each MF_DESCRIBE alias in the scanned translation unit into a RecordRequest.
Definition Collector.hpp:14
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
Source source()
Begin a Source chain.
Definition Plot.hpp:127
Mapped< T > create(Surface &surface, const std::shared_ptr< Buffers::FormaBuffer > &batch, Geometry::Form< T > form, T initial, std::function< float(T)> project={})
Realize a Form on surface as a slice of batch.
Definition Tend.hpp:28
void move(EditableText &s, int n)
Move the cursor by n codepoints.
Definition TextEdit.cpp:113
bool scan_for_build(const BuildScanInput &input, const AttachMatchers &attach, std::vector< std::filesystem::path > &dependencies, std::string &error)
scan() for a build step: also reports the files the scan read.
Definition Scan.cpp:330
bool scan(const ScanInput &input, RequestCollector &collector, std::string &error)
Parses every header of input in one translation unit and fills collector.
Definition Scan.cpp:176
std::function< void(clang::ast_matchers::MatchFinder &)> AttachMatchers
Registers the matchers of one tool on the MatchFinder a scan runs.
Definition Scan.hpp:22
std::vector< std::string > compile_args
Definition Scan.hpp:43
What a build scan needs: a ScanInput, plus optional explicit compile arguments.
Definition Scan.hpp:41
std::filesystem::path build_path
Definition Scan.hpp:12
std::vector< std::string > extra_args
Definition Scan.hpp:17
std::vector< std::string > stubs
Definition Scan.hpp:18
std::vector< std::filesystem::path > headers
Definition Scan.hpp:15
What one scan needs: the compile database, the headers, and flag overrides.
Definition Scan.hpp:11