clang  9.0.0svn
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"
23 #include "clang/Frontend/Utils.h"
27 #include "llvm/Option/OptTable.h"
28 #include "llvm/Option/Option.h"
29 #include "llvm/Support/BuryPointer.h"
30 #include "llvm/Support/DynamicLibrary.h"
31 #include "llvm/Support/ErrorHandling.h"
32 using namespace clang;
33 using namespace llvm::opt;
34 
35 namespace clang {
36 
37 static std::unique_ptr<FrontendAction>
39  using namespace clang::frontend;
40  StringRef Action("unknown");
41  (void)Action;
42 
43  switch (CI.getFrontendOpts().ProgramAction) {
44  case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
45  case ASTDump: return llvm::make_unique<ASTDumpAction>();
46  case ASTPrint: return llvm::make_unique<ASTPrintAction>();
47  case ASTView: return llvm::make_unique<ASTViewAction>();
49  return llvm::make_unique<DumpCompilerOptionsAction>();
50  case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
51  case DumpTokens: return llvm::make_unique<DumpTokensAction>();
52  case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
53  case EmitBC: return llvm::make_unique<EmitBCAction>();
54  case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
55  case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
56  case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
57  case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
58  case EmitObj: return llvm::make_unique<EmitObjAction>();
59  case FixIt: return llvm::make_unique<FixItAction>();
60  case GenerateModule:
61  return llvm::make_unique<GenerateModuleFromModuleMapAction>();
63  return llvm::make_unique<GenerateModuleInterfaceAction>();
65  return llvm::make_unique<GenerateHeaderModuleAction>();
66  case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
67  case InitOnly: return llvm::make_unique<InitOnlyAction>();
68  case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
69  case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
70  case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
71  case TemplightDump: return llvm::make_unique<TemplightDumpAction>();
72 
73  case PluginAction: {
74  for (FrontendPluginRegistry::iterator it =
75  FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
76  it != ie; ++it) {
77  if (it->getName() == CI.getFrontendOpts().ActionName) {
78  std::unique_ptr<PluginASTAction> P(it->instantiate());
79  if ((P->getActionType() != PluginASTAction::ReplaceAction &&
80  P->getActionType() != PluginASTAction::Cmdline) ||
81  !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
82  return nullptr;
83  return std::move(P);
84  }
85  }
86 
87  CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
89  return nullptr;
90  }
91 
92  case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
96  return llvm::make_unique<RewriteIncludesAction>();
97  return llvm::make_unique<PrintPreprocessedAction>();
98  }
99 
100  case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
101  case RewriteTest: return llvm::make_unique<RewriteTestAction>();
102 #if CLANG_ENABLE_OBJC_REWRITER
103  case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
104 #else
105  case RewriteObjC: Action = "RewriteObjC"; break;
106 #endif
107 #if CLANG_ENABLE_ARCMT
108  case MigrateSource:
109  return llvm::make_unique<arcmt::MigrateSourceAction>();
110 #else
111  case MigrateSource: Action = "MigrateSource"; break;
112 #endif
113 #if CLANG_ENABLE_STATIC_ANALYZER
114  case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
115 #else
116  case RunAnalysis: Action = "RunAnalysis"; break;
117 #endif
118  case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
119  }
120 
121 #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
122  || !CLANG_ENABLE_OBJC_REWRITER
123  CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
124  return 0;
125 #else
126  llvm_unreachable("Invalid program action!");
127 #endif
128 }
129 
130 std::unique_ptr<FrontendAction>
132  // Create the underlying action.
133  std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
134  if (!Act)
135  return nullptr;
136 
137  const FrontendOptions &FEOpts = CI.getFrontendOpts();
138 
139  if (FEOpts.FixAndRecompile) {
140  Act = llvm::make_unique<FixItRecompile>(std::move(Act));
141  }
142 
143 #if CLANG_ENABLE_ARCMT
146  // Potentially wrap the base FE action in an ARC Migrate Tool action.
147  switch (FEOpts.ARCMTAction) {
149  break;
151  Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
152  break;
154  Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
155  break;
157  Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
158  FEOpts.MTMigrateDir,
159  FEOpts.ARCMTMigrateReportOut,
161  break;
162  }
163 
165  Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
166  FEOpts.MTMigrateDir,
167  FEOpts.ObjCMTAction);
168  }
169  }
170 #endif
171 
172  // If there are any AST files to merge, create a frontend action
173  // adaptor to perform the merge.
174  if (!FEOpts.ASTMergeFiles.empty())
175  Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
176  FEOpts.ASTMergeFiles);
177 
178  return Act;
179 }
180 
182  // Honor -help.
183  if (Clang->getFrontendOpts().ShowHelp) {
184  std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
185  Opts->PrintHelp(llvm::outs(), "clang -cc1 [options] file...",
186  "LLVM 'Clang' Compiler: http://clang.llvm.org",
187  /*Include=*/driver::options::CC1Option,
188  /*Exclude=*/0, /*ShowAllAliases=*/false);
189  return true;
190  }
191 
192  // Honor -version.
193  //
194  // FIXME: Use a better -version message?
195  if (Clang->getFrontendOpts().ShowVersion) {
196  llvm::cl::PrintVersionMessage();
197  return true;
198  }
199 
200  // Load any requested plugins.
201  for (unsigned i = 0,
202  e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
203  const std::string &Path = Clang->getFrontendOpts().Plugins[i];
204  std::string Error;
205  if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
206  Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
207  << Path << Error;
208  }
209 
210  // Check if any of the loaded plugins replaces the main AST action
211  for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
212  ie = FrontendPluginRegistry::end();
213  it != ie; ++it) {
214  std::unique_ptr<PluginASTAction> P(it->instantiate());
215  if (P->getActionType() == PluginASTAction::ReplaceAction) {
217  Clang->getFrontendOpts().ActionName = it->getName();
218  break;
219  }
220  }
221 
222  // Honor -mllvm.
223  //
224  // FIXME: Remove this, one day.
225  // This should happen AFTER plugins have been loaded!
226  if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
227  unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
228  auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
229  Args[0] = "clang (LLVM option parsing)";
230  for (unsigned i = 0; i != NumArgs; ++i)
231  Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
232  Args[NumArgs + 1] = nullptr;
233  llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
234  }
235 
236 #if CLANG_ENABLE_STATIC_ANALYZER
237  // Honor -analyzer-checker-help.
238  // This should happen AFTER plugins have been loaded!
239  if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
240  ento::printCheckerHelp(llvm::outs(),
241  Clang->getFrontendOpts().Plugins,
242  *Clang->getAnalyzerOpts(),
243  Clang->getDiagnostics(),
244  Clang->getLangOpts());
245  return true;
246  }
247 
248  // Honor -analyzer-list-enabled-checkers.
249  if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
250  ento::printEnabledCheckerList(llvm::outs(),
251  Clang->getFrontendOpts().Plugins,
252  *Clang->getAnalyzerOpts(),
253  Clang->getDiagnostics(),
254  Clang->getLangOpts());
255  }
256 
257  // Honor -analyzer-config-help.
258  if (Clang->getAnalyzerOpts()->ShowConfigOptionsList) {
259  ento::printAnalyzerConfigList(llvm::outs());
260  return true;
261  }
262 #endif
263 
264  // If there were errors in processing arguments, don't do anything else.
265  if (Clang->getDiagnostics().hasErrorOccurred())
266  return false;
267  // Create and execute the frontend action.
268  std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
269  if (!Act)
270  return false;
271  bool Success = Clang->ExecuteAction(*Act);
272  if (Clang->getFrontendOpts().DisableFree)
273  llvm::BuryPointer(std::move(Act));
274  return Success;
275 }
276 
277 } // namespace clang
Expand macros but not #includes.
LangOptions & getLangOpts()
bool hasErrorOccurred() const
Definition: Diagnostic.h:748
void printEnabledCheckerList(raw_ostream &OS, ArrayRef< std::string > plugins, AnalyzerOptions &opts, DiagnosticsEngine &diags, const LangOptions &LangOpts)
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:1296
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.
Action is determined by the cc1 command-line.
Generate LLVM IR, but do not emit anything.
FrontendOptions & getFrontendOpts()
PreprocessorOutputOptions & getPreprocessorOutputOpts()
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins, AnalyzerOptions &opts, DiagnosticsEngine &diags, const LangOptions &LangOpts)
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.
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.