| 1 |
//===--- FrontendAction.cpp -----------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "flang/Frontend/FrontendAction.h" |
| 14 |
#include "flang/Frontend/CompilerInstance.h" |
| 15 |
#include "flang/Frontend/FrontendActions.h" |
| 16 |
#include "flang/Frontend/FrontendOptions.h" |
| 17 |
#include "flang/Frontend/FrontendPluginRegistry.h" |
| 18 |
#include "clang/Basic/DiagnosticFrontend.h" |
| 19 |
#include "llvm/Support/Errc.h" |
| 20 |
#include "llvm/Support/VirtualFileSystem.h" |
| 21 |
|
| 22 |
using namespace Fortran::frontend; |
| 23 |
|
| 24 |
LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry) |
| 25 |
|
| 26 |
void FrontendAction::setCurrentInput(const FrontendInputFile &input) { |
| 27 |
this->currentInput = input; |
| 28 |
} |
| 29 |
|
| 30 |
// Call this method if BeginSourceFile fails. |
| 31 |
// Deallocate compiler instance, input and output descriptors |
| 32 |
static void beginSourceFileCleanUp(FrontendAction &fa, CompilerInstance &ci) { |
| 33 |
ci.clearOutputFiles(/*EraseFiles=*/true); |
| 34 |
fa.setCurrentInput(FrontendInputFile()); |
| 35 |
fa.setInstance(nullptr); |
| 36 |
} |
| 37 |
|
| 38 |
bool FrontendAction::beginSourceFile(CompilerInstance &ci, |
| 39 |
const FrontendInputFile &realInput) { |
| 40 |
|
| 41 |
FrontendInputFile input(realInput); |
| 42 |
|
| 43 |
// Return immediately if the input file does not exist or is not a file. Note |
| 44 |
// that we cannot check this for input from stdin. |
| 45 |
if (input.getFile() != "-") { |
| 46 |
if (!llvm::sys::fs::is_regular_file(input.getFile())) { |
| 47 |
// Create an diagnostic ID to report |
| 48 |
unsigned diagID; |
| 49 |
if (llvm::vfs::getRealFileSystem()->exists(input.getFile())) { |
| 50 |
ci.getDiagnostics().Report(clang::diag::err_fe_error_reading) |
| 51 |
<< input.getFile() << "not a regular file"; |
| 52 |
diagID = ci.getDiagnostics().getCustomDiagID( |
| 53 |
clang::DiagnosticsEngine::Error, "%0 is not a regular file"); |
| 54 |
} else { |
| 55 |
diagID = ci.getDiagnostics().getCustomDiagID( |
| 56 |
clang::DiagnosticsEngine::Error, "%0 does not exist"); |
| 57 |
} |
| 58 |
|
| 59 |
// Report the diagnostic and return |
| 60 |
ci.getDiagnostics().Report(diagID) << input.getFile(); |
| 61 |
beginSourceFileCleanUp(*this, ci); |
| 62 |
return false; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
assert(!instance && "Already processing a source file!"); |
| 67 |
assert(!realInput.isEmpty() && "Unexpected empty filename!"); |
| 68 |
setCurrentInput(realInput); |
| 69 |
setInstance(&ci); |
| 70 |
|
| 71 |
if (!ci.hasAllSources()) { |
| 72 |
beginSourceFileCleanUp(*this, ci); |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
auto &invoc = ci.getInvocation(); |
| 77 |
|
| 78 |
// Include command-line and predefined preprocessor macros. Use either: |
| 79 |
// * `-cpp/-nocpp`, or |
| 80 |
// * the file extension (if the user didn't express any preference) |
| 81 |
// to decide whether to include them or not. |
| 82 |
if ((invoc.getPreprocessorOpts().macrosFlag == PPMacrosFlag::Include) || |
| 83 |
(invoc.getPreprocessorOpts().macrosFlag == PPMacrosFlag::Unknown && |
| 84 |
getCurrentInput().getMustBePreprocessed())) { |
| 85 |
invoc.setDefaultPredefinitions(); |
| 86 |
invoc.collectMacroDefinitions(); |
| 87 |
} |
| 88 |
|
| 89 |
if (!invoc.getFortranOpts().features.IsEnabled( |
| 90 |
Fortran::common::LanguageFeature::CUDA)) { |
| 91 |
// Enable CUDA Fortran if source file is *.cuf/*.CUF and not already |
| 92 |
// enabled. |
| 93 |
invoc.getFortranOpts().features.Enable( |
| 94 |
Fortran::common::LanguageFeature::CUDA, |
| 95 |
getCurrentInput().getIsCUDAFortran()); |
| 96 |
} |
| 97 |
|
| 98 |
// Decide between fixed and free form (if the user didn't express any |
| 99 |
// preference, use the file extension to decide) |
| 100 |
if (invoc.getFrontendOpts().fortranForm == FortranForm::Unknown) { |
| 101 |
invoc.getFortranOpts().isFixedForm = getCurrentInput().getIsFixedForm(); |
| 102 |
} |
| 103 |
|
| 104 |
if (!beginSourceFileAction()) { |
| 105 |
beginSourceFileCleanUp(*this, ci); |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
bool FrontendAction::shouldEraseOutputFiles() { |
| 113 |
return getInstance().getDiagnostics().hasErrorOccurred(); |
| 114 |
} |
| 115 |
|
| 116 |
llvm::Error FrontendAction::execute() { |
| 117 |
executeAction(); |
| 118 |
|
| 119 |
return llvm::Error::success(); |
| 120 |
} |
| 121 |
|
| 122 |
void FrontendAction::endSourceFile() { |
| 123 |
CompilerInstance &ci = getInstance(); |
| 124 |
|
| 125 |
// Cleanup the output streams, and erase the output files if instructed by the |
| 126 |
// FrontendAction. |
| 127 |
ci.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles()); |
| 128 |
|
| 129 |
setInstance(nullptr); |
| 130 |
setCurrentInput(FrontendInputFile()); |
| 131 |
} |
| 132 |
|
| 133 |
bool FrontendAction::runPrescan() { |
| 134 |
CompilerInstance &ci = this->getInstance(); |
| 135 |
std::string currentInputPath{getCurrentFileOrBufferName()}; |
| 136 |
Fortran::parser::Options parserOptions = ci.getInvocation().getFortranOpts(); |
| 137 |
|
| 138 |
if (ci.getInvocation().getFrontendOpts().fortranForm == |
| 139 |
FortranForm::Unknown) { |
| 140 |
// Switch between fixed and free form format based on the input file |
| 141 |
// extension. |
| 142 |
// |
| 143 |
// Ideally we should have all Fortran options set before entering this |
| 144 |
// method (i.e. before processing any specific input files). However, we |
| 145 |
// can't decide between fixed and free form based on the file extension |
| 146 |
// earlier than this. |
| 147 |
parserOptions.isFixedForm = getCurrentInput().getIsFixedForm(); |
| 148 |
} |
| 149 |
|
| 150 |
// Prescan. In case of failure, report and return. |
| 151 |
ci.getParsing().Prescan(currentInputPath, parserOptions); |
| 152 |
|
| 153 |
return !reportFatalScanningErrors(); |
| 154 |
} |
| 155 |
|
| 156 |
bool FrontendAction::runParse(bool emitMessages) { |
| 157 |
CompilerInstance &ci = this->getInstance(); |
| 158 |
|
| 159 |
// Parse. In case of failure, report and return. |
| 160 |
ci.getParsing().Parse(llvm::outs()); |
| 161 |
|
| 162 |
if (reportFatalParsingErrors()) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
if (emitMessages) { |
| 167 |
// Report any non-fatal diagnostics from getParsing now rather than |
| 168 |
// combining them with messages from semantics. |
| 169 |
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources()); |
| 170 |
} |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
bool FrontendAction::runSemanticChecks() { |
| 175 |
CompilerInstance &ci = this->getInstance(); |
| 176 |
std::optional<parser::Program> &parseTree{ci.getParsing().parseTree()}; |
| 177 |
assert(parseTree && "Cannot run semantic checks without a parse tree!"); |
| 178 |
|
| 179 |
// Transfer any pending non-fatal messages from parsing to semantics |
| 180 |
// so that they are merged and all printed in order. |
| 181 |
auto &semanticsCtx{ci.getSemanticsContext()}; |
| 182 |
semanticsCtx.messages().Annex(std::move(ci.getParsing().messages())); |
| 183 |
semanticsCtx.set_debugModuleWriter(ci.getInvocation().getDebugModuleDir()); |
| 184 |
|
| 185 |
// Prepare semantics |
| 186 |
ci.setSemantics(std::make_unique<Fortran::semantics::Semantics>(semanticsCtx, |
| 187 |
*parseTree)); |
| 188 |
auto &semantics = ci.getSemantics(); |
| 189 |
semantics.set_hermeticModuleFileOutput( |
| 190 |
ci.getInvocation().getHermeticModuleFileOutput()); |
| 191 |
|
| 192 |
// Run semantic checks |
| 193 |
semantics.Perform(); |
| 194 |
|
| 195 |
if (reportFatalSemanticErrors()) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
// Report the diagnostics from parsing and the semantic checks |
| 200 |
semantics.EmitMessages(ci.getSemaOutputStream()); |
| 201 |
|
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
bool FrontendAction::generateRtTypeTables() { |
| 206 |
getInstance().setRtTyTables( |
| 207 |
std::make_unique<Fortran::semantics::RuntimeDerivedTypeTables>( |
| 208 |
BuildRuntimeDerivedTypeTables(getInstance().getSemanticsContext()))); |
| 209 |
|
| 210 |
// The runtime derived type information table builder may find additional |
| 211 |
// semantic errors. Report them. |
| 212 |
if (reportFatalSemanticErrors()) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
template <unsigned N> |
| 220 |
bool FrontendAction::reportFatalErrors(const char (&message)[N]) { |
| 221 |
if (!instance->getParsing().messages().empty() && |
| 222 |
(instance->getInvocation().getWarnAsErr() || |
| 223 |
instance->getParsing().messages().AnyFatalError())) { |
| 224 |
const unsigned diagID = instance->getDiagnostics().getCustomDiagID( |
| 225 |
clang::DiagnosticsEngine::Error, message); |
| 226 |
instance->getDiagnostics().Report(diagID) << getCurrentFileOrBufferName(); |
| 227 |
instance->getParsing().messages().Emit(llvm::errs(), |
| 228 |
instance->getAllCookedSources()); |
| 229 |
return true; |
| 230 |
} |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
bool FrontendAction::reportFatalSemanticErrors() { |
| 235 |
auto &diags = instance->getDiagnostics(); |
| 236 |
auto &sema = instance->getSemantics(); |
| 237 |
|
| 238 |
if (instance->getSemantics().AnyFatalError()) { |
| 239 |
unsigned diagID = diags.getCustomDiagID(clang::DiagnosticsEngine::Error, |
| 240 |
"Semantic errors in %0"); |
| 241 |
diags.Report(diagID) << getCurrentFileOrBufferName(); |
| 242 |
sema.EmitMessages(instance->getSemaOutputStream()); |
| 243 |
|
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
return false; |
| 248 |
} |