[clang] [flang] [flang][Driver] Better error message when multiple actions are specified (PR #165575)
Tarun Prabhu via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 29 09:05:31 PDT 2025
https://github.com/tarunprabhu updated https://github.com/llvm/llvm-project/pull/165575
>From 637eea7ea9bdfac5be5fa7b6937e5bab1ed430aa Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun.prabhu at gmail.com>
Date: Tue, 28 Oct 2025 21:32:22 -0600
Subject: [PATCH 1/2] [flang][Driver] Better error message when multiple
actions are specified
List all the action options that were provided in the error message emitted when
multiple actions are specified
Fixes #79458
---
.../clang/Basic/DiagnosticDriverKinds.td | 2 +
flang/lib/Frontend/CompilerInvocation.cpp | 15 ++++++--
flang/test/Driver/multiple-actions-error.f95 | 38 +++++++++++++++----
3 files changed, 44 insertions(+), 11 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 83980e3ac35b7..2fefe1dbe0dd6 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -312,6 +312,8 @@ def warn_drv_yc_multiple_inputs_clang_cl : Warning<
def warn_drv_potentially_misspelled_joined_argument : Warning<
"joined argument treated as '%0'; did you mean '%1'?">, InGroup<UnknownArgument>;
+def err_drv_too_many_actions: Error<
+ "only one action option is allowed. Got '%0'">;
def err_drv_invalid_value : Error<"invalid value '%1' in '%0'">;
def err_drv_invalid_int_value : Error<"invalid integral value '%1' in '%0'">;
def err_drv_invalid_value_with_suggestion : Error<
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 548ca675db5ea..d7bdbc121274d 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -595,9 +595,18 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
// -cc1` does accept multiple action options, but will only consider the
// rightmost one.
if (args.hasMultipleArgs(clang::driver::options::OPT_Action_Group)) {
- const unsigned diagID = diags.getCustomDiagID(
- clang::DiagnosticsEngine::Error, "Only one action option is allowed");
- diags.Report(diagID);
+ llvm::SmallString<32> buf;
+ llvm::raw_svector_ostream os(buf);
+ for (const llvm::opt::Arg *arg :
+ args.filtered(clang::driver::options::OPT_Action_Group)) {
+ if (buf.size())
+ os << ", ";
+ os << "'" << arg->getSpelling() << "'";
+ }
+ // The diagnostics engine will add single quotes around the argument to the
+ // error message. Strip the leading and trailing quote from here.
+ diags.Report(clang::diag::err_drv_too_many_actions)
+ << buf.substr(1, buf.size() - 2);
return false;
}
diff --git a/flang/test/Driver/multiple-actions-error.f95 b/flang/test/Driver/multiple-actions-error.f95
index 5ec4e9166657f..3b2b7dc26d2c6 100644
--- a/flang/test/Driver/multiple-actions-error.f95
+++ b/flang/test/Driver/multiple-actions-error.f95
@@ -1,8 +1,30 @@
-! Verify that the frontend driver error-out if multiple actions are specified
-
-! RUN: not %flang_fc1 -E -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix=ERROR
-! RUN: not %flang_fc1 -fsyntax-only -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix=ERROR
-
-! ERROR: error: Only one action option is allowed
-
-end progream
+! Verify that the frontend driver raises the expected error when multiple
+! actions are specified.
+!
+! RUN: not %flang_fc1 -fsyntax-only -fsyntax-only %s 2>&1 \
+! RUN: | FileCheck %s --check-prefixes=ERROR,ACTIONS-1
+!
+! RUN: not %flang_fc1 -E -fsyntax-only %s 2>&1 \
+! RUN: | FileCheck %s --check-prefixes=ERROR,ACTIONS-2
+!
+! RUN: not %flang_fc1 -fsyntax-only -E -emit-llvm %s 2>&1 \
+! RUN: | FileCheck %s --check-prefixes=ERROR,ACTIONS-3
+!
+! If one or more options are specified with -Xflang, they will appear last in
+! the error message.
+!
+! RUN: not %flang -S -Xflang -emit-llvm %s 2>&1 \
+! RUN: | FileCheck %s --check-prefixes=ERROR,ACTIONS-4
+!
+! RUN: not %flang -Xflang -emit-llvm -S %s 2>&1 \
+! RUN: | FileCheck %s --check-prefixes=ERROR,ACTIONS-4
+!
+! RUN: not %flang -Xflang -emit-obj -S -Xflang -emit-llvm %s 2>&1 \
+! RUN: | FileCheck %s --check-prefixes=ERROR,ACTIONS-5
+!
+! ERROR: error: only one action option is allowed.
+! ACTIONS-1: Got '-fsyntax-only', '-fsyntax-only'
+! ACTIONS-2: Got '-E', '-fsyntax-only'
+! ACTIONS-3: Got '-fsyntax-only', '-E', '-emit-llvm'
+! ACTIONS-4: Got '-S', '-emit-llvm'
+! ACTIONS-5: Got '-S', '-emit-obj', '-emit-llvm'
>From 11ffb4adbde7668071d920e413a0b4c1ae3d51f0 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Wed, 29 Oct 2025 10:05:20 -0600
Subject: [PATCH 2/2] Remove quotes from diagnostic template instead of
stripping it from the constructed argument
---
clang/include/clang/Basic/DiagnosticDriverKinds.td | 2 +-
flang/lib/Frontend/CompilerInvocation.cpp | 3 +--
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 2fefe1dbe0dd6..afd44a110bc74 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -313,7 +313,7 @@ def warn_drv_potentially_misspelled_joined_argument : Warning<
"joined argument treated as '%0'; did you mean '%1'?">, InGroup<UnknownArgument>;
def err_drv_too_many_actions: Error<
- "only one action option is allowed. Got '%0'">;
+ "only one action option is allowed. Got %0">;
def err_drv_invalid_value : Error<"invalid value '%1' in '%0'">;
def err_drv_invalid_int_value : Error<"invalid integral value '%1' in '%0'">;
def err_drv_invalid_value_with_suggestion : Error<
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index d7bdbc121274d..8a72299677699 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -605,8 +605,7 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
}
// The diagnostics engine will add single quotes around the argument to the
// error message. Strip the leading and trailing quote from here.
- diags.Report(clang::diag::err_drv_too_many_actions)
- << buf.substr(1, buf.size() - 2);
+ diags.Report(clang::diag::err_drv_too_many_actions) << buf;
return false;
}
More information about the cfe-commits
mailing list