Skip to content

Commit 0c6c099

Browse files
test: Run index tests after merging (#138)
1 parent f651890 commit 0c6c099

File tree

3 files changed

+118
-43
lines changed

3 files changed

+118
-43
lines changed

test/Snapshot.cc

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "indexer/AbslExtras.h"
2626
#include "indexer/Enforce.h"
2727
#include "indexer/FileSystem.h"
28+
#include "indexer/IpcMessages.h"
2829
#include "indexer/ScipExtras.h"
2930

3031
#include "test/Snapshot.h"
@@ -283,17 +284,71 @@ MultiTuSnapshotTest::MultiTuSnapshotTest(
283284
}
284285
}
285286

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+
286304
void MultiTuSnapshotTest::run(SnapshotMode mode,
287305
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() {
288342
absl::flat_hash_map<RootRelativePathRef, RootRelativePathRef>
289343
inputToOutputMap;
290344
for (auto &io : this->inputOutputs) {
291345
inputToOutputMap.insert(
292346
{io.sourceFilePath.asRef(), io.snapshotPath.asRef()});
293347
}
348+
return inputToOutputMap;
349+
}
294350

295-
absl::flat_hash_map<RootRelativePath, RootRelativePath> alreadyUsedFiles;
296-
351+
void MultiTuSnapshotTest::iterateOverTus(PerTuCallback perTuCallback) {
297352
for (auto &io : this->inputOutputs) {
298353
auto &sourceFileRelPath = io.sourceFilePath.asStringRef();
299354
if (!test::isTuMainFilePath(sourceFileRelPath)) {
@@ -319,31 +374,25 @@ void MultiTuSnapshotTest::run(SnapshotMode mode,
319374
}
320375
}
321376
}
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)});
345379
}
346380
}
347381

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+
348397
} // namespace test
349398
} // namespace scip_clang

test/Snapshot.h

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,48 @@ class MultiTuSnapshotTest final {
6161
const RootRelativePath &)>
6262
getSnapshotPath);
6363

64+
struct CompdbEntryBuilder {
65+
RootRelativePathRef tuPathInSandbox;
66+
std::vector<std::string> commandLine;
67+
68+
clang::tooling::CompileCommand build(const RootPath &rootInSandbox);
69+
};
70+
71+
struct CompdbBuilder {
72+
std::vector<CompdbEntryBuilder> entries;
73+
74+
llvm::json::Value toJSON(const RootPath &rootInSandbox);
75+
};
76+
77+
using SnapshotContentsMap =
78+
absl::flat_hash_map<RootRelativePath, std::string>;
79+
6480
/// Callback that runs a compilation command against a TU and returns
6581
/// a map containing the snapshot outputs for each file used
6682
///
6783
/// Different TUs should not reuse the same headers, because this API
6884
/// currently doesn't handle index merging.
69-
using RunCompileCommandCallback =
70-
absl::FunctionRef<absl::flat_hash_map<RootRelativePath, std::string>(
71-
const RootPath &rootInSandbox, RootRelativePathRef tuFileInSandbox,
72-
std::vector<std::string> &&commandLine)>;
85+
using RunCompileCommandCallback = absl::FunctionRef<SnapshotContentsMap(
86+
const RootPath &rootInSandbox, CompdbEntryBuilder &&)>;
7387

7488
void run(SnapshotMode, RunCompileCommandCallback);
89+
90+
using RunMultiTuCompileCommandCallback =
91+
absl::FunctionRef<SnapshotContentsMap(const RootPath &rootInSandbox,
92+
CompdbBuilder &&)>;
93+
94+
void runWithMerging(SnapshotMode, RunMultiTuCompileCommandCallback);
95+
96+
private:
97+
using InputToOutputMap =
98+
absl::flat_hash_map<RootRelativePathRef, RootRelativePathRef>;
99+
InputToOutputMap buildInputToOutputMap();
100+
101+
using PerTuCallback = absl::FunctionRef<void(CompdbEntryBuilder &&)>;
102+
void iterateOverTus(PerTuCallback);
103+
104+
void checkOrUpdate(SnapshotMode, SnapshotContentsMap &&,
105+
const InputToOutputMap &);
75106
};
76107

77108
} // namespace test

test/test_main.cc

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,21 +262,19 @@ TEST_CASE("PREPROCESSING") {
262262
}};
263263
myTest.run(
264264
test::globalCliOptions.testMode,
265-
[](const RootPath &rootInSandbox, RootRelativePathRef tuPath,
266-
std::vector<std::string> &&commandLine)
265+
[](const RootPath &rootInSandbox, auto &&compdbEntryBuilder)
267266
-> absl::flat_hash_map<RootRelativePath, std::string> {
268267
TempFile tmpYamlFile(
269268
fmt::format("{}.yaml", test::globalCliOptions.testName));
270269

271270
RootRelativePath key{
272271
RootRelativePathRef{"test/preprocessor", RootKind::Project}};
272+
auto tuPath = compdbEntryBuilder.tuPathInSandbox;
273273
auto rootInSourceDir =
274274
::deriveRootInSourceDir(key.asRef(), rootInSandbox, tuPath);
275275

276-
clang::tooling::CompileCommand command{};
277-
command.Filename = rootInSandbox.makeAbsolute(tuPath).asStringRef();
278-
command.Directory = rootInSandbox.asRef().asStringView();
279-
command.CommandLine = std::move(commandLine);
276+
clang::tooling::CompileCommand command =
277+
compdbEntryBuilder.build(rootInSandbox);
280278

281279
CliOptions cliOptions{};
282280
cliOptions.workerMode = "testing";
@@ -387,10 +385,9 @@ TEST_CASE("INDEX") {
387385
newPath.replaceExtension(newExtension);
388386
return newPath;
389387
}};
390-
myTest.run(
388+
myTest.runWithMerging(
391389
test::globalCliOptions.testMode,
392-
[](const RootPath &rootInSandbox, RootRelativePathRef tuPath,
393-
std::vector<std::string> &&commandLine)
390+
[](const RootPath &rootInSandbox, auto &&compdbBuilder)
394391
-> absl::flat_hash_map<RootRelativePath, std::string> {
395392
TempFile tmpCompdb{
396393
fmt::format("{}-compdb.json", test::globalCliOptions.testName)};
@@ -401,16 +398,14 @@ TEST_CASE("INDEX") {
401398
error};
402399
ENFORCE(!error, "failed to open temporary file for compdb at {}",
403400
compdbPath.asStringRef());
404-
compdb << llvm::json::Value({clang::tooling::CompileCommand{
405-
rootInSandbox.asRef().asStringView(),
406-
rootInSandbox.makeAbsolute(tuPath).asStringRef(),
407-
std::move(commandLine), ""}});
401+
compdb << compdbBuilder.toJSON(rootInSandbox);
408402
}
409403

410404
RootRelativePath key{
411405
RootRelativePathRef{"test/index", RootKind::Project}};
412406
auto rootInSourceDir =
413-
::deriveRootInSourceDir(key.asRef(), rootInSandbox, tuPath);
407+
::deriveRootInSourceDir(key.asRef(), rootInSandbox,
408+
compdbBuilder.entries[0].tuPathInSandbox);
414409

415410
TempFile scipIndexFile{
416411
fmt::format("{}.scip", test::globalCliOptions.testName)};

0 commit comments

Comments
 (0)