From 0d0e202ce83ab265a020326e43389ca985e65ad2 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Mon, 27 Jan 2025 15:48:44 -0800 Subject: [PATCH] Switch Driver back to parameters for construction (#4849) This is for more complex construction, see #4846 --- testing/base/source_gen_test.cpp | 7 ++----- toolchain/check/check_fuzzer.cpp | 8 ++------ toolchain/driver/compile_benchmark.cpp | 7 ++----- toolchain/driver/driver.h | 10 ++++++++-- toolchain/driver/driver_env.h | 14 +++++++++++++- toolchain/driver/driver_fuzzer.cpp | 8 ++------ toolchain/driver/driver_test.cpp | 7 ++----- toolchain/install/busybox_main.cpp | 6 +----- toolchain/sem_ir/yaml_test.cpp | 7 ++----- toolchain/testing/file_test.cpp | 7 ++----- 10 files changed, 36 insertions(+), 45 deletions(-) diff --git a/testing/base/source_gen_test.cpp b/testing/base/source_gen_test.cpp index 74e46d51f10f..85fda1d51da7 100644 --- a/testing/base/source_gen_test.cpp +++ b/testing/base/source_gen_test.cpp @@ -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); diff --git a/toolchain/check/check_fuzzer.cpp b/toolchain/check/check_fuzzer.cpp index 9765bd28070c..e275d1c9e5bf 100644 --- a/toolchain/check/check_fuzzer.cpp +++ b/toolchain/check/check_fuzzer.cpp @@ -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. diff --git a/toolchain/driver/compile_benchmark.cpp b/toolchain/driver/compile_benchmark.cpp index f5b0f56c5588..337c57ae905a 100644 --- a/toolchain/driver/compile_benchmark.cpp +++ b/toolchain/driver/compile_benchmark.cpp @@ -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_); } diff --git a/toolchain/driver/driver.h b/toolchain/driver/driver.h index 688bbf90ae6a..ac020d1c2cb8 100644 --- a/toolchain/driver/driver.h +++ b/toolchain/driver/driver.h @@ -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 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. diff --git a/toolchain/driver/driver_env.h b/toolchain/driver/driver_env.h index d6e40f389b20..4e48e41c24fb 100644 --- a/toolchain/driver/driver_env.h +++ b/toolchain/driver/driver_env.h @@ -13,7 +13,19 @@ namespace Carbon { +// Driver environment information, encapsulated for easy passing to subcommands. struct DriverEnv { + explicit DriverEnv(llvm::IntrusiveRefCntPtr 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 fs; @@ -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 diff --git a/toolchain/driver/driver_fuzzer.cpp b/toolchain/driver/driver_fuzzer.cpp index ca837101ab04..61cf603ac9df 100644 --- a/toolchain/driver/driver_fuzzer.cpp +++ b/toolchain/driver/driver_fuzzer.cpp @@ -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) { diff --git a/toolchain/driver/driver_test.cpp b/toolchain/driver/driver_test.cpp index 2fd62947ba36..cec1790804f6 100644 --- a/toolchain/driver/driver_test.cpp +++ b/toolchain/driver/driver_test.cpp @@ -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; diff --git a/toolchain/install/busybox_main.cpp b/toolchain/install/busybox_main.cpp index 5a6682f872e7..12ff08642a4b 100644 --- a/toolchain/install/busybox_main.cpp +++ b/toolchain/install/busybox_main.cpp @@ -45,11 +45,7 @@ static auto Main(int argc, char** argv) -> ErrorOr { } 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; } diff --git a/toolchain/sem_ir/yaml_test.cpp b/toolchain/sem_ir/yaml_test.cpp index c386c166d279..38732564606d 100644 --- a/toolchain/sem_ir/yaml_test.cpp +++ b/toolchain/sem_ir/yaml_test.cpp @@ -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"}); diff --git a/toolchain/testing/file_test.cpp b/toolchain/testing/file_test.cpp index 6d1ec274fbc8..3c174d826185 100644 --- a/toolchain/testing/file_test.cpp +++ b/toolchain/testing/file_test.cpp @@ -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