Skip to content

Commit

Permalink
Switch Driver back to parameters for construction (#4849)
Browse files Browse the repository at this point in the history
This is for more complex construction, see #4846
  • Loading branch information
jonmeow authored Jan 27, 2025
1 parent 8727445 commit 0d0e202
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 45 deletions.
7 changes: 2 additions & 5 deletions testing/base/source_gen_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,8 @@ auto TestCompile(llvm::StringRef source) -> bool {
new llvm::vfs::InMemoryFileSystem;
InstallPaths installation(
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath()));
Driver driver({.fs = fs,
.installation = &installation,
.input_stream = nullptr,
.output_stream = &llvm::outs(),
.error_stream = &llvm::errs()});
Driver driver(fs, &installation, /*input_stream=*/nullptr, &llvm::outs(),
&llvm::errs());

AddPreludeFilesToVfs(installation, fs);

Expand Down
8 changes: 2 additions & 6 deletions toolchain/check/check_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size) {
/*RequiresNullTerminator=*/false)));

llvm::raw_null_ostream null_ostream;
Driver driver({.fs = fs,
.installation = install_paths,
.input_stream = nullptr,
.output_stream = &null_ostream,
.error_stream = &null_ostream,
.fuzzing = true});
Driver driver(fs, install_paths, /*input_stream=*/nullptr, &null_ostream,
&null_ostream, /*fuzzing=*/true);

// TODO: Get checking to a point where it can handle invalid parse trees
// without crashing.
Expand Down
7 changes: 2 additions & 5 deletions toolchain/driver/compile_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ class CompileBenchmark {
public:
CompileBenchmark()
: installation_(InstallPaths::MakeForBazelRunfiles(GetExePath())),
driver_({.fs = fs_,
.installation = &installation_,
.input_stream = nullptr,
.output_stream = &llvm::outs(),
.error_stream = &llvm::errs()}) {
driver_(fs_, &installation_, /*input_stream=*/nullptr, &llvm::outs(),
&llvm::errs()) {
AddPreludeFilesToVfs(installation_, fs_);
}

Expand Down
10 changes: 8 additions & 2 deletions toolchain/driver/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ namespace Carbon {
// with the language.
class Driver {
public:
// Constructs a driver with the provided environment.
explicit Driver(DriverEnv env) : driver_env_(std::move(env)) {}
// Constructs a driver with the provided environment. `input_stream` is
// optional; other parameters are required.
explicit Driver(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
const InstallPaths* installation, FILE* input_stream,
llvm::raw_pwrite_stream* output_stream,
llvm::raw_pwrite_stream* error_stream, bool fuzzing = false)
: driver_env_(std::move(fs), installation, input_stream, output_stream,
error_stream, fuzzing) {}

// Parses the given arguments into both a subcommand to select the operation
// to perform and any arguments to that subcommand.
Expand Down
14 changes: 13 additions & 1 deletion toolchain/driver/driver_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@

namespace Carbon {

// Driver environment information, encapsulated for easy passing to subcommands.
struct DriverEnv {
explicit DriverEnv(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
const InstallPaths* installation, FILE* input_stream,
llvm::raw_pwrite_stream* output_stream,
llvm::raw_pwrite_stream* error_stream, bool fuzzing)
: fs(std::move(fs)),
installation(installation),
input_stream(input_stream),
output_stream(output_stream),
error_stream(error_stream),
fuzzing(fuzzing) {}

// The filesystem for source code.
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs;

Expand All @@ -33,7 +45,7 @@ struct DriverEnv {
// Tracks when the driver is being fuzzed. This allows specific commands to
// error rather than perform operations that aren't well behaved during
// fuzzing.
bool fuzzing = false;
bool fuzzing;
};

} // namespace Carbon
Expand Down
8 changes: 2 additions & 6 deletions toolchain/driver/driver_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ extern "C" auto LLVMFuzzerTestOneInput(const unsigned char* data, size_t size)
new llvm::vfs::InMemoryFileSystem;
RawStringOstream error_stream;
llvm::raw_null_ostream null_ostream;
Driver driver({.fs = fs,
.installation = install_paths,
.input_stream = nullptr,
.output_stream = &null_ostream,
.error_stream = &error_stream,
.fuzzing = true});
Driver driver(fs, install_paths, /*input_stream=*/nullptr, &null_ostream,
&error_stream, /*fuzzing=*/true);
if (!driver.RunCommand(args).success) {
auto str = error_stream.TakeStr();
if (llvm::StringRef(str).find("error:") == llvm::StringRef::npos) {
Expand Down
7 changes: 2 additions & 5 deletions toolchain/driver/driver_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ class DriverTest : public testing::Test {
DriverTest()
: installation_(
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath())),
driver_({.fs = fs_,
.installation = &installation_,
.input_stream = nullptr,
.output_stream = &test_output_stream_,
.error_stream = &test_error_stream_}) {
driver_(fs_, &installation_, /*input_stream=*/nullptr,
&test_output_stream_, &test_error_stream_) {
char* tmpdir_env = getenv("TEST_TMPDIR");
CARBON_CHECK(tmpdir_env != nullptr);
test_tmpdir_ = tmpdir_env;
Expand Down
6 changes: 1 addition & 5 deletions toolchain/install/busybox_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ static auto Main(int argc, char** argv) -> ErrorOr<int> {
}
args.append(argv + 1, argv + argc);

Driver driver({.fs = fs,
.installation = &install_paths,
.input_stream = stdin,
.output_stream = &llvm::outs(),
.error_stream = &llvm::errs()});
Driver driver(fs, &install_paths, stdin, &llvm::outs(), &llvm::errs());
bool success = driver.RunCommand(args).success;
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
Expand Down
7 changes: 2 additions & 5 deletions toolchain/sem_ir/yaml_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ TEST(SemIRTest, YAML) {
const auto install_paths =
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
RawStringOstream print_stream;
Driver driver({.fs = fs,
.installation = &install_paths,
.input_stream = nullptr,
.output_stream = &print_stream,
.error_stream = &llvm::errs()});
Driver driver(fs, &install_paths, /*input_stream=*/nullptr, &print_stream,
&llvm::errs());
auto run_result =
driver.RunCommand({"compile", "--no-prelude-import", "--phase=check",
"--dump-raw-sem-ir", "test.carbon"});
Expand Down
7 changes: 2 additions & 5 deletions toolchain/testing/file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,8 @@ auto ToolchainFileTest::Run(
}
}

Driver driver({.fs = fs,
.installation = &installation_,
.input_stream = input_stream,
.output_stream = &output_stream,
.error_stream = &error_stream});
Driver driver(fs, &installation_, input_stream, &output_stream,
&error_stream);
auto driver_result = driver.RunCommand(test_args);
// If any diagnostics have been produced, add a trailing newline to make the
// last diagnostic match intermediate diagnostics (that have a newline
Expand Down

0 comments on commit 0d0e202

Please sign in to comment.