Skip to content

Commit 37783b6

Browse files
authored
[flang][driver] Introduce FCC_OVERRIDE_OPTIONS. (llvm#140556) (llvm#2671)
2 parents b3dac00 + 7755b9f commit 37783b6

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ llvm::Error expandResponseFiles(SmallVectorImpl<const char *> &Args,
903903
/// See applyOneOverrideOption.
904904
void applyOverrideOptions(SmallVectorImpl<const char *> &Args,
905905
const char *OverrideOpts,
906-
llvm::StringSet<> &SavedStrings,
906+
llvm::StringSet<> &SavedStrings, StringRef EnvVar,
907907
raw_ostream *OS = nullptr);
908908

909909
} // end namespace driver

clang/lib/Driver/Driver.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7139,9 +7139,10 @@ static const char *GetStableCStr(llvm::StringSet<> &SavedStrings, StringRef S) {
71397139
///
71407140
/// '#': Silence information about the changes to the command line arguments.
71417141
///
7142-
/// '^': Add FOO as a new argument at the beginning of the command line.
7142+
/// '^FOO': Add FOO as a new argument at the beginning of the command line
7143+
/// right after the name of the compiler executable.
71437144
///
7144-
/// '+': Add FOO as a new argument at the end of the command line.
7145+
/// '+FOO': Add FOO as a new argument at the end of the command line.
71457146
///
71467147
/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
71477148
/// line.
@@ -7229,7 +7230,7 @@ static void applyOneOverrideOption(raw_ostream &OS,
72297230
void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
72307231
const char *OverrideStr,
72317232
llvm::StringSet<> &SavedStrings,
7232-
raw_ostream *OS) {
7233+
StringRef EnvVar, raw_ostream *OS) {
72337234
if (!OS)
72347235
OS = &llvm::nulls();
72357236

@@ -7238,7 +7239,7 @@ void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
72387239
OS = &llvm::nulls();
72397240
}
72407241

7241-
*OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
7242+
*OS << "### " << EnvVar << ": " << OverrideStr << "\n";
72427243

72437244
// This does not need to be efficient.
72447245

clang/tools/driver/driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
316316
if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
317317
// FIXME: Driver shouldn't take extra initial argument.
318318
driver::applyOverrideOptions(Args, OverrideStr, SavedStrings,
319-
&llvm::errs());
319+
"CCC_OVERRIDE_OPTIONS", &llvm::errs());
320320
}
321321

322322
std::string Path = GetExecutablePath(ToolContext.Path, CanonicalPrefixes);

flang/docs/FlangDriver.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,31 @@ nvfortran defines `-fast` as
619619
- `-Mcache_align`: there is no equivalent flag in Flang or Clang.
620620
- `-Mflushz`: flush-to-zero mode - when `-ffast-math` is specified, Flang will
621621
link to `crtfastmath.o` to ensure denormal numbers are flushed to zero.
622+
623+
624+
## FCC_OVERRIDE_OPTIONS
625+
626+
The environment variable `FCC_OVERRIDE_OPTIONS` can be used to edit flang's
627+
command line arguments. The value of this variable is a space-separated list of
628+
edits to perform. The edits are applied in the order in which they appear in
629+
`FCC_OVERRIDE_OPTIONS`. Each edit should be one of the following form:
630+
631+
- `#`: Silence information about the changes to the command line arguments.
632+
633+
- `^FOO`: Add `FOO` as a new argument at the beginning of the command line right
634+
after the name of the compiler executable.
635+
636+
- `+FOO`: Add `FOO` as a new argument at the end of the command line.
637+
638+
- `s/XXX/YYY/`: Substitute the regular expression `XXX` with `YYY` in the
639+
command line.
640+
641+
- `xOPTION`: Removes all instances of the literal argument `OPTION`.
642+
643+
- `XOPTION`: Removes all instances of the literal argument `OPTION`, and the
644+
following argument.
645+
646+
- `Ox`: Removes all flags matching `O` or `O[sz0-9]` and adds `Ox` at the end
647+
of the command line.
648+
649+
This environment variable does not affect the options added by the config files.

flang/test/Driver/Inputs/config-7.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Werror

flang/test/Driver/fcc_override.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: env FCC_OVERRIDE_OPTIONS="#+-Os +-Oz +-O +-O3 +-Oignore +a +b +c xb Xa Omagic ^-### " %flang --target=x86_64-unknown-linux-gnu %s -O2 b -O3 2>&1 | FileCheck %s
2+
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror +-g" %flang --target=x86_64-unknown-linux-gnu -Werror %s -c -### 2>&1 | FileCheck %s -check-prefix=RM-WERROR
3+
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror" %flang --config=%S/Inputs/config-7.cfg -### %s -c 2>&1 | FileCheck %s -check-prefix=CONF
4+
5+
! CHECK: "-fc1"
6+
! CHECK-NOT: "-Oignore"
7+
! CHECK: "-Omagic"
8+
! CHECK-NOT: "-Oignore"
9+
10+
! RM-WERROR: ### FCC_OVERRIDE_OPTIONS: x-Werror +-g
11+
! RM-WERROR-NEXT: ### Deleting argument -Werror
12+
! RM-WERROR-NEXT: ### Adding argument -g at end
13+
! RM-WERROR-NOT: "-Werror"
14+
15+
! Test that FCC_OVERRIDE_OPTIONS does not affect the options from config files.
16+
! CONF: ### FCC_OVERRIDE_OPTIONS: x-Werror
17+
! CONF: "-Werror"

flang/tools/flang-driver/driver.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ int main(int argc, const char **argv) {
110110
}
111111
}
112112

113+
llvm::StringSet<> savedStrings;
114+
// Handle FCC_OVERRIDE_OPTIONS, used for editing a command line behind the
115+
// scenes.
116+
if (const char *overrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
117+
clang::driver::applyOverrideOptions(args, overrideStr, savedStrings,
118+
"FCC_OVERRIDE_OPTIONS", &llvm::errs());
119+
113120
// Not in the frontend mode - continue in the compiler driver mode.
114121

115122
// Create DiagnosticsEngine for the compiler driver

0 commit comments

Comments
 (0)