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;
34 StringRef Action("unknown");
36 switch (CI.getFrontendOpts().ProgramAction) {
37 case ASTDeclList: return new ASTDeclListAction();
38 case ASTDump: return new ASTDumpAction();
39 case ASTDumpXML: return new ASTDumpXMLAction();
40 case ASTPrint: return new ASTPrintAction();
41 case ASTView: return new ASTViewAction();
42 case DumpRawTokens: return new DumpRawTokensAction();
43 case DumpTokens: return new DumpTokensAction();
44 case EmitAssembly: return new EmitAssemblyAction();
45 case EmitBC: return new EmitBCAction();
46 #ifdef CLANG_ENABLE_REWRITER
47 case EmitHTML: return new HTMLPrintAction();
49 case EmitHTML: Action = "EmitHTML"; break;
51 case EmitLLVM: return new EmitLLVMAction();
52 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
53 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
54 case EmitObj: return new EmitObjAction();
55 #ifdef CLANG_ENABLE_REWRITER
56 case FixIt: return new FixItAction();
58 case FixIt: Action = "FixIt"; break;
60 case GenerateModule: return new GenerateModuleAction;
61 case GeneratePCH: return new GeneratePCHAction;
62 case GeneratePTH: return new GeneratePTHAction();
63 case InitOnly: return new InitOnlyAction();
64 case ParseSyntaxOnly: return new SyntaxOnlyAction();
67 for (FrontendPluginRegistry::iterator it =
68 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
70 if (it->getName() == CI.getFrontendOpts().ActionName) {
71 OwningPtr<PluginASTAction> P(it->instantiate());
72 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
78 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
79 << CI.getFrontendOpts().ActionName;
83 case PrintDeclContext: return new DeclContextPrintAction();
84 case PrintPreamble: return new PrintPreambleAction();
85 case PrintPreprocessedInput: {
86 if (CI.getPreprocessorOutputOpts().RewriteIncludes) {
87 #ifdef CLANG_ENABLE_REWRITER
88 return new RewriteIncludesAction();
90 Action = "RewriteIncludesAction";
94 return new PrintPreprocessedAction();
97 #ifdef CLANG_ENABLE_REWRITER
98 case RewriteMacros: return new RewriteMacrosAction();
99 case RewriteObjC: return new RewriteObjCAction();
100 case RewriteTest: return new RewriteTestAction();
102 case RewriteMacros: Action = "RewriteMacros"; break;
103 case RewriteObjC: Action = "RewriteObjC"; break;
104 case RewriteTest: Action = "RewriteTest"; break;
106 #ifdef CLANG_ENABLE_ARCMT
107 case MigrateSource: return new arcmt::MigrateSourceAction();
109 case MigrateSource: Action = "MigrateSource"; break;
111 #ifdef CLANG_ENABLE_STATIC_ANALYZER
112 case RunAnalysis: return new ento::AnalysisAction();
114 case RunAnalysis: Action = "RunAnalysis"; break;
116 case RunPreprocessorOnly: return new PreprocessOnlyAction();
119 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
120 || !defined(CLANG_ENABLE_REWRITER)
121 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
124 llvm_unreachable("Invalid program action!");
128 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
129 // Create the underlying action.
130 FrontendAction *Act = CreateFrontendBaseAction(CI);
134 const FrontendOptions &FEOpts = CI.getFrontendOpts();
136 #ifdef CLANG_ENABLE_REWRITER
137 if (FEOpts.FixAndRecompile) {
138 Act = new FixItRecompile(Act);
142 #ifdef CLANG_ENABLE_ARCMT
143 // Potentially wrap the base FE action in an ARC Migrate Tool action.
144 switch (FEOpts.ARCMTAction) {
145 case FrontendOptions::ARCMT_None:
147 case FrontendOptions::ARCMT_Check:
148 Act = new arcmt::CheckAction(Act);
150 case FrontendOptions::ARCMT_Modify:
151 Act = new arcmt::ModifyAction(Act);
153 case FrontendOptions::ARCMT_Migrate:
154 Act = new arcmt::MigrateAction(Act,
156 FEOpts.ARCMTMigrateReportOut,
157 FEOpts.ARCMTMigrateEmitARCErrors);
161 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
162 Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
163 FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Literals,
164 FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Subscripting);
168 // If there are any AST files to merge, create a frontend action
169 // adaptor to perform the merge.
170 if (!FEOpts.ASTMergeFiles.empty())
171 Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
176 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
178 if (Clang->getFrontendOpts().ShowHelp) {
179 OwningPtr<driver::OptTable> Opts(driver::createDriverOptTable());
180 Opts->PrintHelp(llvm::outs(), "clang -cc1",
181 "LLVM 'Clang' Compiler: http://clang.llvm.org",
182 /*Include=*/driver::options::CC1Option,
189 // FIXME: Use a better -version message?
190 if (Clang->getFrontendOpts().ShowVersion) {
191 llvm::cl::PrintVersionMessage();
195 // Load any requested plugins.
197 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
198 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
200 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
201 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
207 // FIXME: Remove this, one day.
208 // This should happen AFTER plugins have been loaded!
209 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
210 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
211 const char **Args = new const char*[NumArgs + 2];
212 Args[0] = "clang (LLVM option parsing)";
213 for (unsigned i = 0; i != NumArgs; ++i)
214 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
215 Args[NumArgs + 1] = 0;
216 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
219 #ifdef CLANG_ENABLE_STATIC_ANALYZER
220 // Honor -analyzer-checker-help.
221 // This should happen AFTER plugins have been loaded!
222 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
223 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
228 // If there were errors in processing arguments, don't do anything else.
229 bool Success = false;
230 if (!Clang->getDiagnostics().hasErrorOccurred()) {
231 // Create and execute the frontend action.
232 OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
234 Success = Clang->ExecuteAction(*Act);
235 if (Clang->getFrontendOpts().DisableFree)