[flang-commits] [flang] 625facd - [Flang] Store only options in FLANG_COMPILER_OPTIONS_STRING (#201278)

via flang-commits flang-commits at lists.llvm.org
Sun Jun 14 20:21:29 PDT 2026


Author: Thirumalai Shaktivel
Date: 2026-06-15T08:51:24+05:30
New Revision: 625facd4375f6bfa5de501d0559bd262062e2dc3

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

LOG: [Flang] Store only options in FLANG_COMPILER_OPTIONS_STRING (#201278)

Previously, FLANG_COMPILER_OPTIONS_STRING stored every argument passed
to the flang driver, including input file names. The GNU extension
compiler_options() is documented to return only the options, not the
input files. Including the input files also caused the string to exceed
ARG_MAX on large builds, producing:

    posix_spawn failed: Argument list too long

Use the driver's parsed InputArgList to filter out OPT_INPUT arguments,
preserving all options and their values (e.g. `-I /path`, `-o file`).

Fixes: https://github.com/llvm/llvm-project/issues/170651

Added: 
    

Modified: 
    flang/test/Driver/compiler-options.f90
    flang/tools/flang-driver/driver.cpp

Removed: 
    


################################################################################
diff  --git a/flang/test/Driver/compiler-options.f90 b/flang/test/Driver/compiler-options.f90
index b9ecbbda227d1..92efbc0044b35 100644
--- a/flang/test/Driver/compiler-options.f90
+++ b/flang/test/Driver/compiler-options.f90
@@ -1,6 +1,6 @@
 ! RUN: %flang -S -emit-llvm -o - %s | FileCheck %s
 ! Test communication of COMPILER_OPTIONS from flang to flang -fc1.
-! CHECK: [[OPTSVAR:@_QQclX[0-9a-f]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"{{.*}}flang{{(\.exe)?}} {{.*}}-S -emit-llvm -o - {{.*}}compiler-options.f90"
+! CHECK: [[OPTSVAR:@_QQclX[0-9A-Fa-f]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"-S -emit-llvm -o -"
 program main
     use ISO_FORTRAN_ENV, only: compiler_options
     implicit none

diff  --git a/flang/tools/flang-driver/driver.cpp b/flang/tools/flang-driver/driver.cpp
index 1ce50e0e49cd6..2e301e0daaba8 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -23,6 +23,7 @@
 #include "clang/Basic/DiagnosticIDs.h"
 #include "clang/Basic/DiagnosticOptions.h"
 #include "clang/Driver/Compilation.h"
+#include "clang/Options/Options.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/Option/ArgList.h"
@@ -174,22 +175,29 @@ int main(int argc, const char **argv) {
   if (rejectAssemblyInputs(c->getInputArgs(), diags))
     return 1;
 
-  // Set the environment variable, FLANG_COMPILER_OPTIONS_STRING, to contain all
-  // the compiler options. This is intended for the frontend driver,
-  // flang -fc1, to enable the implementation of the COMPILER_OPTIONS
-  // intrinsic. To this end, the frontend driver requires the list of the
-  // original compiler options, which is not available through other means.
+  // Set the environment variable, FLANG_COMPILER_OPTIONS_STRING, to contain
+  // the compiler options (excluding input file names and the program name).
+  // This is intended for the frontend driver, flang -fc1, to enable the
+  // implementation of the COMPILER_OPTIONS intrinsic. To this end, the
+  // frontend driver requires the list of the original compiler options,
+  // which is not available through other means.
   // TODO: This way of passing information between the compiler and frontend
   // drivers is discouraged. We should find a better way not involving env
   // variables.
   std::string compilerOptsGathered;
   llvm::raw_string_ostream os(compilerOptsGathered);
-  for (int i = 0; i < argc; ++i) {
-    os << argv[i];
-    if (i < argc - 1) {
-      os << ' ';
+  const llvm::opt::InputArgList &argList = c->getInputArgs();
+  bool first = true;
+  for (const llvm::opt::Arg *arg : argList) {
+    if (!arg->getOption().matches(clang::options::OPT_INPUT)) {
+      if (!first) {
+        os << ' ';
+      }
+      os << arg->getAsString(argList);
+      first = false;
     }
   }
+
 #ifdef _WIN32
   _putenv_s("FLANG_COMPILER_OPTIONS_STRING", compilerOptsGathered.c_str());
 #else


        


More information about the flang-commits mailing list