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

Tarun Prabhu via flang-commits flang-commits at lists.llvm.org
Wed Jun 3 03:45:44 PDT 2026


================
@@ -174,22 +175,28 @@ 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) {
+  const llvm::opt::InputArgList &argList = c->getInputArgs();
+  bool first = true;
+  for (const auto *arg : argList) {
+    if (arg->getOption().matches(clang::options::OPT_INPUT))
+      continue;
+    if (!first)
       os << ' ';
-    }
+    os << arg->getAsString(argList);
+    first = false;
----------------
tarunprabhu wrote:

Unlike LLVM, flang requires braces around all conditionals, even those with a single statement. Since the body of the loop is short, you could consider inverting the condition here


```suggestion
    if (!arg->getOption().matches(clang::options::OPT_INPUT)) {
      if (!first) {
        os << ' ';
      }
      os << arg->getAsString(argList);
      first = false;
    }
```

https://github.com/llvm/llvm-project/pull/201278


More information about the flang-commits mailing list