llvm-project/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Caroline Concatto d28de0d7f2 [Flang][Driver] Add PrintPreprocessedInput FrontendAction (flang-new -E)
This patch implements the first frontend action for the Flang parser (i.e.
Fortran::parser). This action runs the preprocessor and is invoked with the
`-E` flag. (i.e. `flang-new -E <input-file>). The generated output is printed
to either stdout or the output file (specified with `-` or `-o <output-file>`).

Note that currently there is no mechanism to map options for the
frontend driver (i.e. Fortran::frontend::FrontendOptions) to options for
the parser (i.e. Fortran::parser::Options). Instead,
Frotran::parser::options are hard-coded to:

```
std::vector<std::string> searchDirectories{"."s};
searchDirectories = searchDirectories;
isFixedForm = false;
_encoding(Fortran::parser::Encoding::UTF_8);
```

These default settings are compatible with the current Flang driver. Further
work is required in order for CompilerInvocation to read and map
clang::driver::options to Fortran::parser::options.

Co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com>

Differential Revision: https://reviews.llvm.org/D88381
2020-11-02 14:03:35 +00:00

83 lines
2.4 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.frontendOpts().programAction_;
switch (ak) {
case InputOutputTest:
return std::make_unique<InputOutputTestAction>();
break;
case PrintPreprocessedInput:
return std::make_unique<PrintPreprocessedAction>();
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->frontendOpts().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->frontendOpts().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