/
ExecuteCompilerInvocation.cpp
167 lines (149 loc) · 5.95 KB
/
ExecuteCompilerInvocation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//===--- 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 "flang/Frontend/FrontendPluginRegistry.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"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Pass/PassManager.h"
namespace Fortran::frontend {
static std::unique_ptr<FrontendAction> CreateFrontendAction(
CompilerInstance &ci) {
switch (ci.frontendOpts().programAction) {
case InputOutputTest:
return std::make_unique<InputOutputTestAction>();
case PrintPreprocessedInput:
return std::make_unique<PrintPreprocessedAction>();
case ParseSyntaxOnly:
return std::make_unique<ParseSyntaxOnlyAction>();
case EmitMLIR:
return std::make_unique<EmitMLIRAction>();
case EmitLLVM:
return std::make_unique<EmitLLVMAction>();
case EmitLLVMBitcode:
return std::make_unique<EmitLLVMBitcodeAction>();
case EmitObj:
return std::make_unique<BackendAction>(
BackendAction::BackendActionTy::Backend_EmitObj);
case EmitAssembly:
return std::make_unique<BackendAction>(
BackendAction::BackendActionTy::Backend_EmitAssembly);
case DebugUnparse:
return std::make_unique<DebugUnparseAction>();
case DebugUnparseNoSema:
return std::make_unique<DebugUnparseNoSemaAction>();
case DebugUnparseWithSymbols:
return std::make_unique<DebugUnparseWithSymbolsAction>();
case DebugDumpSymbols:
return std::make_unique<DebugDumpSymbolsAction>();
case DebugDumpParseTree:
return std::make_unique<DebugDumpParseTreeAction>();
case DebugDumpPFT:
return std::make_unique<DebugDumpPFTAction>();
case DebugDumpParseTreeNoSema:
return std::make_unique<DebugDumpParseTreeNoSemaAction>();
case DebugDumpAll:
return std::make_unique<DebugDumpAllAction>();
case DebugDumpProvenance:
return std::make_unique<DebugDumpProvenanceAction>();
case DebugDumpParsingLog:
return std::make_unique<DebugDumpParsingLogAction>();
case DebugMeasureParseTree:
return std::make_unique<DebugMeasureParseTreeAction>();
case DebugPreFIRTree:
return std::make_unique<DebugPreFIRTreeAction>();
case GetDefinition:
return std::make_unique<GetDefinitionAction>();
case GetSymbolsSources:
return std::make_unique<GetSymbolsSourcesAction>();
case InitOnly:
return std::make_unique<InitOnlyAction>();
case PluginAction: {
for (const FrontendPluginRegistry::entry &plugin :
FrontendPluginRegistry::entries()) {
if (plugin.getName() == ci.frontendOpts().ActionName) {
std::unique_ptr<PluginParseTreeAction> p(plugin.instantiate());
return std::move(p);
}
}
unsigned diagID = ci.diagnostics().getCustomDiagID(
clang::DiagnosticsEngine::Error, "unable to find plugin '%0'");
ci.diagnostics().Report(diagID) << ci.frontendOpts().ActionName;
return nullptr;
}
}
llvm_unreachable("Invalid program action!");
}
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;
}
// Load any requested plugins.
for (const std::string &Path : flang->frontendOpts().plugins) {
std::string Error;
if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(
Path.c_str(), &Error)) {
unsigned diagID = flang->diagnostics().getCustomDiagID(
clang::DiagnosticsEngine::Error, "unable to load plugin '%0': '%1'");
flang->diagnostics().Report(diagID) << Path << Error;
}
}
// Honor -mllvm. This should happen AFTER plugins have been loaded!
if (!flang->frontendOpts().llvmArgs.empty()) {
unsigned numArgs = flang->frontendOpts().llvmArgs.size();
auto args = std::make_unique<const char *[]>(numArgs + 2);
args[0] = "flang (LLVM option parsing)";
for (unsigned i = 0; i != numArgs; ++i)
args[i + 1] = flang->frontendOpts().llvmArgs[i].c_str();
args[numArgs + 1] = nullptr;
llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
}
// Honor -mmlir. This should happen AFTER plugins have been loaded!
if (!flang->frontendOpts().mlirArgs.empty()) {
mlir::registerMLIRContextCLOptions();
mlir::registerPassManagerCLOptions();
unsigned numArgs = flang->frontendOpts().mlirArgs.size();
auto args = std::make_unique<const char *[]>(numArgs + 2);
args[0] = "flang (MLIR option parsing)";
for (unsigned i = 0; i != numArgs; ++i)
args[i + 1] = flang->frontendOpts().mlirArgs[i].c_str();
args[numArgs + 1] = nullptr;
llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
}
// If there were errors in processing arguments, don't do anything else.
if (flang->diagnostics().hasErrorOccurred()) {
return false;
}
// 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