clang 21.0.0git
ExecuteCompilerInvocation.cpp
Go to the documentation of this file.
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
15#include "clang/Config/config.h"
28#include "llvm/Option/OptTable.h"
29#include "llvm/Option/Option.h"
30#include "llvm/Support/BuryPointer.h"
31#include "llvm/Support/DynamicLibrary.h"
32#include "llvm/Support/ErrorHandling.h"
33
34#if CLANG_ENABLE_CIR
36#endif
37
38using namespace clang;
39using namespace llvm::opt;
40
41namespace clang {
42
43static std::unique_ptr<FrontendAction>
45 using namespace clang::frontend;
46 StringRef Action("unknown");
47 (void)Action;
48
49 unsigned UseCIR = CI.getFrontendOpts().UseClangIRPipeline;
51 bool EmitsCIR = Act == EmitCIR;
52
53 if (!UseCIR && EmitsCIR)
54 llvm::report_fatal_error("-emit-cir and only valid when using -fclangir");
55
56 switch (CI.getFrontendOpts().ProgramAction) {
57 case ASTDeclList: return std::make_unique<ASTDeclListAction>();
58 case ASTDump: return std::make_unique<ASTDumpAction>();
59 case ASTPrint: return std::make_unique<ASTPrintAction>();
60 case ASTView: return std::make_unique<ASTViewAction>();
61 case DumpCompilerOptions:
62 return std::make_unique<DumpCompilerOptionsAction>();
63 case DumpRawTokens: return std::make_unique<DumpRawTokensAction>();
64 case DumpTokens: return std::make_unique<DumpTokensAction>();
65 case EmitAssembly: return std::make_unique<EmitAssemblyAction>();
66 case EmitBC: return std::make_unique<EmitBCAction>();
67 case EmitCIR:
68#if CLANG_ENABLE_CIR
69 return std::make_unique<cir::EmitCIRAction>();
70#else
71 llvm_unreachable("CIR suppport not built into clang");
72#endif
73 case EmitHTML: return std::make_unique<HTMLPrintAction>();
74 case EmitLLVM: {
75#if CLANG_ENABLE_CIR
76 if (UseCIR)
77 return std::make_unique<cir::EmitLLVMAction>();
78#endif
79 return std::make_unique<EmitLLVMAction>();
80 }
81 case EmitLLVMOnly: return std::make_unique<EmitLLVMOnlyAction>();
82 case EmitCodeGenOnly: return std::make_unique<EmitCodeGenOnlyAction>();
83 case EmitObj: return std::make_unique<EmitObjAction>();
84 case ExtractAPI:
85 return std::make_unique<ExtractAPIAction>();
86 case FixIt: return std::make_unique<FixItAction>();
87 case GenerateModule:
88 return std::make_unique<GenerateModuleFromModuleMapAction>();
89 case GenerateModuleInterface:
90 return std::make_unique<GenerateModuleInterfaceAction>();
91 case GenerateReducedModuleInterface:
92 return std::make_unique<GenerateReducedModuleInterfaceAction>();
93 case GenerateHeaderUnit:
94 return std::make_unique<GenerateHeaderUnitAction>();
95 case GeneratePCH: return std::make_unique<GeneratePCHAction>();
96 case GenerateInterfaceStubs:
97 return std::make_unique<GenerateInterfaceStubsAction>();
98 case InitOnly: return std::make_unique<InitOnlyAction>();
99 case ParseSyntaxOnly: return std::make_unique<SyntaxOnlyAction>();
100 case ModuleFileInfo: return std::make_unique<DumpModuleInfoAction>();
101 case VerifyPCH: return std::make_unique<VerifyPCHAction>();
102 case TemplightDump: return std::make_unique<TemplightDumpAction>();
103
104 case PluginAction: {
105 for (const FrontendPluginRegistry::entry &Plugin :
106 FrontendPluginRegistry::entries()) {
107 if (Plugin.getName() == CI.getFrontendOpts().ActionName) {
108 std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
109 if ((P->getActionType() != PluginASTAction::ReplaceAction &&
110 P->getActionType() != PluginASTAction::CmdlineAfterMainAction) ||
111 !P->ParseArgs(
112 CI,
113 CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())]))
114 return nullptr;
115 return std::move(P);
116 }
117 }
118
119 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
121 return nullptr;
122 }
123
124 case PrintPreamble: return std::make_unique<PrintPreambleAction>();
125 case PrintPreprocessedInput: {
128 return std::make_unique<RewriteIncludesAction>();
129 return std::make_unique<PrintPreprocessedAction>();
130 }
131
132 case RewriteMacros: return std::make_unique<RewriteMacrosAction>();
133 case RewriteTest: return std::make_unique<RewriteTestAction>();
134#if CLANG_ENABLE_OBJC_REWRITER
135 case RewriteObjC: return std::make_unique<RewriteObjCAction>();
136#else
137 case RewriteObjC: Action = "RewriteObjC"; break;
138#endif
139#if CLANG_ENABLE_STATIC_ANALYZER
140 case RunAnalysis: return std::make_unique<ento::AnalysisAction>();
141#else
142 case RunAnalysis: Action = "RunAnalysis"; break;
143#endif
144 case RunPreprocessorOnly: return std::make_unique<PreprocessOnlyAction>();
145 case PrintDependencyDirectivesSourceMinimizerOutput:
146 return std::make_unique<PrintDependencyDirectivesSourceMinimizerAction>();
147 }
148
149#if !CLANG_ENABLE_STATIC_ANALYZER || !CLANG_ENABLE_OBJC_REWRITER
150 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
151 return 0;
152#else
153 llvm_unreachable("Invalid program action!");
154#endif
155}
156
157std::unique_ptr<FrontendAction>
159 // Create the underlying action.
160 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
161 if (!Act)
162 return nullptr;
163
164 const FrontendOptions &FEOpts = CI.getFrontendOpts();
165
166 if (FEOpts.FixAndRecompile) {
167 Act = std::make_unique<FixItRecompile>(std::move(Act));
168 }
169
170 // Wrap the base FE action in an extract api action to generate
171 // symbol graph as a biproduct of compilation (enabled with
172 // --emit-symbol-graph option)
173 if (FEOpts.EmitSymbolGraph) {
174 if (FEOpts.SymbolGraphOutputDir.empty()) {
175 CI.getDiagnostics().Report(diag::warn_missing_symbol_graph_dir);
177 }
178 CI.getCodeGenOpts().ClearASTBeforeBackend = false;
179 Act = std::make_unique<WrappingExtractAPIAction>(std::move(Act));
180 }
181
182 // If there are any AST files to merge, create a frontend action
183 // adaptor to perform the merge.
184 if (!FEOpts.ASTMergeFiles.empty())
185 Act = std::make_unique<ASTMergeAction>(std::move(Act),
186 FEOpts.ASTMergeFiles);
187
188 return Act;
189}
190
192 // Honor -help.
193 if (Clang->getFrontendOpts().ShowHelp) {
194 driver::getDriverOptTable().printHelp(
195 llvm::outs(), "clang -cc1 [options] file...",
196 "LLVM 'Clang' Compiler: http://clang.llvm.org",
197 /*ShowHidden=*/false, /*ShowAllAliases=*/false,
198 llvm::opt::Visibility(driver::options::CC1Option));
199 return true;
200 }
201
202 // Honor -version.
203 //
204 // FIXME: Use a better -version message?
205 if (Clang->getFrontendOpts().ShowVersion) {
206 llvm::cl::PrintVersionMessage();
207 return true;
208 }
209
210 Clang->LoadRequestedPlugins();
211
212 // Honor -mllvm.
213 //
214 // FIXME: Remove this, one day.
215 // This should happen AFTER plugins have been loaded!
216 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
217 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
218 auto Args = std::make_unique<const char*[]>(NumArgs + 2);
219 Args[0] = "clang (LLVM option parsing)";
220 for (unsigned i = 0; i != NumArgs; ++i)
221 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
222 Args[NumArgs + 1] = nullptr;
223 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
224 }
225
226#if CLANG_ENABLE_STATIC_ANALYZER
227 // These should happen AFTER plugins have been loaded!
228
229 AnalyzerOptions &AnOpts = Clang->getAnalyzerOpts();
230
231 // Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
232 if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||
234 ento::printCheckerHelp(llvm::outs(), *Clang);
235 return true;
236 }
237
238 // Honor -analyzer-checker-option-help.
241 ento::printCheckerConfigList(llvm::outs(), *Clang);
242 return true;
243 }
244
245 // Honor -analyzer-list-enabled-checkers.
246 if (AnOpts.ShowEnabledCheckerList) {
247 ento::printEnabledCheckerList(llvm::outs(), *Clang);
248 return true;
249 }
250
251 // Honor -analyzer-config-help.
252 if (AnOpts.ShowConfigOptionsList) {
253 ento::printAnalyzerConfigList(llvm::outs());
254 return true;
255 }
256#endif
257
258 // If there were errors in processing arguments, don't do anything else.
259 if (Clang->getDiagnostics().hasErrorOccurred())
260 return false;
261 // Create and execute the frontend action.
262 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
263 if (!Act)
264 return false;
265 bool Success = Clang->ExecuteAction(*Act);
266 if (Clang->getFrontendOpts().DisableFree)
267 llvm::BuryPointer(std::move(Act));
268 return Success;
269}
270
271} // namespace clang
StringRef P
This file defines the ExtractAPIAction and WrappingExtractAPIAction frontend actions.
Stores options for the analyzer from the command line.
unsigned ShowCheckerOptionDeveloperList
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
AnalyzerOptions & getAnalyzerOpts()
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
PreprocessorOutputOptions & getPreprocessorOutputOpts()
void LoadRequestedPlugins()
Load the list of plugins requested in the FrontendOptions.
FrontendOptions & getFrontendOpts()
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
CodeGenOptions & getCodeGenOpts()
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1497
bool hasErrorOccurred() const
Definition: Diagnostic.h:868
FrontendOptions - Options for controlling the behavior of the frontend.
unsigned EmitSymbolGraph
Whether to emit symbol graph files as a side effect of compilation.
std::map< std::string, std::vector< std::string > > PluginArgs
Args to pass to the plugins.
std::vector< std::string > LLVMArgs
A list of arguments to forward to LLVM's option processing; this should only be used for debugging an...
unsigned ShowHelp
Show the -help text.
unsigned FixAndRecompile
Apply fixes and recompile.
unsigned UseClangIRPipeline
Use Clang IR pipeline to emit code.
std::string ActionName
The name of the action to run when using a plugin action.
unsigned ShowVersion
Show the -version text.
std::string SymbolGraphOutputDir
std::vector< std::string > ASTMergeFiles
The list of AST files to merge.
unsigned DisableFree
Disable memory freeing on exit.
frontend::ActionKind ProgramAction
The frontend action to perform.
@ ReplaceAction
Replace the main action.
@ CmdlineAfterMainAction
Execute the action after the main action if on the command line.
unsigned RewriteIncludes
Preprocess include directives only.
unsigned RewriteImports
Include contents of transitively-imported modules.
const llvm::opt::OptTable & getDriverOptTable()
void printEnabledCheckerList(llvm::raw_ostream &OS, CompilerInstance &CI)
void printCheckerHelp(llvm::raw_ostream &OS, CompilerInstance &CI)
void printAnalyzerConfigList(llvm::raw_ostream &OS)
void printCheckerConfigList(llvm::raw_ostream &OS, CompilerInstance &CI)
The JSON file list parser is used to communicate input to InstallAPI.
static std::unique_ptr< FrontendAction > CreateFrontendBaseAction(CompilerInstance &CI)
bool ExecuteCompilerInvocation(CompilerInstance *Clang)
ExecuteCompilerInvocation - Execute the given actions described by the compiler invocation object in ...
std::unique_ptr< FrontendAction > CreateFrontendAction(CompilerInstance &CI)
Construct the FrontendAction of a compiler invocation based on the options specified for the compiler...
@ Success
Template argument deduction was successful.