Index: llvm/docs/CommandGuide/FileCheck.rst =================================================================== --- llvm/docs/CommandGuide/FileCheck.rst +++ llvm/docs/CommandGuide/FileCheck.rst @@ -24,6 +24,9 @@ OPTIONS ------- +Options are parsed from the command line and from the environment variable +``FILECHECK_OPTS``. + .. option:: -help Print a summary of command line options. Index: llvm/include/llvm/Support/CommandLine.h =================================================================== --- llvm/include/llvm/Support/CommandLine.h +++ llvm/include/llvm/Support/CommandLine.h @@ -56,9 +56,18 @@ // Returns true on success. Otherwise, this will print the error message to // stderr and exit if \p Errs is not set (nullptr by default), or print the // error message to \p Errs and return false if \p Errs is provided. +// +// If EnvVar is not nullptr, command-line options are also parsed from the +// environment variable named by EnvVar. Precedence is given to occurrences +// from argv. This precedence is currently implemented by parsing argv after +// the environment variable, so it is only implemented correctly for options +// that give precedence to later occurrences. If your program supports options +// that give precedence to earlier occurrences, you will need to extend this +// function to support it correctly. bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview = "", - raw_ostream *Errs = nullptr); + raw_ostream *Errs = nullptr, + const char *EnvVar = nullptr); //===----------------------------------------------------------------------===// // ParseEnvironmentOptions - Environment variable option processing alternate Index: llvm/lib/Support/CommandLine.cpp =================================================================== --- llvm/lib/Support/CommandLine.cpp +++ llvm/lib/Support/CommandLine.cpp @@ -1061,8 +1061,27 @@ } bool cl::ParseCommandLineOptions(int argc, const char *const *argv, - StringRef Overview, raw_ostream *Errs) { - return GlobalParser->ParseCommandLineOptions(argc, argv, Overview, + StringRef Overview, raw_ostream *Errs, + const char *EnvVar) { + SmallVector NewArgv; + BumpPtrAllocator A; + StringSaver Saver(A); + NewArgv.push_back(argv[0]); + + // Parse options from environment variable. + if (EnvVar) { + if (llvm::Optional EnvValue = + sys::Process::GetEnv(StringRef(EnvVar))) + TokenizeGNUCommandLine(*EnvValue, Saver, NewArgv); + } + + // Append options from command line. + for (int I = 1; I < argc; ++I) + NewArgv.push_back(argv[I]); + int NewArgc = static_cast(NewArgv.size()); + + // Parse all options. + return GlobalParser->ParseCommandLineOptions(NewArgc, &NewArgv[0], Overview, Errs); } Index: llvm/test/FileCheck/envvar-opts.txt =================================================================== --- /dev/null +++ llvm/test/FileCheck/envvar-opts.txt @@ -0,0 +1,15 @@ +; Create a case that produces a simple diagnostic. +; RUN: echo foo > %t.in +; CHECK: foo +; CHECK: bar + +; RUN: FILECHECK_OPTS= \ +; RUN: not FileCheck %s -input-file %t.in 2>&1 \ +; RUN: | FileCheck -check-prefix QUIET %s + +; RUN: FILECHECK_OPTS=-v \ +; RUN: not FileCheck %s -input-file %t.in 2>&1 \ +; RUN: | FileCheck -check-prefix VERB %s + +; QUIET-NOT: remark: {{CHECK}}: expected string found in input +; VERB: remark: {{CHECK}}: expected string found in input Index: llvm/utils/FileCheck/FileCheck.cpp =================================================================== --- llvm/utils/FileCheck/FileCheck.cpp +++ llvm/utils/FileCheck/FileCheck.cpp @@ -109,7 +109,7 @@ int main(int argc, char **argv) { InitLLVM X(argc, argv); - cl::ParseCommandLineOptions(argc, argv); + cl::ParseCommandLineOptions(argc, argv, "", nullptr, "FILECHECK_OPTS"); FileCheckRequest Req; for (auto Prefix : CheckPrefixes) Index: llvm/utils/lit/lit/TestingConfig.py =================================================================== --- llvm/utils/lit/lit/TestingConfig.py +++ llvm/utils/lit/lit/TestingConfig.py @@ -26,7 +26,7 @@ 'LSAN_OPTIONS', 'ADB', 'ANDROID_SERIAL', 'SANITIZER_IGNORE_CVE_2016_2143', 'TMPDIR', 'TMP', 'TEMP', 'TEMPDIR', 'AVRLIT_BOARD', 'AVRLIT_PORT', - 'FILECHECK_DUMP_INPUT_ON_FAILURE'] + 'FILECHECK_DUMP_INPUT_ON_FAILURE', 'FILECHECK_OPTS'] for var in pass_vars: val = os.environ.get(var, '') # Check for empty string as some variables such as LD_PRELOAD cannot be empty