1 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "clang/FrontendTool/Utils.h"
16 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
17 #include "clang/ARCMigrate/ARCMTActions.h"
18 #include "clang/CodeGen/CodeGenAction.h"
19 #include "clang/Driver/Option.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Driver/OptTable.h"
22 #include "clang/Frontend/CompilerInvocation.h"
23 #include "clang/Frontend/CompilerInstance.h"
24 #include "clang/Frontend/FrontendActions.h"
25 #include "clang/Frontend/FrontendDiagnostic.h"
26 #include "clang/Frontend/FrontendPluginRegistry.h"
27 #include "clang/Rewrite/Frontend/FrontendActions.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/DynamicLibrary.h"
30 using namespace clang;
32 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
33 using namespace clang::frontend;
35 switch (CI.getFrontendOpts().ProgramAction) {
36 case ASTDeclList: return new ASTDeclListAction();
37 case ASTDump: return new ASTDumpAction();
38 case ASTDumpXML: return new ASTDumpXMLAction();
39 case ASTPrint: return new ASTPrintAction();
40 case ASTView: return new ASTViewAction();
41 case DumpRawTokens: return new DumpRawTokensAction();
42 case DumpTokens: return new DumpTokensAction();
43 case EmitAssembly: return new EmitAssemblyAction();
44 case EmitBC: return new EmitBCAction();
45 case EmitHTML: return new HTMLPrintAction();
46 case EmitLLVM: return new EmitLLVMAction();
47 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
48 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
49 case EmitObj: return new EmitObjAction();
50 case FixIt: return new FixItAction();
51 case GenerateModule: return new GenerateModuleAction;
52 case GeneratePCH: return new GeneratePCHAction;
53 case GeneratePTH: return new GeneratePTHAction();
54 case InitOnly: return new InitOnlyAction();
55 case ParseSyntaxOnly: return new SyntaxOnlyAction();
58 for (FrontendPluginRegistry::iterator it =
59 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
61 if (it->getName() == CI.getFrontendOpts().ActionName) {
62 OwningPtr<PluginASTAction> P(it->instantiate());
63 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
69 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
70 << CI.getFrontendOpts().ActionName;
74 case PrintDeclContext: return new DeclContextPrintAction();
75 case PrintPreamble: return new PrintPreambleAction();
76 case PrintPreprocessedInput: {
77 if (CI.getPreprocessorOutputOpts().RewriteIncludes)
78 return new RewriteIncludesAction();
79 return new PrintPreprocessedAction();
82 case RewriteMacros: return new RewriteMacrosAction();
83 case RewriteObjC: return new RewriteObjCAction();
84 case RewriteTest: return new RewriteTestAction();
85 case RunAnalysis: return new ento::AnalysisAction();
86 case MigrateSource: return new arcmt::MigrateSourceAction();
87 case RunPreprocessorOnly: return new PreprocessOnlyAction();
89 llvm_unreachable("Invalid program action!");
92 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
93 // Create the underlying action.
94 FrontendAction *Act = CreateFrontendBaseAction(CI);
98 const FrontendOptions &FEOpts = CI.getFrontendOpts();
100 if (FEOpts.FixAndRecompile) {
101 Act = new FixItRecompile(Act);
104 // Potentially wrap the base FE action in an ARC Migrate Tool action.
105 switch (FEOpts.ARCMTAction) {
106 case FrontendOptions::ARCMT_None:
108 case FrontendOptions::ARCMT_Check:
109 Act = new arcmt::CheckAction(Act);
111 case FrontendOptions::ARCMT_Modify:
112 Act = new arcmt::ModifyAction(Act);
114 case FrontendOptions::ARCMT_Migrate:
115 Act = new arcmt::MigrateAction(Act,
117 FEOpts.ARCMTMigrateReportOut,
118 FEOpts.ARCMTMigrateEmitARCErrors);
122 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
123 Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
124 FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Literals,
125 FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Subscripting);
128 // If there are any AST files to merge, create a frontend action
129 // adaptor to perform the merge.
130 if (!FEOpts.ASTMergeFiles.empty())
131 Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
136 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
138 if (Clang->getFrontendOpts().ShowHelp) {
139 OwningPtr<driver::OptTable> Opts(driver::createDriverOptTable());
140 Opts->PrintHelp(llvm::outs(), "clang -cc1",
141 "LLVM 'Clang' Compiler: http://clang.llvm.org",
142 /*Include=*/driver::options::CC1Option,
149 // FIXME: Use a better -version message?
150 if (Clang->getFrontendOpts().ShowVersion) {
151 llvm::cl::PrintVersionMessage();
155 // Load any requested plugins.
157 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
158 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
160 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
161 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
167 // FIXME: Remove this, one day.
168 // This should happen AFTER plugins have been loaded!
169 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
170 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
171 const char **Args = new const char*[NumArgs + 2];
172 Args[0] = "clang (LLVM option parsing)";
173 for (unsigned i = 0; i != NumArgs; ++i)
174 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
175 Args[NumArgs + 1] = 0;
176 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
179 // Honor -analyzer-checker-help.
180 // This should happen AFTER plugins have been loaded!
181 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
182 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
186 // If there were errors in processing arguments, don't do anything else.
187 bool Success = false;
188 if (!Clang->getDiagnostics().hasErrorOccurred()) {
189 // Create and execute the frontend action.
190 OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
192 Success = Clang->ExecuteAction(*Act);
193 if (Clang->getFrontendOpts().DisableFree)