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;
37 static std::unique_ptr<FrontendAction>
38 CreateFrontendBaseAction(CompilerInstance &CI) {
39 using namespace clang::frontend;
40 StringRef Action("unknown");
43 switch (CI.getFrontendOpts().ProgramAction) {
44 case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
45 case ASTDump: return llvm::make_unique<ASTDumpAction>();
46 case ASTPrint: return llvm::make_unique<ASTPrintAction>();
47 case ASTView: return llvm::make_unique<ASTViewAction>();
48 case DumpCompilerOptions:
49 return llvm::make_unique<DumpCompilerOptionsAction>();
50 case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
51 case DumpTokens: return llvm::make_unique<DumpTokensAction>();
52 case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
53 case EmitBC: return llvm::make_unique<EmitBCAction>();
54 case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
55 case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
56 case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
57 case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
58 case EmitObj: return llvm::make_unique<EmitObjAction>();
59 case FixIt: return llvm::make_unique<FixItAction>();
61 return llvm::make_unique<GenerateModuleFromModuleMapAction>();
62 case GenerateModuleInterface:
63 return llvm::make_unique<GenerateModuleInterfaceAction>();
64 case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
65 case GeneratePTH: return llvm::make_unique<GeneratePTHAction>();
66 case InitOnly: return llvm::make_unique<InitOnlyAction>();
67 case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
68 case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
69 case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
70 case TemplightDump: return llvm::make_unique<TemplightDumpAction>();
73 for (FrontendPluginRegistry::iterator it =
74 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
76 if (it->getName() == CI.getFrontendOpts().ActionName) {
77 std::unique_ptr<PluginASTAction> P(it->instantiate());
78 if ((P->getActionType() != PluginASTAction::ReplaceAction &&
79 P->getActionType() != PluginASTAction::Cmdline) ||
80 !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
86 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
87 << CI.getFrontendOpts().ActionName;
91 case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>();
92 case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
93 case PrintPreprocessedInput: {
94 if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
95 CI.getPreprocessorOutputOpts().RewriteImports)
96 return llvm::make_unique<RewriteIncludesAction>();
97 return llvm::make_unique<PrintPreprocessedAction>();
100 case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
101 case RewriteTest: return llvm::make_unique<RewriteTestAction>();
102 #if CLANG_ENABLE_OBJC_REWRITER
103 case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
105 case RewriteObjC: Action = "RewriteObjC"; break;
107 #if CLANG_ENABLE_ARCMT
109 return llvm::make_unique<arcmt::MigrateSourceAction>();
111 case MigrateSource: Action = "MigrateSource"; break;
113 #if CLANG_ENABLE_STATIC_ANALYZER
114 case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
116 case RunAnalysis: Action = "RunAnalysis"; break;
118 case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
121 #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
122 || !CLANG_ENABLE_OBJC_REWRITER
123 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
126 llvm_unreachable("Invalid program action!");
130 std::unique_ptr<FrontendAction>
131 CreateFrontendAction(CompilerInstance &CI) {
132 // Create the underlying action.
133 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
137 const FrontendOptions &FEOpts = CI.getFrontendOpts();
139 if (FEOpts.FixAndRecompile) {
140 Act = llvm::make_unique<FixItRecompile>(std::move(Act));
143 #if CLANG_ENABLE_ARCMT
144 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
145 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
146 // Potentially wrap the base FE action in an ARC Migrate Tool action.
147 switch (FEOpts.ARCMTAction) {
148 case FrontendOptions::ARCMT_None:
150 case FrontendOptions::ARCMT_Check:
151 Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
153 case FrontendOptions::ARCMT_Modify:
154 Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
156 case FrontendOptions::ARCMT_Migrate:
157 Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
159 FEOpts.ARCMTMigrateReportOut,
160 FEOpts.ARCMTMigrateEmitARCErrors);
164 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
165 Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
167 FEOpts.ObjCMTAction);
172 // If there are any AST files to merge, create a frontend action
173 // adaptor to perform the merge.
174 if (!FEOpts.ASTMergeFiles.empty())
175 Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
176 FEOpts.ASTMergeFiles);
181 bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
183 if (Clang->getFrontendOpts().ShowHelp) {
184 std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
185 Opts->PrintHelp(llvm::outs(), "clang -cc1",
186 "LLVM 'Clang' Compiler: http://clang.llvm.org",
187 /*Include=*/driver::options::CC1Option,
188 /*Exclude=*/0, /*ShowAllAliases=*/false);
194 // FIXME: Use a better -version message?
195 if (Clang->getFrontendOpts().ShowVersion) {
196 llvm::cl::PrintVersionMessage();
200 // Load any requested plugins.
202 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
203 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
205 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
206 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
210 // Check if any of the loaded plugins replaces the main AST action
211 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
212 ie = FrontendPluginRegistry::end();
214 std::unique_ptr<PluginASTAction> P(it->instantiate());
215 if (P->getActionType() == PluginASTAction::ReplaceAction) {
216 Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
217 Clang->getFrontendOpts().ActionName = it->getName();
224 // FIXME: Remove this, one day.
225 // This should happen AFTER plugins have been loaded!
226 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
227 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
228 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
229 Args[0] = "clang (LLVM option parsing)";
230 for (unsigned i = 0; i != NumArgs; ++i)
231 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
232 Args[NumArgs + 1] = nullptr;
233 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
236 #if CLANG_ENABLE_STATIC_ANALYZER
237 // Honor -analyzer-checker-help.
238 // This should happen AFTER plugins have been loaded!
239 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
240 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
243 if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
244 ento::printEnabledCheckerList(llvm::outs(),
245 Clang->getFrontendOpts().Plugins,
246 *Clang->getAnalyzerOpts());
250 // If there were errors in processing arguments, don't do anything else.
251 if (Clang->getDiagnostics().hasErrorOccurred())
253 // Create and execute the frontend action.
254 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
257 bool Success = Clang->ExecuteAction(*Act);
258 if (Clang->getFrontendOpts().DisableFree)
259 BuryPointer(std::move(Act));