Consolidate default Clang argument handling (#6545)

This unifies the default Clang arguments between the `clang` subcommand,
the `link` subcommand, and the `ClangInvocation` built for C++ interop.

This sets the stage to integrate either pre-built or on-demand runtimes
flags for both of these. However, this PR should have very little
practical difference. The biggest functional change is wrapping the
default arguments in flags to allow unused flags so that we can build a
collection of flags viable across compile and link.
This commit is contained in:
Chandler Carruth
2026-01-03 17:35:29 +00:00
committed by GitHub
parent e545929386
commit e7eb3b7b5a
10 changed files with 131 additions and 85 deletions
+13 -19
View File
@@ -1082,31 +1082,25 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
// TODO: Share any arguments we specify here with the `carbon clang`
// subcommand.
{
llvm::SmallVector<std::string> clang_path_and_args = {
driver_env.installation->clang_path(),
// Propagate the target to Clang.
llvm::formatv("--target={0}", options_.codegen_options.target).str(),
// Enable PIE by default, but allow it to be overridden by Clang
// arguments. Clang's default is configurable, but we'd like our
// defaults to be more stable.
// TODO: Decide if we want this.
"-fPIE",
// Propagate our optimization level to Clang as a default. This can be
// overridden by Clang arguments, but doing so will only have an effect
// if those arguments affect Clang's IR, not its pass pipeline.
GetClangOptimizationFlag(options_.opt_level).str(),
};
if (driver_env.fuzzing && !options_.clang_args.empty()) {
// Parsing specific Clang arguments can reach deep into
// external libraries that aren't fuzz clean.
TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "compile");
return {.success = false};
}
for (auto str : options_.clang_args) {
clang_path_and_args.push_back(str.str());
}
clang_invocation = BuildClangInvocation(driver_env.consumer, driver_env.fs,
clang_path_and_args);
// TODO: Move this into `BuildClangInvocation` when it can accept an
// optimization level.
llvm::SmallVector<llvm::StringRef> clang_args = {
// Propagate our optimization level to Clang as a default. This can be
// overridden by Clang arguments, but doing so will only have an effect
// if those arguments affect Clang's IR, not its pass pipeline.
GetClangOptimizationFlag(options_.opt_level),
};
clang_args.append(options_.clang_args);
clang_invocation = BuildClangInvocation(
driver_env.consumer, driver_env.fs, *driver_env.installation,
options_.codegen_options.target, clang_args);
if (!clang_invocation) {
return {.success = false};
}