From c9f8957ecaab357c45da837daf920c530d8ca5f5 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 20:42:30 +0100 Subject: [PATCH 1/7] fix(compilerlib): run no job after a driver error When Clang's driver reports an error while it builds the compilation, such as an unknown argument or a working directory it cannot move to, clang runs no job: Driver::ExecuteCompilation returns a failure. Plain file output goes through it. The other modes, which run the jobs themselves, went on: a compilation to memory, to a module or with --instrument printed the error and gave its output. They now fail with the driver's diagnostics before any job runs. Refs #264 --- src/compilerlib/compiler.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/compilerlib/compiler.cpp b/src/compilerlib/compiler.cpp index 1367316..d5e5898 100644 --- a/src/compilerlib/compiler.cpp +++ b/src/compilerlib/compiler.cpp @@ -1553,6 +1553,12 @@ namespace compilerlib if (!validateJobPlan(plan, ctx.mode, error)) return {false, std::move(error), {}}; + // The driver reported an error as it built the compilation, such as a working + // directory it could not move to. clang runs no job then, in + // Driver::ExecuteCompilation, which the modes below stand in for (#264). + if (diags->hasErrorOccurred()) + return {false, std::move(ctx.dc.message), {}}; + if (!ctx.dc.message.empty()) { ctx.driver_diagnostics = std::move(ctx.dc.message); From c7cefc6409c8c093803a4ed3f9d2e63695210ff1 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 20:42:31 +0100 Subject: [PATCH 2/7] test(compilerlib): check a compilation to memory after a driver error A working directory that does not exist: the compilation fails with the driver's error and gives no IR. Refs #264 --- test/unit/compile_test.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index b8292c7..cca407c 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -2155,6 +2155,35 @@ int once(int x) { return x; } } } + // An error of the driver's, reported while it builds the compilation: no job runs, as + // with clang, whatever runs the jobs. Here, a working directory that does not exist, and a + // profile that does not either. + class DriverErrorTest : public CompileTest + { + protected: + std::vector inAMissingDirectory(std::vector args) const + { + args.insert(args.begin(), {"-working-directory", path("missing")}); + args.push_back(writeSource("unit.c", "int value(void) { return 42; }\n")); + return args; + } + + static void expectTheDriversError(const std::string& diagnostics) + { + EXPECT_NE(diagnostics.find("unable to set working directory"), std::string::npos) + << diagnostics; + } + }; + + TEST_F(DriverErrorTest, FailsACompilationToMemory) + { + const compilerlib::CompileResult result = compilerlib::compile( + inAMissingDirectory({"-S", "-emit-llvm"}), compilerlib::OutputMode::ToMemory); + EXPECT_FALSE(result.success); + EXPECT_EQ(result.llvmIR, ""); + expectTheDriversError(result.diagnostics); + } + // -working-directory, as clang reads it: the sources, the include directories and the // files a compilation writes are relative to it. class WorkingDirectoryTest : public CompileTest From 22c0e771e565e635deaea6282a03782d5a7833a0 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 20:42:32 +0100 Subject: [PATCH 3/7] test(compilerlib): check bitcode in memory after a driver error Refs #264 --- test/unit/compile_test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index cca407c..1164828 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -2184,6 +2184,15 @@ int once(int x) { return x; } expectTheDriversError(result.diagnostics); } + TEST_F(DriverErrorTest, FailsACompilationToBitcodeInMemory) + { + const compilerlib::CompileResult result = compilerlib::compile( + inAMissingDirectory({"-c", "-emit-llvm"}), compilerlib::OutputMode::ToMemoryBitcode); + EXPECT_FALSE(result.success); + EXPECT_EQ(result.llvmIR, ""); + expectTheDriversError(result.diagnostics); + } + // -working-directory, as clang reads it: the sources, the include directories and the // files a compilation writes are relative to it. class WorkingDirectoryTest : public CompileTest From c44944cfc8cec658187df1ee565127250272514e Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 20:42:32 +0100 Subject: [PATCH 4/7] test(compilerlib): check an instrumented job after a driver error The compilation fails and writes no object. Refs #264 --- test/unit/compile_test.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 1164828..153d41f 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -2193,6 +2193,17 @@ int once(int x) { return x; } expectTheDriversError(result.diagnostics); } + TEST_F(DriverErrorTest, FailsAnInstrumentedCompilation) + { + const std::string object = path("unit.o"); + const compilerlib::CompileResult result = + compilerlib::compile(inAMissingDirectory({"-c", "-o", object}), + compilerlib::OutputMode::ToFile, /*instrument=*/true); + EXPECT_FALSE(result.success); + EXPECT_FALSE(fs::exists(object)); + expectTheDriversError(result.diagnostics); + } + // -working-directory, as clang reads it: the sources, the include directories and the // files a compilation writes are relative to it. class WorkingDirectoryTest : public CompileTest From 7f377880ad8eca5c77b0d4c2849352703ccce37d Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 20:42:33 +0100 Subject: [PATCH 5/7] test(compilerlib): check a module after a driver error Refs #264 --- test/unit/compile_test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 153d41f..787df89 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -2204,6 +2204,16 @@ int once(int x) { return x; } expectTheDriversError(result.diagnostics); } + TEST_F(DriverErrorTest, FailsACompilationToAModule) + { + llvm::LLVMContext context; + const compilerlib::ModuleResult result = + compilerlib::compileToModule(inAMissingDirectory({"-S", "-emit-llvm"}), context); + EXPECT_FALSE(result.success); + EXPECT_TRUE(result.module == nullptr); + expectTheDriversError(result.diagnostics); + } + // -working-directory, as clang reads it: the sources, the include directories and the // files a compilation writes are relative to it. class WorkingDirectoryTest : public CompileTest From 040efb08d0999f636b8dae7bc400b64647255b3f Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 20:42:33 +0100 Subject: [PATCH 6/7] test(compilerlib): check a compilation whose profile is missing Another error of the driver's: the profile of -fprofile-sample-use= does not exist. Refs #264 --- test/unit/compile_test.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 787df89..3c802b3 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -2214,6 +2214,17 @@ int once(int x) { return x; } expectTheDriversError(result.diagnostics); } + TEST_F(DriverErrorTest, FailsACompilationWhoseProfileIsMissing) + { + const compilerlib::CompileResult result = compilerlib::compile( + {"-O1", "-fprofile-sample-use=" + path("missing.afdo"), "-S", "-emit-llvm", + writeSource("unit.c", "int value(void) { return 42; }\n")}, + compilerlib::OutputMode::ToMemory); + EXPECT_FALSE(result.success); + EXPECT_NE(result.diagnostics.find("no such file or directory"), std::string::npos) + << result.diagnostics; + } + // -working-directory, as clang reads it: the sources, the include directories and the // files a compilation writes are relative to it. class WorkingDirectoryTest : public CompileTest From dcb6170370ddfaa3d69569c8f56c278baa8adb5f Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 20:42:34 +0100 Subject: [PATCH 7/7] test(compilerlib): check that a driver warning fails no compilation An argument the compilation does not use: the IR comes with the driver's warning. Refs #264 --- test/unit/compile_test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 3c802b3..bc45d52 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -2225,6 +2225,20 @@ int once(int x) { return x; } << result.diagnostics; } + // A warning of the driver's is no error: the compilation gives its output, and the + // warning with it. + TEST_F(DriverErrorTest, LeavesACompilationWithADriverWarningAlone) + { + const compilerlib::CompileResult result = + compilerlib::compile({"-L" + path("libraries"), "-S", "-emit-llvm", + writeSource("unit.c", "int value(void) { return 42; }\n")}, + compilerlib::OutputMode::ToMemory); + EXPECT_TRUE(result.success) << result.diagnostics; + EXPECT_NE(result.llvmIR.find("ret i32 42"), std::string::npos); + EXPECT_NE(result.diagnostics.find("argument unused during compilation"), std::string::npos) + << result.diagnostics; + } + // -working-directory, as clang reads it: the sources, the include directories and the // files a compilation writes are relative to it. class WorkingDirectoryTest : public CompileTest