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 Clang. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "clang/CodeGen/CodeGenAction.h" |
15 | #include "clang/Config/config.h" |
16 | #include "clang/Driver/Options.h" |
17 | #include "clang/ExtractAPI/FrontendActions.h" |
18 | #include "clang/Frontend/CompilerInstance.h" |
19 | #include "clang/Frontend/CompilerInvocation.h" |
20 | #include "clang/Frontend/FrontendActions.h" |
21 | #include "clang/Frontend/FrontendDiagnostic.h" |
22 | #include "clang/Frontend/FrontendPluginRegistry.h" |
23 | #include "clang/Frontend/Utils.h" |
24 | #include "clang/FrontendTool/Utils.h" |
25 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
26 | #include "clang/StaticAnalyzer/Frontend/AnalyzerHelpFlags.h" |
27 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
28 | #include "llvm/Option/OptTable.h" |
29 | #include "llvm/Support/BuryPointer.h" |
30 | #include "llvm/Support/DynamicLibrary.h" |
31 | #include "llvm/Support/ErrorHandling.h" |
32 | |
33 | #if CLANG_ENABLE_CIR |
34 | #include "mlir/IR/AsmState.h" |
35 | #include "mlir/IR/MLIRContext.h" |
36 | #include "mlir/Pass/PassManager.h" |
37 | #include "clang/CIR/Dialect/Passes.h" |
38 | #include "clang/CIR/FrontendAction/CIRGenAction.h" |
39 | #endif |
40 | |
41 | using namespace clang; |
42 | using namespace llvm::opt; |
43 | |
44 | namespace clang { |
45 | |
46 | static std::unique_ptr<FrontendAction> |
47 | CreateFrontendBaseAction(CompilerInstance &CI) { |
48 | using namespace clang::frontend; |
49 | StringRef Action("unknown" ); |
50 | (void)Action; |
51 | |
52 | unsigned UseCIR = CI.getFrontendOpts().UseClangIRPipeline; |
53 | frontend::ActionKind Act = CI.getFrontendOpts().ProgramAction; |
54 | bool EmitsCIR = Act == EmitCIR; |
55 | |
56 | if (!UseCIR && EmitsCIR) |
57 | llvm::report_fatal_error(reason: "-emit-cir and only valid when using -fclangir" ); |
58 | |
59 | switch (CI.getFrontendOpts().ProgramAction) { |
60 | case ASTDeclList: return std::make_unique<ASTDeclListAction>(); |
61 | case ASTDump: return std::make_unique<ASTDumpAction>(); |
62 | case ASTPrint: return std::make_unique<ASTPrintAction>(); |
63 | case ASTView: return std::make_unique<ASTViewAction>(); |
64 | case DumpCompilerOptions: |
65 | return std::make_unique<DumpCompilerOptionsAction>(); |
66 | case DumpRawTokens: return std::make_unique<DumpRawTokensAction>(); |
67 | case DumpTokens: return std::make_unique<DumpTokensAction>(); |
68 | case EmitAssembly: |
69 | #if CLANG_ENABLE_CIR |
70 | if (UseCIR) |
71 | return std::make_unique<cir::EmitAssemblyAction>(); |
72 | #endif |
73 | return std::make_unique<EmitAssemblyAction>(); |
74 | case EmitBC: |
75 | #if CLANG_ENABLE_CIR |
76 | if (UseCIR) |
77 | return std::make_unique<cir::EmitBCAction>(); |
78 | #endif |
79 | return std::make_unique<EmitBCAction>(); |
80 | case EmitCIR: |
81 | #if CLANG_ENABLE_CIR |
82 | return std::make_unique<cir::EmitCIRAction>(); |
83 | #else |
84 | llvm_unreachable("CIR suppport not built into clang" ); |
85 | #endif |
86 | case EmitHTML: return std::make_unique<HTMLPrintAction>(); |
87 | case EmitLLVM: { |
88 | #if CLANG_ENABLE_CIR |
89 | if (UseCIR) |
90 | return std::make_unique<cir::EmitLLVMAction>(); |
91 | #endif |
92 | return std::make_unique<EmitLLVMAction>(); |
93 | } |
94 | case EmitLLVMOnly: return std::make_unique<EmitLLVMOnlyAction>(); |
95 | case EmitCodeGenOnly: return std::make_unique<EmitCodeGenOnlyAction>(); |
96 | case EmitObj: |
97 | #if CLANG_ENABLE_CIR |
98 | if (UseCIR) |
99 | return std::make_unique<cir::EmitObjAction>(); |
100 | #endif |
101 | return std::make_unique<EmitObjAction>(); |
102 | case ExtractAPI: |
103 | return std::make_unique<ExtractAPIAction>(); |
104 | case FixIt: return std::make_unique<FixItAction>(); |
105 | case GenerateModule: |
106 | return std::make_unique<GenerateModuleFromModuleMapAction>(); |
107 | case GenerateModuleInterface: |
108 | return std::make_unique<GenerateModuleInterfaceAction>(); |
109 | case GenerateReducedModuleInterface: |
110 | return std::make_unique<GenerateReducedModuleInterfaceAction>(); |
111 | case GenerateHeaderUnit: |
112 | return std::make_unique<GenerateHeaderUnitAction>(); |
113 | case GeneratePCH: return std::make_unique<GeneratePCHAction>(); |
114 | case GenerateInterfaceStubs: |
115 | return std::make_unique<GenerateInterfaceStubsAction>(); |
116 | case InitOnly: return std::make_unique<InitOnlyAction>(); |
117 | case ParseSyntaxOnly: return std::make_unique<SyntaxOnlyAction>(); |
118 | case ModuleFileInfo: return std::make_unique<DumpModuleInfoAction>(); |
119 | case VerifyPCH: return std::make_unique<VerifyPCHAction>(); |
120 | case TemplightDump: return std::make_unique<TemplightDumpAction>(); |
121 | |
122 | case PluginAction: { |
123 | for (const FrontendPluginRegistry::entry &Plugin : |
124 | FrontendPluginRegistry::entries()) { |
125 | if (Plugin.getName() == CI.getFrontendOpts().ActionName) { |
126 | std::unique_ptr<PluginASTAction> P(Plugin.instantiate()); |
127 | if ((P->getActionType() != PluginASTAction::ReplaceAction && |
128 | P->getActionType() != PluginASTAction::CmdlineAfterMainAction) || |
129 | !P->ParseArgs( |
130 | CI, |
131 | arg: CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) |
132 | return nullptr; |
133 | return std::move(P); |
134 | } |
135 | } |
136 | |
137 | CI.getDiagnostics().Report(DiagID: diag::err_fe_invalid_plugin_name) |
138 | << CI.getFrontendOpts().ActionName; |
139 | return nullptr; |
140 | } |
141 | |
142 | case PrintPreamble: return std::make_unique<PrintPreambleAction>(); |
143 | case PrintPreprocessedInput: { |
144 | if (CI.getPreprocessorOutputOpts().RewriteIncludes || |
145 | CI.getPreprocessorOutputOpts().RewriteImports) |
146 | return std::make_unique<RewriteIncludesAction>(); |
147 | return std::make_unique<PrintPreprocessedAction>(); |
148 | } |
149 | |
150 | case RewriteMacros: return std::make_unique<RewriteMacrosAction>(); |
151 | case RewriteTest: return std::make_unique<RewriteTestAction>(); |
152 | #if CLANG_ENABLE_OBJC_REWRITER |
153 | case RewriteObjC: return std::make_unique<RewriteObjCAction>(); |
154 | #else |
155 | case RewriteObjC: Action = "RewriteObjC" ; break; |
156 | #endif |
157 | #if CLANG_ENABLE_STATIC_ANALYZER |
158 | case RunAnalysis: return std::make_unique<ento::AnalysisAction>(); |
159 | #else |
160 | case RunAnalysis: Action = "RunAnalysis" ; break; |
161 | #endif |
162 | case RunPreprocessorOnly: return std::make_unique<PreprocessOnlyAction>(); |
163 | case PrintDependencyDirectivesSourceMinimizerOutput: |
164 | return std::make_unique<PrintDependencyDirectivesSourceMinimizerAction>(); |
165 | } |
166 | |
167 | #if !CLANG_ENABLE_STATIC_ANALYZER || !CLANG_ENABLE_OBJC_REWRITER |
168 | CI.getDiagnostics().Report(DiagID: diag::err_fe_action_not_available) << Action; |
169 | return 0; |
170 | #else |
171 | llvm_unreachable("Invalid program action!" ); |
172 | #endif |
173 | } |
174 | |
175 | std::unique_ptr<FrontendAction> |
176 | CreateFrontendAction(CompilerInstance &CI) { |
177 | // Create the underlying action. |
178 | std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI); |
179 | if (!Act) |
180 | return nullptr; |
181 | |
182 | const FrontendOptions &FEOpts = CI.getFrontendOpts(); |
183 | |
184 | if (FEOpts.FixAndRecompile) { |
185 | Act = std::make_unique<FixItRecompile>(args: std::move(Act)); |
186 | } |
187 | |
188 | // Wrap the base FE action in an extract api action to generate |
189 | // symbol graph as a biproduct of compilation (enabled with |
190 | // --emit-symbol-graph option) |
191 | if (FEOpts.EmitSymbolGraph) { |
192 | if (FEOpts.SymbolGraphOutputDir.empty()) { |
193 | CI.getDiagnostics().Report(DiagID: diag::warn_missing_symbol_graph_dir); |
194 | CI.getFrontendOpts().SymbolGraphOutputDir = "." ; |
195 | } |
196 | CI.getCodeGenOpts().ClearASTBeforeBackend = false; |
197 | Act = std::make_unique<WrappingExtractAPIAction>(args: std::move(Act)); |
198 | } |
199 | |
200 | // If there are any AST files to merge, create a frontend action |
201 | // adaptor to perform the merge. |
202 | if (!FEOpts.ASTMergeFiles.empty()) |
203 | Act = std::make_unique<ASTMergeAction>(args: std::move(Act), |
204 | args: FEOpts.ASTMergeFiles); |
205 | |
206 | return Act; |
207 | } |
208 | |
209 | bool ExecuteCompilerInvocation(CompilerInstance *Clang) { |
210 | // Honor -help. |
211 | if (Clang->getFrontendOpts().ShowHelp) { |
212 | driver::getDriverOptTable().printHelp( |
213 | OS&: llvm::outs(), Usage: "clang -cc1 [options] file..." , |
214 | Title: "LLVM 'Clang' Compiler: http://clang.llvm.org" , |
215 | /*ShowHidden=*/false, /*ShowAllAliases=*/false, |
216 | VisibilityMask: llvm::opt::Visibility(driver::options::CC1Option)); |
217 | return true; |
218 | } |
219 | |
220 | // Honor -version. |
221 | // |
222 | // FIXME: Use a better -version message? |
223 | if (Clang->getFrontendOpts().ShowVersion) { |
224 | llvm::cl::PrintVersionMessage(); |
225 | return true; |
226 | } |
227 | |
228 | Clang->LoadRequestedPlugins(); |
229 | |
230 | // Honor -mllvm. |
231 | // |
232 | // FIXME: Remove this, one day. |
233 | // This should happen AFTER plugins have been loaded! |
234 | if (!Clang->getFrontendOpts().LLVMArgs.empty()) { |
235 | unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); |
236 | auto Args = std::make_unique<const char*[]>(num: NumArgs + 2); |
237 | Args[0] = "clang (LLVM option parsing)" ; |
238 | for (unsigned i = 0; i != NumArgs; ++i) |
239 | Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); |
240 | Args[NumArgs + 1] = nullptr; |
241 | llvm::cl::ParseCommandLineOptions(argc: NumArgs + 1, argv: Args.get()); |
242 | } |
243 | |
244 | #if CLANG_ENABLE_STATIC_ANALYZER |
245 | // These should happen AFTER plugins have been loaded! |
246 | |
247 | AnalyzerOptions &AnOpts = Clang->getAnalyzerOpts(); |
248 | |
249 | // Honor -analyzer-checker-help and -analyzer-checker-help-hidden. |
250 | if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha || |
251 | AnOpts.ShowCheckerHelpDeveloper) { |
252 | ento::printCheckerHelp(OS&: llvm::outs(), CI&: *Clang); |
253 | return true; |
254 | } |
255 | |
256 | // Honor -analyzer-checker-option-help. |
257 | if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList || |
258 | AnOpts.ShowCheckerOptionDeveloperList) { |
259 | ento::printCheckerConfigList(OS&: llvm::outs(), CI&: *Clang); |
260 | return true; |
261 | } |
262 | |
263 | // Honor -analyzer-list-enabled-checkers. |
264 | if (AnOpts.ShowEnabledCheckerList) { |
265 | ento::printEnabledCheckerList(OS&: llvm::outs(), CI&: *Clang); |
266 | return true; |
267 | } |
268 | |
269 | // Honor -analyzer-config-help. |
270 | if (AnOpts.ShowConfigOptionsList) { |
271 | ento::printAnalyzerConfigList(OS&: llvm::outs()); |
272 | return true; |
273 | } |
274 | #endif |
275 | |
276 | #if CLANG_ENABLE_CIR |
277 | if (!Clang->getFrontendOpts().MLIRArgs.empty()) { |
278 | mlir::registerCIRPasses(); |
279 | mlir::registerMLIRContextCLOptions(); |
280 | mlir::registerPassManagerCLOptions(); |
281 | mlir::registerAsmPrinterCLOptions(); |
282 | unsigned NumArgs = Clang->getFrontendOpts().MLIRArgs.size(); |
283 | auto Args = std::make_unique<const char *[]>(NumArgs + 2); |
284 | Args[0] = "clang (MLIR option parsing)" ; |
285 | for (unsigned i = 0; i != NumArgs; ++i) |
286 | Args[i + 1] = Clang->getFrontendOpts().MLIRArgs[i].c_str(); |
287 | Args[NumArgs + 1] = nullptr; |
288 | llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get()); |
289 | } |
290 | #endif |
291 | |
292 | // If there were errors in processing arguments, don't do anything else. |
293 | if (Clang->getDiagnostics().hasErrorOccurred()) |
294 | return false; |
295 | // Create and execute the frontend action. |
296 | std::unique_ptr<FrontendAction> Act(CreateFrontendAction(CI&: *Clang)); |
297 | if (!Act) |
298 | return false; |
299 | bool Success = Clang->ExecuteAction(Act&: *Act); |
300 | if (Clang->getFrontendOpts().DisableFree) |
301 | llvm::BuryPointer(Ptr: std::move(Act)); |
302 | return Success; |
303 | } |
304 | |
305 | } // namespace clang |
306 | |