Skip to content

Conversation

@tarunprabhu
Copy link
Contributor

When an option that is only available in flang -fc1 is provided to flang, emit a diagnostic with a suggestion containing "did you mean -Xflang '-foo'".

Partially addresses #163550.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Oct 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2025

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-flang-driver

@llvm/pr-subscribers-clang-driver

Author: Tarun Prabhu (tarunprabhu)

Changes

When an option that is only available in flang -fc1 is provided to flang, emit a diagnostic with a suggestion containing "did you mean -Xflang '-foo'".

Partially addresses #163550.


Full diff: https://github.com/llvm/llvm-project/pull/164741.diff

2 Files Affected:

  • (modified) clang/lib/Driver/Driver.cpp (+10-1)
  • (modified) flang/test/Driver/flang-f-opts.f90 (+19-2)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 40ea513e85427..afaf6c2691ac6 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -308,7 +308,16 @@ InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
     auto ArgString = A->getAsString(Args);
     std::string Nearest;
     if (getOpts().findNearest(ArgString, Nearest, VisibilityMask) > 1) {
-      if (!IsCLMode() &&
+      if (IsFlangMode()) {
+        if (getOpts().findExact(ArgString, Nearest,
+                                llvm::opt::Visibility(options::FC1Option))) {
+          DiagID = diag::err_drv_unknown_argument_with_suggestion;
+          Diags.Report(DiagID) << ArgString << "-Xflang " + Nearest;
+        } else {
+          DiagID = diag::err_drv_unknown_argument;
+          Diags.Report(DiagID) << ArgString;
+        }
+      } else if (!IsCLMode() &&
           getOpts().findExact(ArgString, Nearest,
                               llvm::opt::Visibility(options::CC1Option))) {
         DiagID = diag::err_drv_unknown_argument_with_suggestion;
diff --git a/flang/test/Driver/flang-f-opts.f90 b/flang/test/Driver/flang-f-opts.f90
index b972b9b7b2a59..6720c4e534fdb 100644
--- a/flang/test/Driver/flang-f-opts.f90
+++ b/flang/test/Driver/flang-f-opts.f90
@@ -1,5 +1,5 @@
-! Test for warnings generated when parsing driver options. You can use this file for relatively small tests and to avoid creating
-! new test files.
+! Test for errors and warnings generated when parsing driver options. You can
+! use this file for relatively small tests and to avoid creating new test files.
 
 ! RUN: %flang -### -S -O4 -ffp-contract=on %s 2>&1 | FileCheck %s
 
@@ -13,3 +13,20 @@
 ! CHECK-PROFILE-GENERATE-LLVM: "-fprofile-generate"
 ! RUN: %flang -### -S -fprofile-use=%S %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-USE-DIR %s
 ! CHECK-PROFILE-USE-DIR: "-fprofile-use={{.*}}"
+
+! When issuing a warning with a suggestion, ensure that the suggestion uses
+! "-Xflang" in the diagnostic message. This is typically issued when an option
+! that is available for `flang -fc1` is passed to `flang`. We use -complex-range
+! since it is only available for fc1. If this option is ever exposed to `flang`,
+! a different option will have to be used in the test below.
+!
+! RUN: not %flang -complex-range=full %s 2>&1 \
+! RUN:     | FileCheck %s -check-prefix UNKNOWN-SUGGEST
+!
+! UNKNOWN-SUGGEST: error: unknown argument '-complex-range=full';
+! UNKNOWN-SUGGEST-SAME: did you mean '-Xflang -complex-range=full'
+!
+! RUN: not %flang -not-an-option %s 2>&1 \
+! RUN:     | FileCheck %s -check-prefix UNKNOWN-NO-SUGGEST
+!
+! UNKNOWN-NO-SUGGEST: error: unknown argument: '-not-an-option'{{$}}

@github-actions
Copy link

github-actions bot commented Oct 23, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Tarun

@tarunprabhu tarunprabhu force-pushed the use-xflang-in-diagnostics branch 4 times, most recently from bcf78f6 to abc5c77 Compare October 28, 2025 18:55
Tarun Prabhu added 3 commits October 29, 2025 08:18
When an option that is only available in `flang -fc1` is provided to `flang`,
emit a diagnostic with a suggestion containing "did you mean -Xflang '-foo'".

Partially addresses llvm#163550.
@tarunprabhu tarunprabhu force-pushed the use-xflang-in-diagnostics branch from abc5c77 to e7d7243 Compare October 29, 2025 14:25
@tarunprabhu tarunprabhu merged commit 9ab9d33 into llvm:main Oct 29, 2025
10 checks passed
@tarunprabhu tarunprabhu deleted the use-xflang-in-diagnostics branch October 29, 2025 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants