25
25
#include " indexer/AbslExtras.h"
26
26
#include " indexer/Enforce.h"
27
27
#include " indexer/FileSystem.h"
28
+ #include " indexer/IpcMessages.h"
28
29
#include " indexer/ScipExtras.h"
29
30
30
31
#include " test/Snapshot.h"
@@ -283,17 +284,71 @@ MultiTuSnapshotTest::MultiTuSnapshotTest(
283
284
}
284
285
}
285
286
287
+ clang::tooling::CompileCommand
288
+ MultiTuSnapshotTest::CompdbEntryBuilder::build (const RootPath &rootInSandbox) {
289
+ return clang::tooling::CompileCommand{
290
+ rootInSandbox.asRef ().asStringView (),
291
+ rootInSandbox.makeAbsolute (this ->tuPathInSandbox ).asStringRef (),
292
+ std::move (this ->commandLine ), " " };
293
+ }
294
+
295
+ llvm::json::Value
296
+ MultiTuSnapshotTest::CompdbBuilder::toJSON (const RootPath &rootInSandbox) {
297
+ std::vector<llvm::json::Value> jsonEntries;
298
+ for (auto &builder : this ->entries ) {
299
+ jsonEntries.emplace_back (llvm::json::Value (builder.build (rootInSandbox)));
300
+ }
301
+ return llvm::json::Value (std::move (jsonEntries));
302
+ }
303
+
286
304
void MultiTuSnapshotTest::run (SnapshotMode mode,
287
305
RunCompileCommandCallback compute) {
306
+ auto inputToOutputMap = this ->buildInputToOutputMap ();
307
+ absl::flat_hash_map<RootRelativePath, RootRelativePath> alreadyUsedFiles;
308
+
309
+ this ->iterateOverTus ([&](CompdbEntryBuilder &&entryBuilder) -> void {
310
+ auto tuPath = RootRelativePath{entryBuilder.tuPathInSandbox };
311
+ auto output = compute (this ->rootPath , std::move (entryBuilder));
312
+
313
+ for (auto &[filePath, _] : output) {
314
+ ENFORCE (!alreadyUsedFiles.contains (filePath),
315
+ " {} is (potentially indirectly) included by {} and {}; so "
316
+ " snapshot output will be overwritten" ,
317
+ filePath.asStringRef (), alreadyUsedFiles[filePath].asStringRef (),
318
+ tuPath.asStringRef ());
319
+ alreadyUsedFiles.insert ({filePath, tuPath});
320
+ }
321
+
322
+ this ->checkOrUpdate (mode, std::move (output), inputToOutputMap);
323
+ });
324
+ }
325
+
326
+ void MultiTuSnapshotTest::runWithMerging (
327
+ SnapshotMode mode, RunMultiTuCompileCommandCallback compute) {
328
+ std::vector<CompdbEntryBuilder> compdbBuilders{};
329
+
330
+ this ->iterateOverTus ([&](CompdbEntryBuilder &&entryBuilder) {
331
+ compdbBuilders.emplace_back (std::move (entryBuilder));
332
+ });
333
+
334
+ auto output =
335
+ compute (this ->rootPath , CompdbBuilder{std::move (compdbBuilders)});
336
+
337
+ this ->checkOrUpdate (mode, std::move (output), this ->buildInputToOutputMap ());
338
+ }
339
+
340
+ MultiTuSnapshotTest::InputToOutputMap
341
+ MultiTuSnapshotTest::buildInputToOutputMap () {
288
342
absl::flat_hash_map<RootRelativePathRef, RootRelativePathRef>
289
343
inputToOutputMap;
290
344
for (auto &io : this ->inputOutputs ) {
291
345
inputToOutputMap.insert (
292
346
{io.sourceFilePath .asRef (), io.snapshotPath .asRef ()});
293
347
}
348
+ return inputToOutputMap;
349
+ }
294
350
295
- absl::flat_hash_map<RootRelativePath, RootRelativePath> alreadyUsedFiles;
296
-
351
+ void MultiTuSnapshotTest::iterateOverTus (PerTuCallback perTuCallback) {
297
352
for (auto &io : this ->inputOutputs ) {
298
353
auto &sourceFileRelPath = io.sourceFilePath .asStringRef ();
299
354
if (!test::isTuMainFilePath (sourceFileRelPath)) {
@@ -319,31 +374,25 @@ void MultiTuSnapshotTest::run(SnapshotMode mode,
319
374
}
320
375
}
321
376
}
322
-
323
- auto output = compute (this ->rootPath , io.sourceFilePath .asRef (),
324
- std::move (commandLine));
325
-
326
- for (auto &[filePath, _] : output) {
327
- ENFORCE (!alreadyUsedFiles.contains (filePath),
328
- " {} is (potentially indirectly) included by {} and {}; so "
329
- " snapshot output will be overwritten" ,
330
- filePath.asStringRef (), alreadyUsedFiles[filePath].asStringRef (),
331
- io.sourceFilePath .asStringRef ());
332
- alreadyUsedFiles.insert ({filePath, io.sourceFilePath });
333
- }
334
-
335
- scip_clang::extractTransform (
336
- std::move (output), /* deterministic*/ true ,
337
- absl::FunctionRef<void (RootRelativePath &&, std::string &&)>(
338
- [&](auto &&inputPath, auto &&snapshotContent) -> void {
339
- auto it = inputToOutputMap.find (inputPath.asRef ());
340
- ENFORCE (it != inputToOutputMap.end ());
341
- auto absPath = rootPath.makeAbsolute (it->second );
342
- test::compareOrUpdateSingleFile (mode, snapshotContent,
343
- absPath.asStringRef ());
344
- }));
377
+ perTuCallback (
378
+ CompdbEntryBuilder{io.sourceFilePath .asRef (), std::move (commandLine)});
345
379
}
346
380
}
347
381
382
+ void MultiTuSnapshotTest::checkOrUpdate (
383
+ SnapshotMode mode, SnapshotContentsMap &&output,
384
+ const InputToOutputMap &inputToOutputMap) {
385
+ scip_clang::extractTransform (
386
+ std::move (output), /* deterministic*/ true ,
387
+ absl::FunctionRef<void (RootRelativePath &&, std::string &&)>(
388
+ [&](auto &&inputPath, auto &&snapshotContent) -> void {
389
+ auto it = inputToOutputMap.find (inputPath.asRef ());
390
+ ENFORCE (it != inputToOutputMap.end ());
391
+ auto absPath = rootPath.makeAbsolute (it->second );
392
+ test::compareOrUpdateSingleFile (mode, snapshotContent,
393
+ absPath.asStringRef ());
394
+ }));
395
+ }
396
+
348
397
} // namespace test
349
398
} // namespace scip_clang
0 commit comments