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 std::unique_ptr<FrontendAction>
35 CreateFrontendBaseAction(CompilerInstance &CI) {
36 using namespace clang::frontend;
37 StringRef Action("unknown");
40 switch (CI.getFrontendOpts().ProgramAction) {
41 case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
42 case ASTDump: return llvm::make_unique<ASTDumpAction>();
43 case ASTPrint: return llvm::make_unique<ASTPrintAction>();
44 case ASTView: return llvm::make_unique<ASTViewAction>();
45 case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
46 case DumpTokens: return llvm::make_unique<DumpTokensAction>();
47 case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
48 case EmitBC: return llvm::make_unique<EmitBCAction>();
49 case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
50 case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
51 case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
52 case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
53 case EmitObj: return llvm::make_unique<EmitObjAction>();
54 case FixIt: return llvm::make_unique<FixItAction>();
56 return llvm::make_unique<GenerateModuleFromModuleMapAction>();
57 case GenerateModuleInterface:
58 return llvm::make_unique<GenerateModuleInterfaceAction>();
59 case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
60 case GeneratePTH: return llvm::make_unique<GeneratePTHAction>();
61 case InitOnly: return llvm::make_unique<InitOnlyAction>();
62 case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
63 case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
64 case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
67 for (FrontendPluginRegistry::iterator it =
68 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
70 if (it->getName() == CI.getFrontendOpts().ActionName) {
71 std::unique_ptr<PluginASTAction> P(it->instantiate());
72 if ((P->getActionType() != PluginASTAction::ReplaceAction &&
73 P->getActionType() != PluginASTAction::Cmdline) ||
74 !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
80 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
81 << CI.getFrontendOpts().ActionName;
85 case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>();
86 case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
87 case PrintPreprocessedInput: {
88 if (CI.getPreprocessorOutputOpts().RewriteIncludes)
89 return llvm::make_unique<RewriteIncludesAction>();
90 return llvm::make_unique<PrintPreprocessedAction>();
93 case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
94 case RewriteTest: return llvm::make_unique<RewriteTestAction>();
95 #ifdef CLANG_ENABLE_OBJC_REWRITER
96 case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
98 case RewriteObjC: Action = "RewriteObjC"; break;
100 #ifdef CLANG_ENABLE_ARCMT
102 return llvm::make_unique<arcmt::MigrateSourceAction>();
104 case MigrateSource: Action = "MigrateSource"; break;
106 #ifdef CLANG_ENABLE_STATIC_ANALYZER
107 case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
109 case RunAnalysis: Action = "RunAnalysis"; break;
111 case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
114 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
115 || !defined(CLANG_ENABLE_OBJC_REWRITER)
116 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
119 llvm_unreachable("Invalid program action!");
123 static std::unique_ptr<FrontendAction>
124 CreateFrontendAction(CompilerInstance &CI) {
125 // Create the underlying action.
126 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
130 const FrontendOptions &FEOpts = CI.getFrontendOpts();
132 if (FEOpts.FixAndRecompile) {
133 Act = llvm::make_unique<FixItRecompile>(std::move(Act));
136 #ifdef CLANG_ENABLE_ARCMT
137 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
138 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
139 // Potentially wrap the base FE action in an ARC Migrate Tool action.
140 switch (FEOpts.ARCMTAction) {
141 case FrontendOptions::ARCMT_None:
143 case FrontendOptions::ARCMT_Check:
144 Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
146 case FrontendOptions::ARCMT_Modify:
147 Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
149 case FrontendOptions::ARCMT_Migrate:
150 Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
152 FEOpts.ARCMTMigrateReportOut,
153 FEOpts.ARCMTMigrateEmitARCErrors);
157 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
158 Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
160 FEOpts.ObjCMTAction);
165 // If there are any AST files to merge, create a frontend action
166 // adaptor to perform the merge.
167 if (!FEOpts.ASTMergeFiles.empty())
168 Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
169 FEOpts.ASTMergeFiles);
174 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
176 if (Clang->getFrontendOpts().ShowHelp) {
177 std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
178 Opts->PrintHelp(llvm::outs(), "clang -cc1",
179 "LLVM 'Clang' Compiler: http://clang.llvm.org",
180 /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0);
186 // FIXME: Use a better -version message?
187 if (Clang->getFrontendOpts().ShowVersion) {
188 llvm::cl::PrintVersionMessage();
192 // Load any requested plugins.
194 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
195 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
197 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
198 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
202 // Check if any of the loaded plugins replaces the main AST action
203 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
204 ie = FrontendPluginRegistry::end();
206 std::unique_ptr<PluginASTAction> P(it->instantiate());
207 if (P->getActionType() == PluginASTAction::ReplaceAction) {
208 Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
209 Clang->getFrontendOpts().ActionName = it->getName();
216 // FIXME: Remove this, one day.
217 // This should happen AFTER plugins have been loaded!
218 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
219 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
220 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
221 Args[0] = "clang (LLVM option parsing)";
222 for (unsigned i = 0; i != NumArgs; ++i)
223 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
224 Args[NumArgs + 1] = nullptr;
225 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
228 #ifdef CLANG_ENABLE_STATIC_ANALYZER
229 // Honor -analyzer-checker-help.
230 // This should happen AFTER plugins have been loaded!
231 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
232 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
235 if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
236 ento::printEnabledCheckerList(llvm::outs(),
237 Clang->getFrontendOpts().Plugins,
238 *Clang->getAnalyzerOpts());
242 // If there were errors in processing arguments, don't do anything else.
243 if (Clang->getDiagnostics().hasErrorOccurred())
245 // Create and execute the frontend action.
246 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
249 bool Success = Clang->ExecuteAction(*Act);
250 if (Clang->getFrontendOpts().DisableFree)
251 BuryPointer(std::move(Act));