Add INCLUDE-FILE: and --custom-core for file tests to specify a minimal prelude library (#5080)

The INCLUDE-FILE option is only used in the toolchain tests for now. If
specified in a file test, the given file path is added to the test's
arguments. For toolchain tests this makes the file's package available
to the test. The `--custom-core` command line flag is added to the
driver, which avoids adding the production `Core` package to the command
line. Together, these allow a test to provide their own minimal `Core`
package.

For example, this would replace `Core` with the package and prelude in
`facet_types.carbon`.
```
// INCLUDE-FILE: toolchain/testing/min_prelude/facet_types.carbon
// EXTRA-ARGS: --custom-core
```

To support this:
* //testing knows how to parse INCLUDE-FILE out of the header of a test
file.
* //testing adds the file to the virtual file system, and includes it in
the test's arguments.
* //toolchain/driver grows the --custom-core command line flag to avoid
loading the production `Core` package.

Tests that were creating their own minimal prelude to define BitAnd on
types are now pointed to
toolchain/testing/min_prelude/facet_types.carbon as the prelude. They no
longer need to `import Core` in each test as a result.

Such tests are no longer `no_prelude`, but instead have their own
prelude. So they are moved to a `min_prelude` subdirectory.

Closes #5076
This commit is contained in:
Dana Jansens
2025-03-10 19:44:50 +00:00
committed by GitHub
parent 4539114c21
commit d58b523a5e
21 changed files with 1853 additions and 1672 deletions
+19 -1
View File
@@ -492,6 +492,17 @@ static auto TryConsumeArgs(llvm::StringRef line, llvm::StringRef line_trimmed,
return true;
}
static auto TryConsumeIncludeFile(llvm::StringRef line_trimmed,
llvm::SmallVector<std::string>* include_files)
-> ErrorOr<bool> {
if (!line_trimmed.consume_front("// INCLUDE-FILE: ")) {
return false;
}
include_files->push_back(std::string(line_trimmed));
return true;
}
// Processes AUTOUPDATE lines when found. Returns true if the line is consumed.
static auto TryConsumeAutoupdate(int line_index, llvm::StringRef line_trimmed,
bool* found_autoupdate,
@@ -598,6 +609,12 @@ auto ProcessTestFile(llvm::StringRef test_name, bool running_autoupdate)
if (is_consumed) {
continue;
}
CARBON_ASSIGN_OR_RETURN(
is_consumed,
TryConsumeIncludeFile(line_trimmed, &test_file.include_files));
if (is_consumed) {
continue;
}
CARBON_ASSIGN_OR_RETURN(
is_consumed,
TryConsumeAutoupdate(line_index, line_trimmed, &found_autoupdate,
@@ -621,7 +638,8 @@ auto ProcessTestFile(llvm::StringRef test_name, bool running_autoupdate)
}
if (!found_autoupdate) {
return Error("Missing AUTOUPDATE/NOAUTOUPDATE setting");
return ErrorBuilder() << "Missing AUTOUPDATE/NOAUTOUPDATE setting: "
<< test_name;
}
test_file.has_splits = split.has_splits();