1 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file holds ExecuteCompilerInvocation(). It is split into its own file to
10 // minimize the impact of pulling in essentially everything else in Flang.
12 //===----------------------------------------------------------------------===//
14 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
16 //===----------------------------------------------------------------------===//
18 #include "flang/Frontend/CompilerInstance.h"
19 #include "flang/Frontend/FrontendActions.h"
20 #include "flang/Frontend/FrontendPluginRegistry.h"
22 #include "mlir/IR/AsmState.h"
23 #include "mlir/IR/MLIRContext.h"
24 #include "mlir/Pass/PassManager.h"
25 #include "clang/Driver/Options.h"
26 #include "llvm/Option/OptTable.h"
27 #include "llvm/Option/Option.h"
28 #include "llvm/Support/BuryPointer.h"
29 #include "llvm/Support/CommandLine.h"
31 namespace Fortran::frontend
{
33 static std::unique_ptr
<FrontendAction
>
34 createFrontendAction(CompilerInstance
&ci
) {
36 switch (ci
.getFrontendOpts().programAction
) {
38 return std::make_unique
<InputOutputTestAction
>();
39 case PrintPreprocessedInput
:
40 return std::make_unique
<PrintPreprocessedAction
>();
42 return std::make_unique
<ParseSyntaxOnlyAction
>();
44 return std::make_unique
<EmitMLIRAction
>();
46 return std::make_unique
<EmitLLVMAction
>();
48 return std::make_unique
<EmitLLVMBitcodeAction
>();
50 return std::make_unique
<EmitObjAction
>();
52 return std::make_unique
<EmitAssemblyAction
>();
54 return std::make_unique
<DebugUnparseAction
>();
55 case DebugUnparseNoSema
:
56 return std::make_unique
<DebugUnparseNoSemaAction
>();
57 case DebugUnparseWithSymbols
:
58 return std::make_unique
<DebugUnparseWithSymbolsAction
>();
59 case DebugDumpSymbols
:
60 return std::make_unique
<DebugDumpSymbolsAction
>();
61 case DebugDumpParseTree
:
62 return std::make_unique
<DebugDumpParseTreeAction
>();
64 return std::make_unique
<DebugDumpPFTAction
>();
65 case DebugDumpParseTreeNoSema
:
66 return std::make_unique
<DebugDumpParseTreeNoSemaAction
>();
68 return std::make_unique
<DebugDumpAllAction
>();
69 case DebugDumpProvenance
:
70 return std::make_unique
<DebugDumpProvenanceAction
>();
71 case DebugDumpParsingLog
:
72 return std::make_unique
<DebugDumpParsingLogAction
>();
73 case DebugMeasureParseTree
:
74 return std::make_unique
<DebugMeasureParseTreeAction
>();
76 return std::make_unique
<DebugPreFIRTreeAction
>();
78 return std::make_unique
<GetDefinitionAction
>();
79 case GetSymbolsSources
:
80 return std::make_unique
<GetSymbolsSourcesAction
>();
82 return std::make_unique
<InitOnlyAction
>();
84 for (const FrontendPluginRegistry::entry
&plugin
:
85 FrontendPluginRegistry::entries()) {
86 if (plugin
.getName() == ci
.getFrontendOpts().actionName
) {
87 std::unique_ptr
<PluginParseTreeAction
> p(plugin
.instantiate());
91 unsigned diagID
= ci
.getDiagnostics().getCustomDiagID(
92 clang::DiagnosticsEngine::Error
, "unable to find plugin '%0'");
93 ci
.getDiagnostics().Report(diagID
) << ci
.getFrontendOpts().actionName
;
98 llvm_unreachable("Invalid program action!");
101 bool executeCompilerInvocation(CompilerInstance
*flang
) {
103 if (flang
->getFrontendOpts().showHelp
) {
104 clang::driver::getDriverOptTable().printHelp(
105 llvm::outs(), "flang-new -fc1 [options] file...",
106 "LLVM 'Flang' Compiler",
107 /*Include=*/clang::driver::options::FC1Option
,
108 /*Exclude=*/llvm::opt::DriverFlag::HelpHidden
,
109 /*ShowAllAliases=*/false);
114 if (flang
->getFrontendOpts().showVersion
) {
115 llvm::cl::PrintVersionMessage();
119 // Load any requested plugins.
120 for (const std::string
&path
: flang
->getFrontendOpts().plugins
) {
122 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(path
.c_str(),
124 unsigned diagID
= flang
->getDiagnostics().getCustomDiagID(
125 clang::DiagnosticsEngine::Error
, "unable to load plugin '%0': '%1'");
126 flang
->getDiagnostics().Report(diagID
) << path
<< error
;
130 // Honor -mllvm. This should happen AFTER plugins have been loaded!
131 if (!flang
->getFrontendOpts().llvmArgs
.empty()) {
132 unsigned numArgs
= flang
->getFrontendOpts().llvmArgs
.size();
133 auto args
= std::make_unique
<const char *[]>(numArgs
+ 2);
134 args
[0] = "flang (LLVM option parsing)";
136 for (unsigned i
= 0; i
!= numArgs
; ++i
)
137 args
[i
+ 1] = flang
->getFrontendOpts().llvmArgs
[i
].c_str();
139 args
[numArgs
+ 1] = nullptr;
140 llvm::cl::ParseCommandLineOptions(numArgs
+ 1, args
.get());
143 // Honor -mmlir. This should happen AFTER plugins have been loaded!
144 if (!flang
->getFrontendOpts().mlirArgs
.empty()) {
145 mlir::registerMLIRContextCLOptions();
146 mlir::registerPassManagerCLOptions();
147 mlir::registerAsmPrinterCLOptions();
148 unsigned numArgs
= flang
->getFrontendOpts().mlirArgs
.size();
149 auto args
= std::make_unique
<const char *[]>(numArgs
+ 2);
150 args
[0] = "flang (MLIR option parsing)";
152 for (unsigned i
= 0; i
!= numArgs
; ++i
)
153 args
[i
+ 1] = flang
->getFrontendOpts().mlirArgs
[i
].c_str();
155 args
[numArgs
+ 1] = nullptr;
156 llvm::cl::ParseCommandLineOptions(numArgs
+ 1, args
.get());
159 // If there were errors in processing arguments, don't do anything else.
160 if (flang
->getDiagnostics().hasErrorOccurred()) {
164 // Honor color diagnostics.
165 flang
->getDiagnosticOpts().ShowColors
= flang
->getFrontendOpts().showColors
;
167 // Create and execute the frontend action.
168 std::unique_ptr
<FrontendAction
> act(createFrontendAction(*flang
));
172 bool success
= flang
->executeAction(*act
);
176 } // namespace Fortran::frontend