llvm-project/flang/lib/Frontend/FrontendActions.cpp
Andrzej Warzynski 7d246cb19d [flang][driver] Add support for -fsyntax-only
The behaviour triggered with this flag is consistent with `-fparse-only`
in `flang` (i.e. the throwaway driver). This new spelling is consistent
with Clang and gfortran, and was proposed and agreed on for the new
driver in [1].

This patch also adds some minimal logic to communicate whether the
semantic checks have failed or not. When semantic checks fail, a
frontend driver error is generated. The return code from the frontend
driver is then determined by checking the driver diagnostics - the
presence of driver errors means that the compilation has failed. This
logic is consistent with `clang -cc1`.

[1] http://lists.llvm.org/pipermail/flang-dev/2020-November/000588.html

Differential Revision: https://reviews.llvm.org/D92854
2020-12-18 09:35:02 +00:00

103 lines
3.3 KiB
C++

//===--- FrontendActions.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
//
//===----------------------------------------------------------------------===//
#include "flang/Frontend/FrontendActions.h"
#include "flang/Common/default-kinds.h"
#include "flang/Frontend/CompilerInstance.h"
#include "flang/Parser/parsing.h"
#include "flang/Parser/provenance.h"
#include "flang/Parser/source.h"
#include "flang/Semantics/semantics.h"
using namespace Fortran::frontend;
void InputOutputTestAction::ExecuteAction() {
// Get the name of the file from FrontendInputFile current.
std::string path{GetCurrentFileOrBufferName()};
std::string buf;
llvm::raw_string_ostream error_stream{buf};
bool binaryMode = true;
// Set/store input file info into CompilerInstance.
CompilerInstance &ci = instance();
Fortran::parser::AllSources &allSources{ci.allSources()};
const Fortran::parser::SourceFile *sf;
sf = allSources.Open(path, error_stream);
llvm::ArrayRef<char> fileContent = sf->content();
// Output file descriptor to receive the content of input file.
std::unique_ptr<llvm::raw_ostream> os;
// Do not write on the output file if using outputStream_.
if (ci.IsOutputStreamNull()) {
os = ci.CreateDefaultOutputFile(
binaryMode, GetCurrentFileOrBufferName(), "txt");
if (!os)
return;
(*os) << fileContent.data();
} else {
ci.WriteOutputStream(fileContent.data());
}
}
void PrintPreprocessedAction::ExecuteAction() {
std::string buf;
llvm::raw_string_ostream outForPP{buf};
// Run the preprocessor
CompilerInstance &ci = this->instance();
ci.parsing().DumpCookedChars(outForPP);
// If a pre-defined output stream exists, dump the preprocessed content there
if (!ci.IsOutputStreamNull()) {
// Send the output to the pre-defined output buffer.
ci.WriteOutputStream(outForPP.str());
return;
}
// Create a file and save the preprocessed output there
if (auto os{ci.CreateDefaultOutputFile(
/*Binary=*/true, /*InFile=*/GetCurrentFileOrBufferName())}) {
(*os) << outForPP.str();
} else {
llvm::errs() << "Unable to create the output file\n";
return;
}
}
void ParseSyntaxOnlyAction::ExecuteAction() {
CompilerInstance &ci = this->instance();
// TODO: These should be specifiable by users. For now just use the defaults.
common::LanguageFeatureControl features;
Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
// Parse
ci.parsing().Parse(llvm::outs());
auto &parseTree{*ci.parsing().parseTree()};
// Prepare semantics
Fortran::semantics::SemanticsContext semanticsContext{
defaultKinds, features, ci.allCookedSources()};
Fortran::semantics::Semantics semantics{
semanticsContext, parseTree, ci.parsing().cooked().AsCharBlock()};
// Run semantic checks
semantics.Perform();
// Report the diagnostics from the semantic checks
semantics.EmitMessages(ci.semaOutputStream());
if (semantics.AnyFatalError()) {
unsigned DiagID = ci.diagnostics().getCustomDiagID(
clang::DiagnosticsEngine::Error, "semantic errors in %0");
ci.diagnostics().Report(DiagID) << GetCurrentFileOrBufferName();
}
}