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/Config/config.h"
19 #include "clang/Driver/Options.h"
20 #include "clang/Frontend/CompilerInstance.h"
21 #include "clang/Frontend/CompilerInvocation.h"
22 #include "clang/Frontend/FrontendActions.h"
23 #include "clang/Frontend/FrontendDiagnostic.h"
24 #include "clang/Frontend/FrontendPluginRegistry.h"
25 #include "clang/Frontend/Utils.h"
26 #include "clang/Rewrite/Frontend/FrontendActions.h"
27 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
28 #include "llvm/Option/OptTable.h"
29 #include "llvm/Option/Option.h"
30 #include "llvm/Support/DynamicLibrary.h"
31 #include "llvm/Support/ErrorHandling.h"
32 using namespace clang;
33 using namespace llvm::opt;
35 static std::unique_ptr<FrontendAction>
36 CreateFrontendBaseAction(CompilerInstance &CI) {
37 using namespace clang::frontend;
38 StringRef Action("unknown");
41 switch (CI.getFrontendOpts().ProgramAction) {
42 case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
43 case ASTDump: return llvm::make_unique<ASTDumpAction>();
44 case ASTPrint: return llvm::make_unique<ASTPrintAction>();
45 case ASTView: return llvm::make_unique<ASTViewAction>();
46 case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
47 case DumpTokens: return llvm::make_unique<DumpTokensAction>();
48 case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
49 case EmitBC: return llvm::make_unique<EmitBCAction>();
50 case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
51 case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
52 case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
53 case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
54 case EmitObj: return llvm::make_unique<EmitObjAction>();
55 case FixIt: return llvm::make_unique<FixItAction>();
57 return llvm::make_unique<GenerateModuleFromModuleMapAction>();
58 case GenerateModuleInterface:
59 return llvm::make_unique<GenerateModuleInterfaceAction>();
60 case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
61 case GeneratePTH: return llvm::make_unique<GeneratePTHAction>();
62 case InitOnly: return llvm::make_unique<InitOnlyAction>();
63 case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
64 case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
65 case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
68 for (FrontendPluginRegistry::iterator it =
69 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
71 if (it->getName() == CI.getFrontendOpts().ActionName) {
72 std::unique_ptr<PluginASTAction> P(it->instantiate());
73 if ((P->getActionType() != PluginASTAction::ReplaceAction &&
74 P->getActionType() != PluginASTAction::Cmdline) ||
75 !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
81 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
82 << CI.getFrontendOpts().ActionName;
86 case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>();
87 case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
88 case PrintPreprocessedInput: {
89 if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
90 CI.getPreprocessorOutputOpts().RewriteImports)
91 return llvm::make_unique<RewriteIncludesAction>();
92 return llvm::make_unique<PrintPreprocessedAction>();
95 case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
96 case RewriteTest: return llvm::make_unique<RewriteTestAction>();
97 #ifdef CLANG_ENABLE_OBJC_REWRITER
98 case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
100 case RewriteObjC: Action = "RewriteObjC"; break;
102 #ifdef CLANG_ENABLE_ARCMT
104 return llvm::make_unique<arcmt::MigrateSourceAction>();
106 case MigrateSource: Action = "MigrateSource"; break;
108 #ifdef CLANG_ENABLE_STATIC_ANALYZER
109 case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
111 case RunAnalysis: Action = "RunAnalysis"; break;
113 case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
116 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
117 || !defined(CLANG_ENABLE_OBJC_REWRITER)
118 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
121 llvm_unreachable("Invalid program action!");
125 static std::unique_ptr<FrontendAction>
126 CreateFrontendAction(CompilerInstance &CI) {
127 // Create the underlying action.
128 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
132 const FrontendOptions &FEOpts = CI.getFrontendOpts();
134 if (FEOpts.FixAndRecompile) {
135 Act = llvm::make_unique<FixItRecompile>(std::move(Act));
138 #ifdef CLANG_ENABLE_ARCMT
139 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
140 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
141 // Potentially wrap the base FE action in an ARC Migrate Tool action.
142 switch (FEOpts.ARCMTAction) {
143 case FrontendOptions::ARCMT_None:
145 case FrontendOptions::ARCMT_Check:
146 Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
148 case FrontendOptions::ARCMT_Modify:
149 Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
151 case FrontendOptions::ARCMT_Migrate:
152 Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
154 FEOpts.ARCMTMigrateReportOut,
155 FEOpts.ARCMTMigrateEmitARCErrors);
159 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
160 Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
162 FEOpts.ObjCMTAction);
167 // If there are any AST files to merge, create a frontend action
168 // adaptor to perform the merge.
169 if (!FEOpts.ASTMergeFiles.empty())
170 Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
171 FEOpts.ASTMergeFiles);
176 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
178 if (Clang->getFrontendOpts().ShowHelp) {
179 std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
180 Opts->PrintHelp(llvm::outs(), "clang -cc1",
181 "LLVM 'Clang' Compiler: http://clang.llvm.org",
182 /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0);
188 // FIXME: Use a better -version message?
189 if (Clang->getFrontendOpts().ShowVersion) {
190 llvm::cl::PrintVersionMessage();
194 // Load any requested plugins.
196 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
197 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
199 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
200 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
204 // Check if any of the loaded plugins replaces the main AST action
205 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
206 ie = FrontendPluginRegistry::end();
208 std::unique_ptr<PluginASTAction> P(it->instantiate());
209 if (P->getActionType() == PluginASTAction::ReplaceAction) {
210 Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
211 Clang->getFrontendOpts().ActionName = it->getName();
218 // FIXME: Remove this, one day.
219 // This should happen AFTER plugins have been loaded!
220 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
221 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
222 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
223 Args[0] = "clang (LLVM option parsing)";
224 for (unsigned i = 0; i != NumArgs; ++i)
225 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
226 Args[NumArgs + 1] = nullptr;
227 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
230 #ifdef CLANG_ENABLE_STATIC_ANALYZER
231 // Honor -analyzer-checker-help.
232 // This should happen AFTER plugins have been loaded!
233 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
234 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
237 if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
238 ento::printEnabledCheckerList(llvm::outs(),
239 Clang->getFrontendOpts().Plugins,
240 *Clang->getAnalyzerOpts());
244 // If there were errors in processing arguments, don't do anything else.
245 if (Clang->getDiagnostics().hasErrorOccurred())
247 // Create and execute the frontend action.
248 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
251 bool Success = Clang->ExecuteAction(*Act);
252 if (Clang->getFrontendOpts().DisableFree)
253 BuryPointer(std::move(Act));