summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
Diffstat (limited to 'llvm')
-rw-r--r--llvm/docs/CommandGuide/FileCheck.rst3
-rw-r--r--llvm/include/llvm/Support/CommandLine.h11
-rw-r--r--llvm/lib/Support/CommandLine.cpp23
-rw-r--r--llvm/test/FileCheck/envvar-opts.txt15
-rw-r--r--llvm/utils/FileCheck/FileCheck.cpp3
-rw-r--r--llvm/utils/lit/lit/TestingConfig.py2
6 files changed, 52 insertions, 5 deletions
diff --git a/llvm/docs/CommandGuide/FileCheck.rst b/llvm/docs/CommandGuide/FileCheck.rst
index 830b1e00d4e..6581b33ba1c 100644
--- a/llvm/docs/CommandGuide/FileCheck.rst
+++ b/llvm/docs/CommandGuide/FileCheck.rst
@@ -24,6 +24,9 @@ match. The file to verify is read from standard input unless the
OPTIONS
-------
+Options are parsed from the environment variable ``FILECHECK_OPTS``
+and from the command line.
+
.. option:: -help
Print a summary of command line options.
diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index 799b41fbf8b..cd3543c130e 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -56,9 +56,18 @@ namespace cl {
// 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
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index b169bb60964..cb2a2e557fa 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -1061,8 +1061,27 @@ void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
}
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<const char *, 20> NewArgv;
+ BumpPtrAllocator A;
+ StringSaver Saver(A);
+ NewArgv.push_back(argv[0]);
+
+ // Parse options from environment variable.
+ if (EnvVar) {
+ if (llvm::Optional<std::string> 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<int>(NewArgv.size());
+
+ // Parse all options.
+ return GlobalParser->ParseCommandLineOptions(NewArgc, &NewArgv[0], Overview,
Errs);
}
diff --git a/llvm/test/FileCheck/envvar-opts.txt b/llvm/test/FileCheck/envvar-opts.txt
new file mode 100644
index 00000000000..bc52d88e0ab
--- /dev/null
+++ b/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
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index bf3c3983cfa..967d22f12b6 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -114,7 +114,8 @@ int main(int argc, char **argv) {
llvm::sys::Process::UseANSIEscapeCodes(true);
InitLLVM X(argc, argv);
- cl::ParseCommandLineOptions(argc, argv);
+ cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr,
+ "FILECHECK_OPTS");
FileCheckRequest Req;
for (auto Prefix : CheckPrefixes)
diff --git a/llvm/utils/lit/lit/TestingConfig.py b/llvm/utils/lit/lit/TestingConfig.py
index e2ac73b0b42..d5adb535775 100644
--- a/llvm/utils/lit/lit/TestingConfig.py
+++ b/llvm/utils/lit/lit/TestingConfig.py
@@ -26,7 +26,7 @@ class TestingConfig:
'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
OpenPOWER on IntegriCloud