[lldb] Add ability to hide the root name of a value
[llvm-project.git] / flang / lib / FrontendTool / ExecuteCompilerInvocation.cpp
blobb99d2b7196da3fda1a578ab153ac8b135238ead1
1 //===--- ExecuteCompilerInvocation.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 // 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) {
37 case InputOutputTest:
38 return std::make_unique<InputOutputTestAction>();
39 case PrintPreprocessedInput:
40 return std::make_unique<PrintPreprocessedAction>();
41 case ParseSyntaxOnly:
42 return std::make_unique<ParseSyntaxOnlyAction>();
43 case EmitMLIR:
44 return std::make_unique<EmitMLIRAction>();
45 case EmitLLVM:
46 return std::make_unique<EmitLLVMAction>();
47 case EmitLLVMBitcode:
48 return std::make_unique<EmitLLVMBitcodeAction>();
49 case EmitObj:
50 return std::make_unique<EmitObjAction>();
51 case EmitAssembly:
52 return std::make_unique<EmitAssemblyAction>();
53 case DebugUnparse:
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>();
63 case DebugDumpPFT:
64 return std::make_unique<DebugDumpPFTAction>();
65 case DebugDumpParseTreeNoSema:
66 return std::make_unique<DebugDumpParseTreeNoSemaAction>();
67 case DebugDumpAll:
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>();
75 case DebugPreFIRTree:
76 return std::make_unique<DebugPreFIRTreeAction>();
77 case GetDefinition:
78 return std::make_unique<GetDefinitionAction>();
79 case GetSymbolsSources:
80 return std::make_unique<GetSymbolsSourcesAction>();
81 case InitOnly:
82 return std::make_unique<InitOnlyAction>();
83 case PluginAction: {
84 for (const FrontendPluginRegistry::entry &plugin :
85 FrontendPluginRegistry::entries()) {
86 if (plugin.getName() == ci.getFrontendOpts().actionName) {
87 std::unique_ptr<PluginParseTreeAction> p(plugin.instantiate());
88 return std::move(p);
91 unsigned diagID = ci.getDiagnostics().getCustomDiagID(
92 clang::DiagnosticsEngine::Error, "unable to find plugin '%0'");
93 ci.getDiagnostics().Report(diagID) << ci.getFrontendOpts().actionName;
94 return nullptr;
98 llvm_unreachable("Invalid program action!");
101 bool executeCompilerInvocation(CompilerInstance *flang) {
102 // Honor -help.
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);
110 return true;
113 // Honor -version.
114 if (flang->getFrontendOpts().showVersion) {
115 llvm::cl::PrintVersionMessage();
116 return true;
119 // Load any requested plugins.
120 for (const std::string &path : flang->getFrontendOpts().plugins) {
121 std::string error;
122 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(path.c_str(),
123 &error)) {
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()) {
161 return false;
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));
169 if (!act)
170 return false;
172 bool success = flang->executeAction(*act);
173 return success;
176 } // namespace Fortran::frontend