clang  6.0.0svn
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 
18 #include "clang/Config/config.h"
19 #include "clang/Driver/Options.h"
25 #include "clang/Frontend/Utils.h"
28 #include "llvm/Option/OptTable.h"
29 #include "llvm/Option/Option.h"
30 #include "llvm/Support/DynamicLibrary.h"
31 #include "llvm/Support/ErrorHandling.h"
32 using namespace clang;
33 using namespace llvm::opt;
34 
35 static std::unique_ptr<FrontendAction>
37  using namespace clang::frontend;
38  StringRef Action("unknown");
39  (void)Action;
40 
41  switch (CI.getFrontendOpts().ProgramAction) {
42  case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
43  case ASTDump: return llvm::make_unique<ASTDumpAction>();
44  case ASTPrint: return llvm::make_unique<ASTPrintAction>();
45  case ASTView: return llvm::make_unique<ASTViewAction>();
46  case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
47  case DumpTokens: return llvm::make_unique<DumpTokensAction>();
48  case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
49  case EmitBC: return llvm::make_unique<EmitBCAction>();
50  case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
51  case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
52  case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
53  case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
54  case EmitObj: return llvm::make_unique<EmitObjAction>();
55  case FixIt: return llvm::make_unique<FixItAction>();
56  case GenerateModule:
57  return llvm::make_unique<GenerateModuleFromModuleMapAction>();
59  return llvm::make_unique<GenerateModuleInterfaceAction>();
60  case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
61  case GeneratePTH: return llvm::make_unique<GeneratePTHAction>();
62  case InitOnly: return llvm::make_unique<InitOnlyAction>();
63  case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
64  case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
65  case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
66 
67  case PluginAction: {
68  for (FrontendPluginRegistry::iterator it =
69  FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
70  it != ie; ++it) {
71  if (it->getName() == CI.getFrontendOpts().ActionName) {
72  std::unique_ptr<PluginASTAction> P(it->instantiate());
73  if ((P->getActionType() != PluginASTAction::ReplaceAction &&
74  P->getActionType() != PluginASTAction::Cmdline) ||
75  !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
76  return nullptr;
77  return std::move(P);
78  }
79  }
80 
81  CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
83  return nullptr;
84  }
85 
86  case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>();
87  case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
91  return llvm::make_unique<RewriteIncludesAction>();
92  return llvm::make_unique<PrintPreprocessedAction>();
93  }
94 
95  case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
96  case RewriteTest: return llvm::make_unique<RewriteTestAction>();
97 #ifdef CLANG_ENABLE_OBJC_REWRITER
98  case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
99 #else
100  case RewriteObjC: Action = "RewriteObjC"; break;
101 #endif
102 #ifdef CLANG_ENABLE_ARCMT
103  case MigrateSource:
104  return llvm::make_unique<arcmt::MigrateSourceAction>();
105 #else
106  case MigrateSource: Action = "MigrateSource"; break;
107 #endif
108 #ifdef CLANG_ENABLE_STATIC_ANALYZER
109  case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
110 #else
111  case RunAnalysis: Action = "RunAnalysis"; break;
112 #endif
113  case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
114  }
115 
116 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
117  || !defined(CLANG_ENABLE_OBJC_REWRITER)
118  CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
119  return 0;
120 #else
121  llvm_unreachable("Invalid program action!");
122 #endif
123 }
124 
125 static std::unique_ptr<FrontendAction>
127  // Create the underlying action.
128  std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
129  if (!Act)
130  return nullptr;
131 
132  const FrontendOptions &FEOpts = CI.getFrontendOpts();
133 
134  if (FEOpts.FixAndRecompile) {
135  Act = llvm::make_unique<FixItRecompile>(std::move(Act));
136  }
137 
138 #ifdef CLANG_ENABLE_ARCMT
141  // Potentially wrap the base FE action in an ARC Migrate Tool action.
142  switch (FEOpts.ARCMTAction) {
144  break;
146  Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
147  break;
149  Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
150  break;
152  Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
153  FEOpts.MTMigrateDir,
154  FEOpts.ARCMTMigrateReportOut,
156  break;
157  }
158 
160  Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
161  FEOpts.MTMigrateDir,
162  FEOpts.ObjCMTAction);
163  }
164  }
165 #endif
166 
167  // If there are any AST files to merge, create a frontend action
168  // adaptor to perform the merge.
169  if (!FEOpts.ASTMergeFiles.empty())
170  Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
171  FEOpts.ASTMergeFiles);
172 
173  return Act;
174 }
175 
177  // Honor -help.
178  if (Clang->getFrontendOpts().ShowHelp) {
179  std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
180  Opts->PrintHelp(llvm::outs(), "clang -cc1",
181  "LLVM 'Clang' Compiler: http://clang.llvm.org",
182  /*Include=*/driver::options::CC1Option,
183  /*Exclude=*/0, /*ShowAllAliases=*/false);
184  return true;
185  }
186 
187  // Honor -version.
188  //
189  // FIXME: Use a better -version message?
190  if (Clang->getFrontendOpts().ShowVersion) {
191  llvm::cl::PrintVersionMessage();
192  return true;
193  }
194 
195  // Load any requested plugins.
196  for (unsigned i = 0,
197  e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
198  const std::string &Path = Clang->getFrontendOpts().Plugins[i];
199  std::string Error;
200  if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
201  Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
202  << Path << Error;
203  }
204 
205  // Check if any of the loaded plugins replaces the main AST action
206  for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
207  ie = FrontendPluginRegistry::end();
208  it != ie; ++it) {
209  std::unique_ptr<PluginASTAction> P(it->instantiate());
210  if (P->getActionType() == PluginASTAction::ReplaceAction) {
212  Clang->getFrontendOpts().ActionName = it->getName();
213  break;
214  }
215  }
216 
217  // Honor -mllvm.
218  //
219  // FIXME: Remove this, one day.
220  // This should happen AFTER plugins have been loaded!
221  if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
222  unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
223  auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
224  Args[0] = "clang (LLVM option parsing)";
225  for (unsigned i = 0; i != NumArgs; ++i)
226  Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
227  Args[NumArgs + 1] = nullptr;
228  llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
229  }
230 
231 #ifdef CLANG_ENABLE_STATIC_ANALYZER
232  // Honor -analyzer-checker-help.
233  // This should happen AFTER plugins have been loaded!
234  if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
235  ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
236  return true;
237  }
238  if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
239  ento::printEnabledCheckerList(llvm::outs(),
240  Clang->getFrontendOpts().Plugins,
241  *Clang->getAnalyzerOpts());
242  }
243 #endif
244 
245  // If there were errors in processing arguments, don't do anything else.
246  if (Clang->getDiagnostics().hasErrorOccurred())
247  return false;
248  // Create and execute the frontend action.
249  std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
250  if (!Act)
251  return false;
252  bool Success = Clang->ExecuteAction(*Act);
253  if (Clang->getFrontendOpts().DisableFree)
254  BuryPointer(std::move(Act));
255  return Success;
256 }
Expand macros but not #includes.
bool hasErrorOccurred() const
Definition: Diagnostic.h:660
Generate pre-compiled module from a module map.
Parse and perform semantic analysis.
Emit a .bc file.
Parse ASTs and print them.
StringRef P
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1205
void printEnabledCheckerList(raw_ostream &OS, ArrayRef< std::string > plugins, const AnalyzerOptions &opts)
enum clang::FrontendOptions::@168 ARCMTAction
Parse and apply any fixits to the source.
static std::unique_ptr< FrontendAction > CreateFrontendAction(CompilerInstance &CI)
Translate input source into HTML.
std::vector< std::string > ASTMergeFiles
The list of AST files to merge.
Print DeclContext and their Decls.
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 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.
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.
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins)
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,.
void BuryPointer(const void *Ptr)
Parse ASTs and dump them.
bool ExecuteCompilerInvocation(CompilerInstance *Clang)
ExecuteCompilerInvocation - Execute the given actions described by the compiler invocation object in ...
static std::unique_ptr< FrontendAction > CreateFrontendBaseAction(CompilerInstance &CI)
unsigned DisableFree
Disable memory freeing on exit.
Generate pre-compiled header.
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.
Generate pre-tokenized header.