clang  3.7.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 
18 #include "clang/Driver/Options.h"
24 #include "clang/Frontend/Utils.h"
27 #include "llvm/Option/OptTable.h"
28 #include "llvm/Option/Option.h"
29 #include "llvm/Support/DynamicLibrary.h"
30 #include "llvm/Support/ErrorHandling.h"
31 using namespace clang;
32 using namespace llvm::opt;
33 
35  using namespace clang::frontend;
36  StringRef Action("unknown");
37  (void)Action;
38 
39  switch (CI.getFrontendOpts().ProgramAction) {
40  case ASTDeclList: return new ASTDeclListAction();
41  case ASTDump: return new ASTDumpAction();
42  case ASTPrint: return new ASTPrintAction();
43  case ASTView: return new ASTViewAction();
44  case DumpRawTokens: return new DumpRawTokensAction();
45  case DumpTokens: return new DumpTokensAction();
46  case EmitAssembly: return new EmitAssemblyAction();
47  case EmitBC: return new EmitBCAction();
48  case EmitHTML: return new HTMLPrintAction();
49  case EmitLLVM: return new EmitLLVMAction();
50  case EmitLLVMOnly: return new EmitLLVMOnlyAction();
51  case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
52  case EmitObj: return new EmitObjAction();
53  case FixIt: return new FixItAction();
54  case GenerateModule: return new GenerateModuleAction;
55  case GeneratePCH: return new GeneratePCHAction;
56  case GeneratePTH: return new GeneratePTHAction();
57  case InitOnly: return new InitOnlyAction();
58  case ParseSyntaxOnly: return new SyntaxOnlyAction();
59  case ModuleFileInfo: return new DumpModuleInfoAction();
60  case VerifyPCH: return new VerifyPCHAction();
61 
62  case PluginAction: {
63  for (FrontendPluginRegistry::iterator it =
64  FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
65  it != ie; ++it) {
66  if (it->getName() == CI.getFrontendOpts().ActionName) {
67  std::unique_ptr<PluginASTAction> P(it->instantiate());
68  if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
69  return nullptr;
70  return P.release();
71  }
72  }
73 
74  CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
76  return nullptr;
77  }
78 
79  case PrintDeclContext: return new DeclContextPrintAction();
80  case PrintPreamble: return new PrintPreambleAction();
83  return new RewriteIncludesAction();
84  return new PrintPreprocessedAction();
85  }
86 
87  case RewriteMacros: return new RewriteMacrosAction();
88  case RewriteTest: return new RewriteTestAction();
89 #ifdef CLANG_ENABLE_OBJC_REWRITER
90  case RewriteObjC: return new RewriteObjCAction();
91 #else
92  case RewriteObjC: Action = "RewriteObjC"; break;
93 #endif
94 #ifdef CLANG_ENABLE_ARCMT
95  case MigrateSource: return new arcmt::MigrateSourceAction();
96 #else
97  case MigrateSource: Action = "MigrateSource"; break;
98 #endif
99 #ifdef CLANG_ENABLE_STATIC_ANALYZER
100  case RunAnalysis: return new ento::AnalysisAction();
101 #else
102  case RunAnalysis: Action = "RunAnalysis"; break;
103 #endif
104  case RunPreprocessorOnly: return new PreprocessOnlyAction();
105  }
106 
107 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
108  || !defined(CLANG_ENABLE_OBJC_REWRITER)
109  CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
110  return 0;
111 #else
112  llvm_unreachable("Invalid program action!");
113 #endif
114 }
115 
117  // Create the underlying action.
119  if (!Act)
120  return nullptr;
121 
122  const FrontendOptions &FEOpts = CI.getFrontendOpts();
123 
124  if (FEOpts.FixAndRecompile) {
125  Act = new FixItRecompile(Act);
126  }
127 
128 #ifdef CLANG_ENABLE_ARCMT
131  // Potentially wrap the base FE action in an ARC Migrate Tool action.
132  switch (FEOpts.ARCMTAction) {
134  break;
136  Act = new arcmt::CheckAction(Act);
137  break;
139  Act = new arcmt::ModifyAction(Act);
140  break;
142  Act = new arcmt::MigrateAction(Act,
143  FEOpts.MTMigrateDir,
144  FEOpts.ARCMTMigrateReportOut,
146  break;
147  }
148 
150  Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
151  FEOpts.ObjCMTAction);
152  }
153  }
154 #endif
155 
156  // If there are any AST files to merge, create a frontend action
157  // adaptor to perform the merge.
158  if (!FEOpts.ASTMergeFiles.empty())
159  Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
160 
161  return Act;
162 }
163 
165  // Honor -help.
166  if (Clang->getFrontendOpts().ShowHelp) {
167  std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
168  Opts->PrintHelp(llvm::outs(), "clang -cc1",
169  "LLVM 'Clang' Compiler: http://clang.llvm.org",
170  /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0);
171  return true;
172  }
173 
174  // Honor -version.
175  //
176  // FIXME: Use a better -version message?
177  if (Clang->getFrontendOpts().ShowVersion) {
178  llvm::cl::PrintVersionMessage();
179  return true;
180  }
181 
182  // Load any requested plugins.
183  for (unsigned i = 0,
184  e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
185  const std::string &Path = Clang->getFrontendOpts().Plugins[i];
186  std::string Error;
187  if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
188  Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
189  << Path << Error;
190  }
191 
192  // Honor -mllvm.
193  //
194  // FIXME: Remove this, one day.
195  // This should happen AFTER plugins have been loaded!
196  if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
197  unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
198  auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
199  Args[0] = "clang (LLVM option parsing)";
200  for (unsigned i = 0; i != NumArgs; ++i)
201  Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
202  Args[NumArgs + 1] = nullptr;
203  llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
204  }
205 
206 #ifdef CLANG_ENABLE_STATIC_ANALYZER
207  // Honor -analyzer-checker-help.
208  // This should happen AFTER plugins have been loaded!
209  if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
210  ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
211  return true;
212  }
213 #endif
214 
215  // If there were errors in processing arguments, don't do anything else.
216  if (Clang->getDiagnostics().hasErrorOccurred())
217  return false;
218  // Create and execute the frontend action.
219  std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
220  if (!Act)
221  return false;
222  bool Success = Clang->ExecuteAction(*Act);
223  if (Clang->getFrontendOpts().DisableFree)
224  BuryPointer(std::move(Act));
225  return Success;
226 }
Expand macros but not #includes.
Generate pre-compiled module.
Parse and perform semantic analysis.
Emit a .bc file.
Dump information about the given module file, to be used for basic debugging and discovery.
Abstract base class for actions which can be performed by the frontend.
Parse ASTs and print them.
bool hasErrorOccurred() const
Definition: Diagnostic.h:573
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1118
Migrates to modern ObjC syntax.
Definition: ARCMTActions.h:58
std::vector< std::string > PluginArgs
Args to pass to the plugin.
Parse and apply any fixits to the source.
Translate input source into HTML.
FrontendAction * Action
Definition: Tooling.cpp:168
std::vector< std::string > ASTMergeFiles
The list of AST files to merge.
Print DeclContext and their Decls.
Generate LLVM IR, but do not emit anything.
FrontendOptions & getFrontendOpts()
PreprocessorOutputOptions & getPreprocessorOutputOpts()
AnalyzerOptionsRef getAnalyzerOpts()
unsigned FixAndRecompile
Apply fixes and recompile.
AnnotatingParser & P
Dump out preprocessed tokens.
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.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
bool ExecuteAction(FrontendAction &Act)
Frontend action adaptor that merges ASTs together.
Generate machine code, but don't emit anything.
Emits changes to temporary files and uses them for the original frontend action.
Parse ASTs and view them in Graphviz.
Parse ASTs and list Decl nodes.
static FrontendAction * CreateFrontendBaseAction(CompilerInstance &CI)
Load and verify that a PCH file is usable.
unsigned ShowVersion
Show the -version text.
unsigned ShowHelp
Show the -help text.
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins)
frontend::ActionKind ProgramAction
The frontend action to perform.
std::string ARCMTMigrateReportOut
enum clang::FrontendOptions::@156 ARCMTAction
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)
llvm::opt::OptTable * createDriverOptTable()
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.
std::vector< std::string > LLVMArgs
A list of arguments to forward to LLVM's option processing; this should only be used for debugging an...
Dump information about a module file.
Generate pre-tokenized header.
static FrontendAction * CreateFrontendAction(CompilerInstance &CI)