[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / flang / lib / Frontend / FrontendAction.cpp
blob041182bdf6178101cde3525b670d20f3734bbbe0
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/
11 //===----------------------------------------------------------------------===//
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"
22 using namespace Fortran::frontend;
24 LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
26 void FrontendAction::setCurrentInput(const FrontendInputFile &input) {
27 this->currentInput = input;
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);
38 bool FrontendAction::beginSourceFile(CompilerInstance &ci,
39 const FrontendInputFile &realInput) {
41 FrontendInputFile input(realInput);
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");
59 // Report the diagnostic and return
60 ci.getDiagnostics().Report(diagID) << input.getFile();
61 beginSourceFileCleanUp(*this, ci);
62 return false;
66 assert(!instance && "Already processing a source file!");
67 assert(!realInput.isEmpty() && "Unexpected empty filename!");
68 setCurrentInput(realInput);
69 setInstance(&ci);
71 if (!ci.hasAllSources()) {
72 beginSourceFileCleanUp(*this, ci);
73 return false;
76 auto &invoc = ci.getInvocation();
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();
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());
98 // -fpreprocess-include-lines
99 invoc.getFortranOpts().expandIncludeLinesInPreprocessedOutput =
100 invoc.getPreprocessorOpts().preprocessIncludeLines;
102 // Decide between fixed and free form (if the user didn't express any
103 // preference, use the file extension to decide)
104 if (invoc.getFrontendOpts().fortranForm == FortranForm::Unknown) {
105 invoc.getFortranOpts().isFixedForm = getCurrentInput().getIsFixedForm();
108 if (!beginSourceFileAction()) {
109 beginSourceFileCleanUp(*this, ci);
110 return false;
113 return true;
116 bool FrontendAction::shouldEraseOutputFiles() {
117 return getInstance().getDiagnostics().hasErrorOccurred();
120 llvm::Error FrontendAction::execute() {
121 executeAction();
123 return llvm::Error::success();
126 void FrontendAction::endSourceFile() {
127 CompilerInstance &ci = getInstance();
129 // Cleanup the output streams, and erase the output files if instructed by the
130 // FrontendAction.
131 ci.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
133 setInstance(nullptr);
134 setCurrentInput(FrontendInputFile());
137 bool FrontendAction::runPrescan() {
138 CompilerInstance &ci = this->getInstance();
139 std::string currentInputPath{getCurrentFileOrBufferName()};
140 Fortran::parser::Options parserOptions = ci.getInvocation().getFortranOpts();
142 if (ci.getInvocation().getFrontendOpts().fortranForm ==
143 FortranForm::Unknown) {
144 // Switch between fixed and free form format based on the input file
145 // extension.
147 // Ideally we should have all Fortran options set before entering this
148 // method (i.e. before processing any specific input files). However, we
149 // can't decide between fixed and free form based on the file extension
150 // earlier than this.
151 parserOptions.isFixedForm = getCurrentInput().getIsFixedForm();
154 // Prescan. In case of failure, report and return.
155 ci.getParsing().Prescan(currentInputPath, parserOptions);
157 return !reportFatalScanningErrors();
160 bool FrontendAction::runParse(bool emitMessages) {
161 CompilerInstance &ci = this->getInstance();
163 // Parse. In case of failure, report and return.
164 ci.getParsing().Parse(llvm::outs());
166 if (reportFatalParsingErrors()) {
167 return false;
170 if (emitMessages) {
171 // Report any non-fatal diagnostics from getParsing now rather than
172 // combining them with messages from semantics.
173 ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
175 return true;
178 bool FrontendAction::runSemanticChecks() {
179 CompilerInstance &ci = this->getInstance();
180 std::optional<parser::Program> &parseTree{ci.getParsing().parseTree()};
181 assert(parseTree && "Cannot run semantic checks without a parse tree!");
183 // Transfer any pending non-fatal messages from parsing to semantics
184 // so that they are merged and all printed in order.
185 auto &semanticsCtx{ci.getSemanticsContext()};
186 semanticsCtx.messages().Annex(std::move(ci.getParsing().messages()));
187 semanticsCtx.set_debugModuleWriter(ci.getInvocation().getDebugModuleDir());
189 // Prepare semantics
190 ci.setSemantics(std::make_unique<Fortran::semantics::Semantics>(semanticsCtx,
191 *parseTree));
192 auto &semantics = ci.getSemantics();
193 semantics.set_hermeticModuleFileOutput(
194 ci.getInvocation().getHermeticModuleFileOutput());
196 // Run semantic checks
197 semantics.Perform();
199 if (reportFatalSemanticErrors()) {
200 return false;
203 // Report the diagnostics from parsing and the semantic checks
204 semantics.EmitMessages(ci.getSemaOutputStream());
206 return true;
209 bool FrontendAction::generateRtTypeTables() {
210 getInstance().setRtTyTables(
211 std::make_unique<Fortran::semantics::RuntimeDerivedTypeTables>(
212 BuildRuntimeDerivedTypeTables(getInstance().getSemanticsContext())));
214 // The runtime derived type information table builder may find additional
215 // semantic errors. Report them.
216 if (reportFatalSemanticErrors()) {
217 return false;
220 return true;
223 template <unsigned N>
224 bool FrontendAction::reportFatalErrors(const char (&message)[N]) {
225 if (!instance->getParsing().messages().empty() &&
226 (instance->getInvocation().getWarnAsErr() ||
227 instance->getParsing().messages().AnyFatalError())) {
228 const unsigned diagID = instance->getDiagnostics().getCustomDiagID(
229 clang::DiagnosticsEngine::Error, message);
230 instance->getDiagnostics().Report(diagID) << getCurrentFileOrBufferName();
231 instance->getParsing().messages().Emit(llvm::errs(),
232 instance->getAllCookedSources());
233 return true;
235 return false;
238 bool FrontendAction::reportFatalSemanticErrors() {
239 auto &diags = instance->getDiagnostics();
240 auto &sema = instance->getSemantics();
242 if (instance->getSemantics().AnyFatalError()) {
243 unsigned diagID = diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
244 "Semantic errors in %0");
245 diags.Report(diagID) << getCurrentFileOrBufferName();
246 sema.EmitMessages(instance->getSemaOutputStream());
248 return true;
251 return false;