Author: Abid Qadeer
Date: 2025-06-02T11:37:26+01:00
New Revision: de38c4eaf52717f152b41f285403e6006783f39c

URL: 
https://github.com/llvm/llvm-project/commit/de38c4eaf52717f152b41f285403e6006783f39c
DIFF: 
https://github.com/llvm/llvm-project/commit/de38c4eaf52717f152b41f285403e6006783f39c.diff

LOG: [flang][driver] Introduce FCC_OVERRIDE_OPTIONS. (#140556)

This PR add functionality to change `flang` command line using
environment variable `FCC_OVERRIDE_OPTIONS`. It is quite similar to what
`CCC_OVERRIDE_OPTIONS` does for clang.

The `FCC_OVERRIDE_OPTIONS` seemed like the most obvious name to me but I
am open to other ideas. The `applyOverrideOptions` now takes an extra
argument that is the name of the environment variable. Previously
`CCC_OVERRIDE_OPTIONS` was hardcoded.

Added: 
    flang/test/Driver/Inputs/config-7.cfg
    flang/test/Driver/fcc_override.f90

Modified: 
    clang/include/clang/Driver/Driver.h
    clang/lib/Driver/Driver.cpp
    clang/tools/driver/driver.cpp
    flang/docs/FlangDriver.md
    flang/tools/flang-driver/driver.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Driver.h 
b/clang/include/clang/Driver/Driver.h
index b463dc2a93550..7ca848f11b561 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -879,7 +879,7 @@ llvm::Error expandResponseFiles(SmallVectorImpl<const char 
*> &Args,
 /// See applyOneOverrideOption.
 void applyOverrideOptions(SmallVectorImpl<const char *> &Args,
                           const char *OverrideOpts,
-                          llvm::StringSet<> &SavedStrings,
+                          llvm::StringSet<> &SavedStrings, StringRef EnvVar,
                           raw_ostream *OS = nullptr);
 
 } // end namespace driver

diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 6aa0f4c9b1584..87c827de17d9e 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -7204,9 +7204,10 @@ static const char *GetStableCStr(llvm::StringSet<> 
&SavedStrings, StringRef S) {
 ///
 ///  '#': Silence information about the changes to the command line arguments.
 ///
-///  '^': Add FOO as a new argument at the beginning of the command line.
+///  '^FOO': Add FOO as a new argument at the beginning of the command line
+///  right after the name of the compiler executable.
 ///
-///  '+': Add FOO as a new argument at the end of the command line.
+///  '+FOO': Add FOO as a new argument at the end of the command line.
 ///
 ///  's/XXX/YYY/': Substitute the regular expression XXX with YYY in the 
command
 ///  line.
@@ -7294,7 +7295,7 @@ static void applyOneOverrideOption(raw_ostream &OS,
 void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
                                   const char *OverrideStr,
                                   llvm::StringSet<> &SavedStrings,
-                                  raw_ostream *OS) {
+                                  StringRef EnvVar, raw_ostream *OS) {
   if (!OS)
     OS = &llvm::nulls();
 
@@ -7303,7 +7304,7 @@ void driver::applyOverrideOptions(SmallVectorImpl<const 
char *> &Args,
     OS = &llvm::nulls();
   }
 
-  *OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
+  *OS << "### " << EnvVar << ": " << OverrideStr << "\n";
 
   // This does not need to be efficient.
 

diff  --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index d21a34e961394..9ed8d4ed87612 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -305,7 +305,7 @@ int clang_main(int Argc, char **Argv, const 
llvm::ToolContext &ToolContext) {
   if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
     // FIXME: Driver shouldn't take extra initial argument.
     driver::applyOverrideOptions(Args, OverrideStr, SavedStrings,
-                                 &llvm::errs());
+                                 "CCC_OVERRIDE_OPTIONS", &llvm::errs());
   }
 
   std::string Path = GetExecutablePath(ToolContext.Path, CanonicalPrefixes);

diff  --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md
index 97744f0bee069..f246163d3ce66 100644
--- a/flang/docs/FlangDriver.md
+++ b/flang/docs/FlangDriver.md
@@ -614,3 +614,31 @@ nvfortran defines `-fast` as
  - `-Mcache_align`: there is no equivalent flag in Flang or Clang.
  - `-Mflushz`: flush-to-zero mode - when `-ffast-math` is specified, Flang will
    link to `crtfastmath.o` to ensure denormal numbers are flushed to zero.
+
+
+## FCC_OVERRIDE_OPTIONS
+
+The environment variable `FCC_OVERRIDE_OPTIONS` can be used to edit flang's
+command line arguments. The value of this variable is a space-separated list of
+edits to perform. The edits are applied in the order in which they appear in
+`FCC_OVERRIDE_OPTIONS`. Each edit should be one of the following form:
+
+- `#`: Silence information about the changes to the command line arguments.
+
+- `^FOO`: Add `FOO` as a new argument at the beginning of the command line 
right
+  after the name of the compiler executable.
+
+- `+FOO`: Add `FOO` as a new argument at the end of the command line.
+
+- `s/XXX/YYY/`: Substitute the regular expression `XXX` with `YYY` in the
+  command line.
+
+- `xOPTION`: Removes all instances of the literal argument `OPTION`.
+
+- `XOPTION`: Removes all instances of the literal argument `OPTION`, and the
+  following argument.
+
+- `Ox`: Removes all flags matching `O` or `O[sz0-9]` and adds `Ox` at the end
+  of the command line.
+
+This environment variable does not affect the options added by the config 
files.

diff  --git a/flang/test/Driver/Inputs/config-7.cfg 
b/flang/test/Driver/Inputs/config-7.cfg
new file mode 100644
index 0000000000000..2f41be663b282
--- /dev/null
+++ b/flang/test/Driver/Inputs/config-7.cfg
@@ -0,0 +1 @@
+-Werror

diff  --git a/flang/test/Driver/fcc_override.f90 
b/flang/test/Driver/fcc_override.f90
new file mode 100644
index 0000000000000..2717d203c2ea3
--- /dev/null
+++ b/flang/test/Driver/fcc_override.f90
@@ -0,0 +1,17 @@
+! 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
+! 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
+! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror" %flang 
--config=%S/Inputs/config-7.cfg -### %s -c  2>&1 | FileCheck %s 
-check-prefix=CONF
+
+! CHECK: "-fc1"
+! CHECK-NOT: "-Oignore"
+! CHECK: "-Omagic"
+! CHECK-NOT: "-Oignore"
+
+! RM-WERROR: ### FCC_OVERRIDE_OPTIONS: x-Werror +-g
+! RM-WERROR-NEXT: ### Deleting argument -Werror
+! RM-WERROR-NEXT: ### Adding argument -g at end
+! RM-WERROR-NOT: "-Werror"
+
+! Test that FCC_OVERRIDE_OPTIONS does not affect the options from config files.
+! CONF: ### FCC_OVERRIDE_OPTIONS: x-Werror
+! CONF: "-Werror"

diff  --git a/flang/tools/flang-driver/driver.cpp 
b/flang/tools/flang-driver/driver.cpp
index 35cc2efc0ac01..3a2dffc66428f 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -111,6 +111,13 @@ int main(int argc, const char **argv) {
     }
   }
 
+  llvm::StringSet<> savedStrings;
+  // Handle FCC_OVERRIDE_OPTIONS, used for editing a command line behind the
+  // scenes.
+  if (const char *overrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
+    clang::driver::applyOverrideOptions(args, overrideStr, savedStrings,
+                                        "FCC_OVERRIDE_OPTIONS", &llvm::errs());
+
   // Not in the frontend mode - continue in the compiler driver mode.
 
   // Create DiagnosticsEngine for the compiler driver


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to