Index: test/Driver/cl-options.c =================================================================== --- test/Driver/cl-options.c +++ test/Driver/cl-options.c @@ -379,6 +379,12 @@ // RUN: %clang_cl -fmsc-version=1900 -TP -### -- %s 2>&1 | FileCheck -check-prefix=CXX14 %s // CXX14: -std=c++14 +// RUN: env CL="/Gy" %clang_cl -### -- %s 2>&1 | FileCheck -check-prefix=ENV-CL %s +// ENV-CL: "-ffunction-sections" + +// RUN: env CL="/Gy" _CL_="/Gy- -- %s" %clang_cl -### 2>&1 | FileCheck -check-prefix=ENV-_CL_ %s +// ENV-_CL_-NOT: "-ffunction-sections" + // Accept "core" clang options. // (/Zs is for syntax-only, -Werror makes it fail hard on unknown options) // RUN: %clang_cl \ Index: tools/driver/driver.cpp =================================================================== --- tools/driver/driver.cpp +++ tools/driver/driver.cpp @@ -445,6 +445,38 @@ } } + // Handle CL and _CL_ which permits additional command line options to be + // prepended or appended. + if (Tokenizer == &llvm::cl::TokenizeWindowsCommandLine) { + // The first instance of '#' should be replaced with '='. + auto AdjustOpt = [] (const char *Opt) { + if (char *NumberSignPtr = const_cast(::strchr(Opt, '#'))) + *NumberSignPtr = '='; + return Opt; + }; + + // Arguments in "CL" are prepended. + llvm::Optional OptCL = llvm::sys::Process::GetEnv("CL"); + if (OptCL.hasValue()) { + SmallVector PrependedOpts; + llvm::cl::TokenizeWindowsCommandLine(OptCL.getValue(), Saver, + PrependedOpts); + // Insert right after the program name to prepend to the argument list. + for (const char *PrependedOpt : PrependedOpts) + argv.insert(argv.begin() + 1, AdjustOpt(PrependedOpt)); + } + // Arguments in "CL" are appended. + llvm::Optional Opt_CL_ = llvm::sys::Process::GetEnv("_CL_"); + if (Opt_CL_.hasValue()) { + SmallVector AppendedOpts; + llvm::cl::TokenizeWindowsCommandLine(Opt_CL_.getValue(), Saver, + AppendedOpts); + // Insert at the end of the argument list to append. + for (const char *AppendedOpt : AppendedOpts) + argv.push_back(AdjustOpt(AppendedOpt)); + } + } + std::set SavedStrings; // Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the // scenes.