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

source code of clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp