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 @@ -84,6 +84,10 @@ static InputKind ParseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) { + + // By default the frontend driver creates a ParseSyntaxOnly action. + opts.programAction_ = ParseSyntaxOnly; + // 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/lib/Frontend/FrontendAction.cpp b/flang/lib/Frontend/FrontendAction.cpp --- a/flang/lib/Frontend/FrontendAction.cpp +++ b/flang/lib/Frontend/FrontendAction.cpp @@ -11,7 +11,9 @@ #include "flang/Frontend/FrontendActions.h" #include "flang/Frontend/FrontendOptions.h" #include "flang/FrontendTool/Utils.h" +#include "clang/Basic/DiagnosticFrontend.h" #include "llvm/Support/Errc.h" +#include "llvm/Support/VirtualFileSystem.h" using namespace Fortran::frontend; @@ -31,6 +33,18 @@ CompilerInstance &ci, const FrontendInputFile &realInput) { FrontendInputFile input(realInput); + + // Return immediately if the input file does not exist. Note that we cannot + // check this for input from stdin. + if (input.file() != "-") { + if (!llvm::vfs::getRealFileSystem()->exists(input.file())) { + ci.diagnostics().Report(clang::diag::err_fe_error_reading) + << input.file(); + BeginSourceFileCleanUp(*this, ci); + return false; + } + } + assert(!instance_ && "Already processing a source file!"); assert(!realInput.IsEmpty() && "Unexpected empty filename!"); set_currentInput(realInput); diff --git a/flang/test/Flang-Driver/missing-input.f90 b/flang/test/Flang-Driver/missing-input.f90 --- a/flang/test/Flang-Driver/missing-input.f90 +++ b/flang/test/Flang-Driver/missing-input.f90 @@ -1,5 +1,28 @@ -! RUN: not %flang-new 2>&1 | FileCheck %s - ! REQUIRES: new-flang-driver -! CHECK: flang-new: error: no input files +! Test the behaviour when input is missing. Note that with the compiler driver, +! the input _has_ to be specified. Indeed, the driver decides what to do based +! on the file extension. No input file means that it doesn't know what to do +! (compile? preprocess? link?). Whereas the frontend driver simply assumes +! that "no explicit input == read from stdin" + +!-------------------------- +! FLANG DRIVER (flang-new) +!-------------------------- +! RUN: not %flang-new 2>&1 | FileCheck %s --check-prefix=FLANG-NO-FILE +! RUN: not %flang-new %t 2>&1 | FileCheck %s --check-prefix=FLANG-NOEXISITANT-FILE + +!----------------------------------------- +! FLANG FRONTEND DRIVER (flang-new -fc1) +!----------------------------------------- +! RUN: not %flang-new -fc1 %t 2>&1 | FileCheck %s --check-prefix=FLANG-FC1-NOEXISITANT-FILE + +!----------------------- +! EXPECTED OUTPUT +!----------------------- +! FLANG-NO-FILE: flang-new: error: no input files + +! FLANG-NOEXISITANT-FILE: flang-new: error: no such file or directory: {{.*}} +! FLANG-NOEXISITANT-FILE: flang-new: error: no input files + +! FLANG-FC1-NOEXISITANT-FILE: error: error reading {{.*}}