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