From 4332d8239d0e83fa670a591ffe6fe20dee94ca1d Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Fri, 27 Sep 2024 13:33:11 -0700 Subject: [PATCH] Fix invocation issues with clang runner (#4353) `ToolContext` should be explicitly initialized. `-c` can still require a valid, writable `-o` path. --------- Co-authored-by: Chandler Carruth --- toolchain/driver/clang_runner.cpp | 5 ++++- toolchain/driver/clang_runner_test.cpp | 12 +++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/toolchain/driver/clang_runner.cpp b/toolchain/driver/clang_runner.cpp index d97ce55e30df..e495c2b7b6c0 100644 --- a/toolchain/driver/clang_runner.cpp +++ b/toolchain/driver/clang_runner.cpp @@ -135,7 +135,10 @@ auto ClangRunner::Run(llvm::ArrayRef args) -> bool { // busybox of LLD as well, and having even the subprocesses consistently run // the Carbon install toolchain and not a system toolchain whenever possible. driver.CC1Main = [](llvm::SmallVectorImpl& argv) -> int { - llvm::ToolContext tool_context; + // TODO: Try to use a better path for argv[0] (maybe in the LLVM install + // paths). This works for now. + llvm::ToolContext tool_context = { + .Path = argv[0], .PrependArg = "clang", .NeedsPrependArg = true}; return clang_main(argv.size(), const_cast(argv.data()), tool_context); }; diff --git a/toolchain/driver/clang_runner_test.cpp b/toolchain/driver/clang_runner_test.cpp index 9025ea08cf68..5c59fe1349e0 100644 --- a/toolchain/driver/clang_runner_test.cpp +++ b/toolchain/driver/clang_runner_test.cpp @@ -155,6 +155,7 @@ TEST(ClangRunnerTest, LinkCommandEcho) { TEST(ClangRunnerTest, DashC) { std::filesystem::path test_file = WriteTestFile("test.cpp", "int test() { return 0; }"); + std::filesystem::path test_output = WriteTestFile("test.o", ""); const auto install_paths = InstallPaths::MakeForBazelRunfiles(Testing::GetExePath()); @@ -164,11 +165,12 @@ TEST(ClangRunnerTest, DashC) { ClangRunner runner(&install_paths, target, &verbose_os); std::string out; std::string err; - EXPECT_TRUE( - RunWithCapturedOutput(out, err, - [&] { - return runner.Run({"-c", test_file.string()}); - })) + EXPECT_TRUE(RunWithCapturedOutput(out, err, + [&] { + return runner.Run( + {"-c", test_file.string(), "-o", + test_output.string()}); + })) << "Verbose output from runner:\n" << verbose_out << "\n";