
This patch introduces the dependencies required to read and manage input files provided by the command line option. It also adds the infrastructure to create and write to output files. The output is sent to either stdout or a file (specified with the `-o` flag). Separately, in order to be able to test the code for file I/O, it adds infrastructure to create frontend actions. As a basic testable example, it adds the `InputOutputTest` FrontendAction. The sole purpose of this action is to read a file from the command line and print it either to stdout or the output file. This action is run by using the `-test-io` flag also introduced in this patch (available for `flang-new` and `flang-new -fc1`). With this patch: ``` flang-new -test-io input-file.f90 ``` will read input-file.f90 and print it in the output file. The `InputOutputTest` frontend action has been introduced primarily to facilitate testing. It is hidden from users (i.e. it's only displayed with `--help-hidden`). Currently Clang doesn’t have an equivalent action. `-test-io` is used to trigger the InputOutputTest action in the Flang frontend driver. This patch makes sure that “flang-new” forwards it to “flang-new -fc1" by creating a preprocessor job. However, in Flang.cpp, `-test-io` is passed to “flang-new -fc1” without `-E`. This way we make sure that the preprocessor is _not_ run in the frontend driver. This is the desired behaviour: `-test-io` should only read the input file and print it to the output stream. co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com> Differential Revision: https://reviews.llvm.org/D87989
80 lines
2.3 KiB
C++
80 lines
2.3 KiB
C++
//===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file holds ExecuteCompilerInvocation(). It is split into its own file to
|
|
// minimize the impact of pulling in essentially everything else in Flang.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "flang/Frontend/CompilerInstance.h"
|
|
#include "flang/Frontend/FrontendActions.h"
|
|
#include "clang/Driver/Options.h"
|
|
#include "llvm/Option/OptTable.h"
|
|
#include "llvm/Option/Option.h"
|
|
#include "llvm/Support/BuryPointer.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
namespace Fortran::frontend {
|
|
|
|
static std::unique_ptr<FrontendAction> CreateFrontendBaseAction(
|
|
CompilerInstance &ci) {
|
|
|
|
ActionKind ak = ci.GetFrontendOpts().programAction_;
|
|
switch (ak) {
|
|
case InputOutputTest:
|
|
return std::make_unique<InputOutputTestAction>();
|
|
break;
|
|
default:
|
|
break;
|
|
// TODO:
|
|
// case RunPreprocessor:
|
|
// case ParserSyntaxOnly:
|
|
// case EmitLLVM:
|
|
// case EmitLLVMOnly:
|
|
// case EmitCodeGenOnly:
|
|
// (...)
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
std::unique_ptr<FrontendAction> CreateFrontendAction(CompilerInstance &ci) {
|
|
// Create the underlying action.
|
|
std::unique_ptr<FrontendAction> act = CreateFrontendBaseAction(ci);
|
|
if (!act)
|
|
return nullptr;
|
|
|
|
return act;
|
|
}
|
|
bool ExecuteCompilerInvocation(CompilerInstance *flang) {
|
|
// Honor -help.
|
|
if (flang->GetFrontendOpts().showHelp_) {
|
|
clang::driver::getDriverOptTable().PrintHelp(llvm::outs(),
|
|
"flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler",
|
|
/*Include=*/clang::driver::options::FC1Option,
|
|
/*Exclude=*/llvm::opt::DriverFlag::HelpHidden,
|
|
/*ShowAllAliases=*/false);
|
|
return true;
|
|
}
|
|
|
|
// Honor -version.
|
|
if (flang->GetFrontendOpts().showVersion_) {
|
|
llvm::cl::PrintVersionMessage();
|
|
return true;
|
|
}
|
|
|
|
// Create and execute the frontend action.
|
|
std::unique_ptr<FrontendAction> act(CreateFrontendAction(*flang));
|
|
if (!act)
|
|
return false;
|
|
|
|
bool success = flang->ExecuteAction(*act);
|
|
return success;
|
|
}
|
|
|
|
} // namespace Fortran::frontend
|