From e74a7a9fd79a74073277471243a44527c71eb4a9 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 7 May 2024 09:15:52 -0700 Subject: cc1: Report an error for multiple actions unless separated by -main-file-name (#91140) When multiple actions are specified, the last one is used and others are overridden. This might lead to confusion if the user is used to driver's `-S -emit-llvm` behavior. ``` %clang_cc1 -S -emit-llvm a.c # -S is overridden %clang_cc1 -emit-llvm -S a.c # -emit-llvm is overridden %clang_cc1 -fsyntax-only -S a.c # -fsyntax-only is overridden ``` However, we want to continue supporting overriding the driver action with -Xclang: * `clang -c -Xclang -ast-dump a.c` (`%clang -cc1 -emit-obj ... -main-file-name a.c ... -ast-dump`) * `clang -c -xc++ -Xclang -emit-module stl.modulemap` As an exception, we allow -ast-dump* options to be composed together (e.g. `-ast-dump -ast-dump-lookups` in AST/ast-dump-lookups.cpp). --- clang/lib/Frontend/CompilerInvocation.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'clang/lib/Frontend/CompilerInvocation.cpp') diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 8312abc..948fe08 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) + << SavedAction->getSpelling() << A->getSpelling(); + break; + } + } + } } if (const Arg* A = Args.getLastArg(OPT_plugin)) { -- cgit v1.1