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/ARCMigrate/ARCMTActions.h"
17 #include "clang/CodeGen/CodeGenAction.h"
18 #include "clang/Driver/Options.h"
19 #include "clang/Frontend/CompilerInstance.h"
20 #include "clang/Frontend/CompilerInvocation.h"
21 #include "clang/Frontend/FrontendActions.h"
22 #include "clang/Frontend/FrontendDiagnostic.h"
23 #include "clang/Frontend/FrontendPluginRegistry.h"
24 #include "clang/Frontend/Utils.h"
25 #include "clang/Rewrite/Frontend/FrontendActions.h"
26 #include "clang/StaticAnalyzer/Frontend/FrontendActions.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;
34 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
35 using namespace clang::frontend;
36 StringRef Action("unknown");
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();
63 for (FrontendPluginRegistry::iterator it =
64 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
66 if (it->getName() == CI.getFrontendOpts().ActionName) {
67 std::unique_ptr<PluginASTAction> P(it->instantiate());
68 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
74 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
75 << CI.getFrontendOpts().ActionName;
79 case PrintDeclContext: return new DeclContextPrintAction();
80 case PrintPreamble: return new PrintPreambleAction();
81 case PrintPreprocessedInput: {
82 if (CI.getPreprocessorOutputOpts().RewriteIncludes)
83 return new RewriteIncludesAction();
84 return new PrintPreprocessedAction();
87 case RewriteMacros: return new RewriteMacrosAction();
88 case RewriteTest: return new RewriteTestAction();
89 #ifdef CLANG_ENABLE_OBJC_REWRITER
90 case RewriteObjC: return new RewriteObjCAction();
92 case RewriteObjC: Action = "RewriteObjC"; break;
94 #ifdef CLANG_ENABLE_ARCMT
95 case MigrateSource: return new arcmt::MigrateSourceAction();
97 case MigrateSource: Action = "MigrateSource"; break;
99 #ifdef CLANG_ENABLE_STATIC_ANALYZER
100 case RunAnalysis: return new ento::AnalysisAction();
102 case RunAnalysis: Action = "RunAnalysis"; break;
104 case RunPreprocessorOnly: return new PreprocessOnlyAction();
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;
112 llvm_unreachable("Invalid program action!");
116 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
117 // Create the underlying action.
118 FrontendAction *Act = CreateFrontendBaseAction(CI);
122 const FrontendOptions &FEOpts = CI.getFrontendOpts();
124 if (FEOpts.FixAndRecompile) {
125 Act = new FixItRecompile(Act);
128 #ifdef CLANG_ENABLE_ARCMT
129 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
130 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
131 // Potentially wrap the base FE action in an ARC Migrate Tool action.
132 switch (FEOpts.ARCMTAction) {
133 case FrontendOptions::ARCMT_None:
135 case FrontendOptions::ARCMT_Check:
136 Act = new arcmt::CheckAction(Act);
138 case FrontendOptions::ARCMT_Modify:
139 Act = new arcmt::ModifyAction(Act);
141 case FrontendOptions::ARCMT_Migrate:
142 Act = new arcmt::MigrateAction(Act,
144 FEOpts.ARCMTMigrateReportOut,
145 FEOpts.ARCMTMigrateEmitARCErrors);
149 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
150 Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
151 FEOpts.ObjCMTAction);
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);
164 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
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);
176 // FIXME: Use a better -version message?
177 if (Clang->getFrontendOpts().ShowVersion) {
178 llvm::cl::PrintVersionMessage();
182 // Load any requested plugins.
184 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
185 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
187 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
188 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
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());
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);
215 // If there were errors in processing arguments, don't do anything else.
216 if (Clang->getDiagnostics().hasErrorOccurred())
218 // Create and execute the frontend action.
219 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
222 bool Success = Clang->ExecuteAction(*Act);
223 if (Clang->getFrontendOpts().DisableFree)
224 BuryPointer(std::move(Act));