[clang] cc1: Report an error for multiple actions unless separated by -main-file-name (PR #91140)
Fangrui Song via cfe-commits
cfe-commits at lists.llvm.org
Mon May 6 11:01:11 PDT 2024
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/91140
>From c8677337fbcc6a1456466e65dd847f9317f16a4f Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 5 May 2024 13:06:36 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.5-bogner
---
.../clang/Basic/DiagnosticFrontendKinds.td | 2 ++
clang/lib/Frontend/CompilerInvocation.cpp | 24 +++++++++++++++++++
clang/test/Frontend/multiple-actions.c | 7 ++++++
3 files changed, 33 insertions(+)
create mode 100644 clang/test/Frontend/multiple-actions.c
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index fcffadacc8e631..48c174cfa9e8a5 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -134,6 +134,8 @@ def err_fe_no_pch_in_dir : Error<
"no suitable precompiled header file found in directory '%0'">;
def err_fe_action_not_available : Error<
"action %0 not compiled in">;
+def err_fe_invalid_multiple_actions : Error<
+ "action %0 is specified, another action is not allowed: %1">;
def err_fe_invalid_alignment : Error<
"invalid value '%1' in '%0'; alignment must be a power of 2">;
def err_fe_invalid_exception_model
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..4a4c02b03ce938 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -2841,6 +2841,30 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
}
Opts.ProgramAction = *ProgramAction;
+
+ // Catch common mistakes when multiple actions are specified for cc1 (e.g.
+ // -S -emit-llvm means -emit-llvm while -emit-llvm -S means -S). However, to
+ // support driver `-c -Xclang ACTION` (-cc1 -emit-llvm file -main-file-name
+ // X ACTION), we suppress the error when the two actions are separated by
+ // -main-file-name.
+ //
+ // As an exception, accept composable -ast-dump*.
+ if (!A->getSpelling().starts_with("-ast-dump")) {
+ const Arg *SavedAction = nullptr;
+ for (const Arg *AA :
+ Args.filtered(OPT_Action_Group, OPT_main_file_name)) {
+ if (AA->getOption().matches(OPT_main_file_name)) {
+ SavedAction = nullptr;
+ } else if (!SavedAction) {
+ SavedAction = AA;
+ } else {
+ if (!A->getOption().matches(OPT_ast_dump_EQ))
+ Diags.Report(diag::err_fe_invalid_multiple_actions)
+ << A->getSpelling() << SavedAction->getSpelling();
+ break;
+ }
+ }
+ }
}
if (const Arg* A = Args.getLastArg(OPT_plugin)) {
diff --git a/clang/test/Frontend/multiple-actions.c b/clang/test/Frontend/multiple-actions.c
new file mode 100644
index 00000000000000..5dd9ac3c8754f7
--- /dev/null
+++ b/clang/test/Frontend/multiple-actions.c
@@ -0,0 +1,7 @@
+// RUN: not %clang_cc1 -S -emit-llvm -main-file-name %s 2>&1 | FileCheck %s --check-prefix=ERR1 --implicit-check-not=error:
+// ERR1: error: action -emit-llvm is specified, another action is not allowed: -S
+
+// RUN: not %clang_cc1 -main-file-name %s -emit-llvm-only -emit-llvm -S 2>&1 | FileCheck %s --check-prefix=ERR2 --implicit-check-not=error:
+// ERR2: error: action -S is specified, another action is not allowed: -emit-llvm-only
+
+// RUN: %clang_cc1 -S -main-file-name %s -emit-llvm -o /dev/null
More information about the cfe-commits
mailing list