clang  15.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 
16 #include "clang/Config/config.h"
17 #include "clang/Driver/Options.h"
24 #include "clang/Frontend/Utils.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"
34 using namespace clang;
35 using namespace llvm::opt;
36 
37 namespace clang {
38 
39 static std::unique_ptr<FrontendAction>
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>();
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>();
67  return std::make_unique<GenerateModuleInterfaceAction>();
69  return std::make_unique<GenerateHeaderModuleAction>();
70  case GenerateHeaderUnit:
71  return std::make_unique<GenerateHeaderUnitAction>();
72  case GeneratePCH: return std::make_unique<GeneratePCHAction>();
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  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)
98  return nullptr;
99  }
100 
101  case PrintPreamble: return std::make_unique<PrintPreambleAction>();
102  case PrintPreprocessedInput: {
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>();
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 
141 std::unique_ptr<FrontendAction>
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>(std::move(Act));
152  }
153 
154 #if CLANG_ENABLE_ARCMT
157  // Potentially wrap the base FE action in an ARC Migrate Tool action.
158  switch (FEOpts.ARCMTAction) {
160  break;
162  Act = std::make_unique<arcmt::CheckAction>(std::move(Act));
163  break;
165  Act = std::make_unique<arcmt::ModifyAction>(std::move(Act));
166  break;
168  Act = std::make_unique<arcmt::MigrateAction>(std::move(Act),
169  FEOpts.MTMigrateDir,
170  FEOpts.ARCMTMigrateReportOut,
172  break;
173  }
174 
176  Act = std::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
177  FEOpts.MTMigrateDir,
178  FEOpts.ObjCMTAction);
179  }
180  }
181 #endif
182 
183  // If there are any AST files to merge, create a frontend action
184  // adaptor to perform the merge.
185  if (!FEOpts.ASTMergeFiles.empty())
186  Act = std::make_unique<ASTMergeAction>(std::move(Act),
187  FEOpts.ASTMergeFiles);
188 
189  return Act;
190 }
191 
193  // Honor -help.
194  if (Clang->getFrontendOpts().ShowHelp) {
195  driver::getDriverOptTable().printHelp(
196  llvm::outs(), "clang -cc1 [options] file...",
197  "LLVM 'Clang' Compiler: http://clang.llvm.org",
198  /*Include=*/driver::options::CC1Option,
199  /*Exclude=*/0, /*ShowAllAliases=*/false);
200  return true;
201  }
202 
203  // Honor -version.
204  //
205  // FIXME: Use a better -version message?
206  if (Clang->getFrontendOpts().ShowVersion) {
207  llvm::cl::PrintVersionMessage();
208  return true;
209  }
210 
211  Clang->LoadRequestedPlugins();
212 
213  // Honor -mllvm.
214  //
215  // FIXME: Remove this, one day.
216  // This should happen AFTER plugins have been loaded!
217  if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
218  unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
219  auto Args = std::make_unique<const char*[]>(NumArgs + 2);
220  Args[0] = "clang (LLVM option parsing)";
221  for (unsigned i = 0; i != NumArgs; ++i)
222  Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
223  Args[NumArgs + 1] = nullptr;
224  llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
225  }
226 
227 #if CLANG_ENABLE_STATIC_ANALYZER
228  // These should happen AFTER plugins have been loaded!
229 
230  AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts();
231 
232  // Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
233  if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||
234  AnOpts.ShowCheckerHelpDeveloper) {
235  ento::printCheckerHelp(llvm::outs(), *Clang);
236  return true;
237  }
238 
239  // Honor -analyzer-checker-option-help.
240  if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList ||
242  ento::printCheckerConfigList(llvm::outs(), *Clang);
243  return true;
244  }
245 
246  // Honor -analyzer-list-enabled-checkers.
247  if (AnOpts.ShowEnabledCheckerList) {
248  ento::printEnabledCheckerList(llvm::outs(), *Clang);
249  return true;
250  }
251 
252  // Honor -analyzer-config-help.
253  if (AnOpts.ShowConfigOptionsList) {
254  ento::printAnalyzerConfigList(llvm::outs());
255  return true;
256  }
257 #endif
258 
259  // If there were errors in processing arguments, don't do anything else.
260  if (Clang->getDiagnostics().hasErrorOccurred())
261  return false;
262  // Create and execute the frontend action.
263  std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
264  if (!Act)
265  return false;
266  bool Success = Clang->ExecuteAction(*Act);
267  if (Clang->getFrontendOpts().DisableFree)
268  llvm::BuryPointer(std::move(Act));
269  return Success;
270 }
271 
272 } // namespace clang
clang::ento::printAnalyzerConfigList
void printAnalyzerConfigList(llvm::raw_ostream &OS)
clang::AnalyzerOptions::ShowCheckerOptionDeveloperList
unsigned ShowCheckerOptionDeveloperList
Definition: AnalyzerOptions.h:240
clang::FrontendOptions::FixAndRecompile
unsigned FixAndRecompile
Apply fixes and recompile.
Definition: FrontendOptions.h:299
FrontendPluginRegistry.h
clang::frontend::EmitAssembly
@ EmitAssembly
Emit a .s file.
Definition: FrontendOptions.h:58
clang::FrontendOptions::ObjCMT_None
@ ObjCMT_None
Definition: FrontendOptions.h:363
Utils.h
clang::CompilerInstance::getPreprocessorOutputOpts
PreprocessorOutputOptions & getPreprocessorOutputOpts()
Definition: CompilerInstance.h:313
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::frontend::GenerateHeaderModule
@ GenerateHeaderModule
Generate pre-compiled module from a set of header files.
Definition: FrontendOptions.h:91
clang::frontend::RewriteTest
@ RewriteTest
Rewriter playground.
Definition: FrontendOptions.h:130
clang::FrontendOptions::ProgramAction
frontend::ActionKind ProgramAction
The frontend action to perform.
Definition: FrontendOptions.h:445
clang::PluginASTAction::ReplaceAction
@ ReplaceAction
Replace the main action.
Definition: FrontendAction.h:277
clang::FrontendOptions::LLVMArgs
std::vector< std::string > LLVMArgs
A list of arguments to forward to LLVM's option processing; this should only be used for debugging an...
Definition: FrontendOptions.h:481
clang::FrontendOptions::PluginArgs
std::map< std::string, std::vector< std::string > > PluginArgs
Args to pass to the plugins.
Definition: FrontendOptions.h:455
clang::FrontendOptions::ARCMTAction
enum clang::FrontendOptions::@188 ARCMTAction
clang::FrontendOptions::ARCMTMigrateReportOut
std::string ARCMTMigrateReportOut
Definition: FrontendOptions.h:419
clang::frontend::ModuleFileInfo
@ ModuleFileInfo
Dump information about a module file.
Definition: FrontendOptions.h:106
clang::frontend::RunPreprocessorOnly
@ RunPreprocessorOnly
Just lex, no output.
Definition: FrontendOptions.h:142
clang::CreateFrontendAction
std::unique_ptr< FrontendAction > CreateFrontendAction(CompilerInstance &CI)
Construct the FrontendAction of a compiler invocation based on the options specified for the compiler...
Definition: ExecuteCompilerInvocation.cpp:142
AnalyzerHelpFlags.h
clang::driver::options::CC1Option
@ CC1Option
Definition: Options.h:31
clang::frontend::ASTDump
@ ASTDump
Parse ASTs and dump them.
Definition: FrontendOptions.h:40
clang::CompilerInstance::getAnalyzerOpts
AnalyzerOptionsRef getAnalyzerOpts()
Definition: CompilerInstance.h:250
clang::frontend::VerifyPCH
@ VerifyPCH
Load and verify that a PCH file is usable.
Definition: FrontendOptions.h:109
clang::frontend::PluginAction
@ PluginAction
Run a plugin action,.
Definition: FrontendOptions.h:115
clang::frontend::RewriteObjC
@ RewriteObjC
ObjC->C Rewriter.
Definition: FrontendOptions.h:127
CompilerInvocation.h
CodeGenAction.h
clang::FrontendOptions::ShowVersion
unsigned ShowVersion
Show the -version text.
Definition: FrontendOptions.h:290
clang::frontend::GeneratePCH
@ GeneratePCH
Generate pre-compiled header.
Definition: FrontendOptions.h:97
clang::FrontendOptions::ARCMT_Modify
@ ARCMT_Modify
Definition: FrontendOptions.h:358
clang::frontend::ASTView
@ ASTView
Parse ASTs and view them in Graphviz.
Definition: FrontendOptions.h:46
clang::FrontendOptions::ShowHelp
unsigned ShowHelp
Show the -help text.
Definition: FrontendOptions.h:278
clang::AnalyzerOptions::ShowCheckerHelpAlpha
unsigned ShowCheckerHelpAlpha
Definition: AnalyzerOptions.h:235
clang::frontend::ASTPrint
@ ASTPrint
Parse ASTs and print them.
Definition: FrontendOptions.h:43
clang::frontend::RunAnalysis
@ RunAnalysis
Run one or more source code analyses.
Definition: FrontendOptions.h:133
clang::driver::getDriverOptTable
const llvm::opt::OptTable & getDriverOptTable()
Definition: DriverOptions.cpp:42
Options.h
clang::frontend::EmitObj
@ EmitObj
Emit a .o file.
Definition: FrontendOptions.h:76
clang::CompilerInstance::ExecuteAction
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
Definition: CompilerInstance.cpp:990
clang::DiagnosticsEngine::hasErrorOccurred
bool hasErrorOccurred() const
Definition: Diagnostic.h:837
clang::frontend::ParseSyntaxOnly
@ ParseSyntaxOnly
Parse and perform semantic analysis.
Definition: FrontendOptions.h:112
llvm::opt
Definition: DiagnosticOptions.h:19
clang::ento::printEnabledCheckerList
void printEnabledCheckerList(llvm::raw_ostream &OS, CompilerInstance &CI)
clang::FrontendOptions
FrontendOptions - Options for controlling the behavior of the frontend.
Definition: FrontendOptions.h:268
clang::AnalyzerOptions::ShowEnabledCheckerList
unsigned ShowEnabledCheckerList
Definition: AnalyzerOptions.h:242
Utils.h
clang::frontend::ASTDeclList
@ ASTDeclList
Parse ASTs and list Decl nodes.
Definition: FrontendOptions.h:37
clang::frontend::DumpRawTokens
@ DumpRawTokens
Dump out raw tokens.
Definition: FrontendOptions.h:52
clang::frontend::EmitLLVM
@ EmitLLVM
Emit a .ll file.
Definition: FrontendOptions.h:67
clang::PreprocessorOutputOptions::RewriteImports
unsigned RewriteImports
Include contents of transitively-imported modules.
Definition: PreprocessorOutputOptions.h:26
clang::FrontendOptions::ObjCMTAction
unsigned ObjCMTAction
Definition: FrontendOptions.h:415
clang::frontend::PrintDependencyDirectivesSourceMinimizerOutput
@ PrintDependencyDirectivesSourceMinimizerOutput
Print the output of the dependency directives source minimizer.
Definition: FrontendOptions.h:145
clang::FrontendOptions::ASTMergeFiles
std::vector< std::string > ASTMergeFiles
The list of AST files to merge.
Definition: FrontendOptions.h:477
clang::FrontendOptions::ARCMT_Migrate
@ ARCMT_Migrate
Definition: FrontendOptions.h:359
clang::AnalyzerOptions::ShowCheckerHelp
unsigned ShowCheckerHelp
Definition: AnalyzerOptions.h:234
clang::frontend::EmitHTML
@ EmitHTML
Translate input source into HTML.
Definition: FrontendOptions.h:64
clang::frontend::RewriteMacros
@ RewriteMacros
Expand macros but not #includes.
Definition: FrontendOptions.h:124
clang::PreprocessorOutputOptions::RewriteIncludes
unsigned RewriteIncludes
Preprocess include directives only.
Definition: PreprocessorOutputOptions.h:25
clang::FrontendOptions::ARCMT_None
@ ARCMT_None
Definition: FrontendOptions.h:356
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
clang::AnalyzerOptions::ShowCheckerOptionList
unsigned ShowCheckerOptionList
Definition: AnalyzerOptions.h:238
clang::ExecuteCompilerInvocation
bool ExecuteCompilerInvocation(CompilerInstance *Clang)
ExecuteCompilerInvocation - Execute the given actions described by the compiler invocation object in ...
Definition: ExecuteCompilerInvocation.cpp:192
clang::ento::printCheckerConfigList
void printCheckerConfigList(llvm::raw_ostream &OS, CompilerInstance &CI)
clang::frontend::EmitBC
@ EmitBC
Emit a .bc file.
Definition: FrontendOptions.h:61
clang::frontend::GenerateHeaderUnit
@ GenerateHeaderUnit
Generate a C++20 header unit module from a header file.
Definition: FrontendOptions.h:94
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::AnalyzerOptions::ShowCheckerHelpDeveloper
unsigned ShowCheckerHelpDeveloper
Definition: AnalyzerOptions.h:236
clang::CreateFrontendBaseAction
static std::unique_ptr< FrontendAction > CreateFrontendBaseAction(CompilerInstance &CI)
Definition: ExecuteCompilerInvocation.cpp:40
clang::frontend::GenerateModule
@ GenerateModule
Generate pre-compiled module from a module map.
Definition: FrontendOptions.h:85
clang::ento::printCheckerHelp
void printCheckerHelp(llvm::raw_ostream &OS, CompilerInstance &CI)
ARCMTActions.h
clang::frontend::FixIt
@ FixIt
Parse and apply any fixits to the source.
Definition: FrontendOptions.h:82
FrontendActions.h
FrontendActions.h
clang::frontend::ExtractAPI
@ ExtractAPI
Definition: FrontendOptions.h:79
clang::AnalyzerOptions
Stores options for the analyzer from the command line.
Definition: AnalyzerOptions.h:165
clang::FrontendOptions::ActionName
std::string ActionName
The name of the action to run when using a plugin action.
Definition: FrontendOptions.h:448
clang::AnalyzerOptions::ShowConfigOptionsList
unsigned ShowConfigOptionsList
Definition: AnalyzerOptions.h:243
clang::FrontendOptions::ARCMT_Check
@ ARCMT_Check
Definition: FrontendOptions.h:357
clang::FrontendOptions::ARCMTMigrateEmitARCErrors
unsigned ARCMTMigrateEmitARCErrors
Emit ARC errors even if the migrator can fix them.
Definition: FrontendOptions.h:305
clang
Definition: CalledOnceCheck.h:17
clang::frontend::DumpTokens
@ DumpTokens
Dump out preprocessed tokens.
Definition: FrontendOptions.h:55
FrontendDiagnostic.h
clang::frontend
Definition: FrontendOptions.h:33
clang::frontend::DumpCompilerOptions
@ DumpCompilerOptions
Dump the compiler configuration.
Definition: FrontendOptions.h:49
CompilerInstance.h
clang::FrontendOptions::MTMigrateDir
std::string MTMigrateDir
Definition: FrontendOptions.h:418
clang::CompilerInstance::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInstance.h:282
clang::FrontendOptions::DisableFree
unsigned DisableFree
Disable memory freeing on exit.
Definition: FrontendOptions.h:271
clang::frontend::PrintPreprocessedInput
@ PrintPreprocessedInput
-E mode.
Definition: FrontendOptions.h:121
clang::CompilerInstance::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
Definition: CompilerInstance.h:334
clang::PluginASTAction::CmdlineAfterMainAction
@ CmdlineAfterMainAction
Execute the action after the main action if on the command line.
Definition: FrontendAction.h:275
FrontendActions.h
clang::frontend::EmitLLVMOnly
@ EmitLLVMOnly
Generate LLVM IR, but do not emit anything.
Definition: FrontendOptions.h:70
clang::frontend::MigrateSource
@ MigrateSource
Run migrator.
Definition: FrontendOptions.h:139
FrontendActions.h
clang::frontend::GenerateInterfaceStubs
@ GenerateInterfaceStubs
Generate Interface Stub Files.
Definition: FrontendOptions.h:100
clang::frontend::InitOnly
@ InitOnly
Only execute frontend initialization.
Definition: FrontendOptions.h:103
clang::AnalyzerOptions::ShowCheckerOptionAlphaList
unsigned ShowCheckerOptionAlphaList
Definition: AnalyzerOptions.h:239
clang::frontend::PrintPreamble
@ PrintPreamble
Print the "preamble" of the input file.
Definition: FrontendOptions.h:118
clang::frontend::TemplightDump
@ TemplightDump
Dump template instantiations.
Definition: FrontendOptions.h:136
clang::frontend::GenerateModuleInterface
@ GenerateModuleInterface
Generate pre-compiled module from a C++ module interface file.
Definition: FrontendOptions.h:88
clang::CompilerInstance::LoadRequestedPlugins
void LoadRequestedPlugins()
Load the list of plugins requested in the FrontendOptions.
Definition: CompilerInstance.cpp:1091
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1536
clang::frontend::EmitCodeGenOnly
@ EmitCodeGenOnly
Generate machine code, but don't emit anything.
Definition: FrontendOptions.h:73