diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -107,6 +107,14 @@ // By default the frontend driver creates a ParseSyntaxOnly action. opts.programAction = ParseSyntaxOnly; + // Treat multiple action options as an invocation error. + 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); + return false; + } + // Identify the action (i.e. opts.ProgramAction) if (const llvm::opt::Arg *a = args.getLastArg(clang::driver::options::OPT_Action_Group)) { diff --git a/flang/test/Driver/multiple-actions-error.f95 b/flang/test/Driver/multiple-actions-error.f95 new file mode 100644 --- /dev/null +++ b/flang/test/Driver/multiple-actions-error.f95 @@ -0,0 +1,8 @@ +! 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 diff --git a/llvm/include/llvm/Option/ArgList.h b/llvm/include/llvm/Option/ArgList.h --- a/llvm/include/llvm/Option/ArgList.h +++ b/llvm/include/llvm/Option/ArgList.h @@ -245,6 +245,12 @@ return getLastArg(Ids...) != nullptr; } + /// Return true if the arg list contains multiple arguments matching \p Id. + bool hasMultipleArgs(OptSpecifier Id) const { + auto Args = filtered(Id); + return (++Args.begin()) != Args.end(); + } + /// Return the last argument matching \p Id, or null. template Arg *getLastArg(OptSpecifiers ...Ids) const {