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 CI.getPreprocessorOutputOpts().RewriteImports)
90 return llvm::make_unique<RewriteIncludesAction>();
91 return llvm::make_unique<PrintPreprocessedAction>();
94 case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
95 case RewriteTest: return llvm::make_unique<RewriteTestAction>();
96 #ifdef CLANG_ENABLE_OBJC_REWRITER
97 case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
99 case RewriteObjC: Action = "RewriteObjC"; break;
101 #ifdef CLANG_ENABLE_ARCMT
103 return llvm::make_unique<arcmt::MigrateSourceAction>();
105 case MigrateSource: Action = "MigrateSource"; break;
107 #ifdef CLANG_ENABLE_STATIC_ANALYZER
108 case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
110 case RunAnalysis: Action = "RunAnalysis"; break;
112 case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
115 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
116 || !defined(CLANG_ENABLE_OBJC_REWRITER)
117 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
120 llvm_unreachable("Invalid program action!");
124 static std::unique_ptr<FrontendAction>
125 CreateFrontendAction(CompilerInstance &CI) {
126 // Create the underlying action.
127 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
131 const FrontendOptions &FEOpts = CI.getFrontendOpts();
133 if (FEOpts.FixAndRecompile) {
134 Act = llvm::make_unique<FixItRecompile>(std::move(Act));
137 #ifdef CLANG_ENABLE_ARCMT
138 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
139 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
140 // Potentially wrap the base FE action in an ARC Migrate Tool action.
141 switch (FEOpts.ARCMTAction) {
142 case FrontendOptions::ARCMT_None:
144 case FrontendOptions::ARCMT_Check:
145 Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
147 case FrontendOptions::ARCMT_Modify:
148 Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
150 case FrontendOptions::ARCMT_Migrate:
151 Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
153 FEOpts.ARCMTMigrateReportOut,
154 FEOpts.ARCMTMigrateEmitARCErrors);
158 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
159 Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
161 FEOpts.ObjCMTAction);
166 // If there are any AST files to merge, create a frontend action
167 // adaptor to perform the merge.
168 if (!FEOpts.ASTMergeFiles.empty())
169 Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
170 FEOpts.ASTMergeFiles);
175 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
177 if (Clang->getFrontendOpts().ShowHelp) {
178 std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
179 Opts->PrintHelp(llvm::outs(), "clang -cc1",
180 "LLVM 'Clang' Compiler: http://clang.llvm.org",
181 /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0);
187 // FIXME: Use a better -version message?
188 if (Clang->getFrontendOpts().ShowVersion) {
189 llvm::cl::PrintVersionMessage();
193 // Load any requested plugins.
195 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
196 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
198 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
199 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
203 // Check if any of the loaded plugins replaces the main AST action
204 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
205 ie = FrontendPluginRegistry::end();
207 std::unique_ptr<PluginASTAction> P(it->instantiate());
208 if (P->getActionType() == PluginASTAction::ReplaceAction) {
209 Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
210 Clang->getFrontendOpts().ActionName = it->getName();
217 // FIXME: Remove this, one day.
218 // This should happen AFTER plugins have been loaded!
219 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
220 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
221 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
222 Args[0] = "clang (LLVM option parsing)";
223 for (unsigned i = 0; i != NumArgs; ++i)
224 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
225 Args[NumArgs + 1] = nullptr;
226 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
229 #ifdef CLANG_ENABLE_STATIC_ANALYZER
230 // Honor -analyzer-checker-help.
231 // This should happen AFTER plugins have been loaded!
232 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
233 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
236 if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
237 ento::printEnabledCheckerList(llvm::outs(),
238 Clang->getFrontendOpts().Plugins,
239 *Clang->getAnalyzerOpts());
243 // If there were errors in processing arguments, don't do anything else.
244 if (Clang->getDiagnostics().hasErrorOccurred())
246 // Create and execute the frontend action.
247 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
250 bool Success = Clang->ExecuteAction(*Act);
251 if (Clang->getFrontendOpts().DisableFree)
252 BuryPointer(std::move(Act));