clang  8.0.0
ExecuteCompilerInvocation.cpp
Go to the documentation of this file.
1 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file holds ExecuteCompilerInvocation(). It is split into its own file to
11 // minimize the impact of pulling in essentially everything else in Clang.
12 //
13 //===----------------------------------------------------------------------===//
14 
17 #include "clang/Config/config.h"
18 #include "clang/Driver/Options.h"
24 #include "clang/Frontend/Utils.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 using namespace clang;
34 using namespace llvm::opt;
35 
36 namespace clang {
37 
38 static std::unique_ptr<FrontendAction>
40  using namespace clang::frontend;
41  StringRef Action("unknown");
42  (void)Action;
43 
44  switch (CI.getFrontendOpts().ProgramAction) {
45  case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
46  case ASTDump: return llvm::make_unique<ASTDumpAction>();
47  case ASTPrint: return llvm::make_unique<ASTPrintAction>();
48  case ASTView: return llvm::make_unique<ASTViewAction>();
50  return llvm::make_unique<DumpCompilerOptionsAction>();
51  case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
52  case DumpTokens: return llvm::make_unique<DumpTokensAction>();
53  case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
54  case EmitBC: return llvm::make_unique<EmitBCAction>();
55  case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
56  case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
57  case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
58  case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
59  case EmitObj: return llvm::make_unique<EmitObjAction>();
60  case FixIt: return llvm::make_unique<FixItAction>();
61  case GenerateModule:
62  return llvm::make_unique<GenerateModuleFromModuleMapAction>();
64  return llvm::make_unique<GenerateModuleInterfaceAction>();
66  return llvm::make_unique<GenerateHeaderModuleAction>();
67  case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
68  case InitOnly: return llvm::make_unique<InitOnlyAction>();
69  case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
70  case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
71  case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
72  case TemplightDump: return llvm::make_unique<TemplightDumpAction>();
73 
74  case PluginAction: {
75  for (FrontendPluginRegistry::iterator it =
76  FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
77  it != ie; ++it) {
78  if (it->getName() == CI.getFrontendOpts().ActionName) {
79  std::unique_ptr<PluginASTAction> P(it->instantiate());
80  if ((P->getActionType() != PluginASTAction::ReplaceAction &&
81  P->getActionType() != PluginASTAction::Cmdline) ||
82  !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
83  return nullptr;
84  return std::move(P);
85  }
86  }
87 
88  CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
90  return nullptr;
91  }
92 
93  case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
97  return llvm::make_unique<RewriteIncludesAction>();
98  return llvm::make_unique<PrintPreprocessedAction>();
99  }
100 
101  case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
102  case RewriteTest: return llvm::make_unique<RewriteTestAction>();
103 #if CLANG_ENABLE_OBJC_REWRITER
104  case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
105 #else
106  case RewriteObjC: Action = "RewriteObjC"; break;
107 #endif
108 #if CLANG_ENABLE_ARCMT
109  case MigrateSource:
110  return llvm::make_unique<arcmt::MigrateSourceAction>();
111 #else
112  case MigrateSource: Action = "MigrateSource"; break;
113 #endif
114 #if CLANG_ENABLE_STATIC_ANALYZER
115  case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
116 #else
117  case RunAnalysis: Action = "RunAnalysis"; break;
118 #endif
119  case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
120  }
121 
122 #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
123  || !CLANG_ENABLE_OBJC_REWRITER
124  CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
125  return 0;
126 #else
127  llvm_unreachable("Invalid program action!");
128 #endif
129 }
130 
131 std::unique_ptr<FrontendAction>
133  // Create the underlying action.
134  std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
135  if (!Act)
136  return nullptr;
137 
138  const FrontendOptions &FEOpts = CI.getFrontendOpts();
139 
140  if (FEOpts.FixAndRecompile) {
141  Act = llvm::make_unique<FixItRecompile>(std::move(Act));
142  }
143 
144 #if CLANG_ENABLE_ARCMT
147  // Potentially wrap the base FE action in an ARC Migrate Tool action.
148  switch (FEOpts.ARCMTAction) {
150  break;
152  Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
153  break;
155  Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
156  break;
158  Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
159  FEOpts.MTMigrateDir,
160  FEOpts.ARCMTMigrateReportOut,
162  break;
163  }
164 
166  Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
167  FEOpts.MTMigrateDir,
168  FEOpts.ObjCMTAction);
169  }
170  }
171 #endif
172 
173  // If there are any AST files to merge, create a frontend action
174  // adaptor to perform the merge.
175  if (!FEOpts.ASTMergeFiles.empty())
176  Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
177  FEOpts.ASTMergeFiles);
178 
179  return Act;
180 }
181 
183  // Honor -help.
184  if (Clang->getFrontendOpts().ShowHelp) {
185  std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
186  Opts->PrintHelp(llvm::outs(), "clang -cc1 [options] file...",
187  "LLVM 'Clang' Compiler: http://clang.llvm.org",
188  /*Include=*/driver::options::CC1Option,
189  /*Exclude=*/0, /*ShowAllAliases=*/false);
190  return true;
191  }
192 
193  // Honor -version.
194  //
195  // FIXME: Use a better -version message?
196  if (Clang->getFrontendOpts().ShowVersion) {
197  llvm::cl::PrintVersionMessage();
198  return true;
199  }
200 
201  // Load any requested plugins.
202  for (unsigned i = 0,
203  e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
204  const std::string &Path = Clang->getFrontendOpts().Plugins[i];
205  std::string Error;
206  if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
207  Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
208  << Path << Error;
209  }
210 
211  // Check if any of the loaded plugins replaces the main AST action
212  for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
213  ie = FrontendPluginRegistry::end();
214  it != ie; ++it) {
215  std::unique_ptr<PluginASTAction> P(it->instantiate());
216  if (P->getActionType() == PluginASTAction::ReplaceAction) {
218  Clang->getFrontendOpts().ActionName = it->getName();
219  break;
220  }
221  }
222 
223  // Honor -mllvm.
224  //
225  // FIXME: Remove this, one day.
226  // This should happen AFTER plugins have been loaded!
227  if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
228  unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
229  auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
230  Args[0] = "clang (LLVM option parsing)";
231  for (unsigned i = 0; i != NumArgs; ++i)
232  Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
233  Args[NumArgs + 1] = nullptr;
234  llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
235  }
236 
237 #if CLANG_ENABLE_STATIC_ANALYZER
238  // Honor -analyzer-checker-help.
239  // This should happen AFTER plugins have been loaded!
240  if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
241  ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins,
242  Clang->getDiagnostics());
243  return true;
244  }
245 
246  // Honor -analyzer-list-enabled-checkers.
247  if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
248  ento::printEnabledCheckerList(llvm::outs(),
249  Clang->getFrontendOpts().Plugins,
250  *Clang->getAnalyzerOpts(),
251  Clang->getDiagnostics());
252  }
253 
254  // Honor -analyzer-config-help.
255  if (Clang->getAnalyzerOpts()->ShowConfigOptionsList) {
256  ento::printAnalyzerConfigList(llvm::outs());
257  return true;
258  }
259 #endif
260 
261  // If there were errors in processing arguments, don't do anything else.
262  if (Clang->getDiagnostics().hasErrorOccurred())
263  return false;
264  // Create and execute the frontend action.
265  std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
266  if (!Act)
267  return false;
268  bool Success = Clang->ExecuteAction(*Act);
269  if (Clang->getFrontendOpts().DisableFree)
270  llvm::BuryPointer(std::move(Act));
271  return Success;
272 }
273 
274 } // namespace clang
Expand macros but not #includes.
bool hasErrorOccurred() const
Definition: Diagnostic.h:747
Generate pre-compiled module from a module map.
Parse and perform semantic analysis.
Emit a .bc file.
std::unique_ptr< FrontendAction > CreateFrontendAction(CompilerInstance &CI)
Construct the FrontendAction of a compiler invocation based on the options specified for the compiler...
Parse ASTs and print them.
StringRef P
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1295
void printAnalyzerConfigList(raw_ostream &OS)
Parse and apply any fixits to the source.
Translate input source into HTML.
static std::unique_ptr< FrontendAction > CreateFrontendBaseAction(CompilerInstance &CI)
std::vector< std::string > ASTMergeFiles
The list of AST files to merge.
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins, DiagnosticsEngine &diags)
Action is determined by the cc1 command-line.
Generate LLVM IR, but do not emit anything.
FrontendOptions & getFrontendOpts()
PreprocessorOutputOptions & getPreprocessorOutputOpts()
std::unique_ptr< llvm::opt::OptTable > createDriverOptTable()
AnalyzerOptionsRef getAnalyzerOpts()
unsigned FixAndRecompile
Apply fixes and recompile.
Dump the compiler configuration.
Dump template instantiations.
Dump out preprocessed tokens.
Generate pre-compiled module from a C++ module interface file.
std::vector< std::string > Plugins
The list of plugins to load.
unsigned RewriteIncludes
Preprocess include directives only.
Only execute frontend initialization.
Print the "preamble" of the input file.
Rewriter playground.
enum clang::FrontendOptions::@191 ARCMTAction
unsigned ARCMTMigrateEmitARCErrors
Emit ARC errors even if the migrator can fix them.
void printEnabledCheckerList(raw_ostream &OS, ArrayRef< std::string > plugins, const AnalyzerOptions &opts, DiagnosticsEngine &diags)
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler&#39;s CompilerInvocation object...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Generate machine code, but don&#39;t emit anything.
Parse ASTs and view them in Graphviz.
Parse ASTs and list Decl nodes.
std::unordered_map< std::string, std::vector< std::string > > PluginArgs
Args to pass to the plugins.
Load and verify that a PCH file is usable.
unsigned ShowVersion
Show the -version text.
unsigned RewriteImports
Include contents of transitively-imported modules.
unsigned ShowHelp
Show the -help text.
Dataflow Directional Tag Classes.
frontend::ActionKind ProgramAction
The frontend action to perform.
std::string ARCMTMigrateReportOut
FrontendOptions - Options for controlling the behavior of the frontend.
Run a plugin action,.
Parse ASTs and dump them.
bool ExecuteCompilerInvocation(CompilerInstance *Clang)
ExecuteCompilerInvocation - Execute the given actions described by the compiler invocation object in ...
unsigned DisableFree
Disable memory freeing on exit.
Generate pre-compiled header.
Generate pre-compiled module from a set of header files.
std::string ActionName
The name of the action to run when using a plugin action.
Run one or more source code analyses.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
std::vector< std::string > LLVMArgs
A list of arguments to forward to LLVM&#39;s option processing; this should only be used for debugging an...
Dump information about a module file.