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/ARCMigrate/ARCMTActions.h"
16 #include "clang/CodeGen/CodeGenAction.h"
17 #include "clang/Config/config.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/FrontendTool/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/BuryPointer.h"
31 #include "llvm/Support/DynamicLibrary.h"
32 #include "llvm/Support/ErrorHandling.h"
33 using namespace clang;
34 using namespace llvm::opt;
38 static std::unique_ptr<FrontendAction>
39 CreateFrontendBaseAction(CompilerInstance &CI) {
40 using namespace clang::frontend;
41 StringRef Action("unknown");
44 switch (CI.getFrontendOpts().ProgramAction) {
45 case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
46 case ASTDump: return llvm::make_unique<ASTDumpAction>();
47 case ASTPrint: return llvm::make_unique<ASTPrintAction>();
48 case ASTView: return llvm::make_unique<ASTViewAction>();
49 case DumpCompilerOptions:
50 return llvm::make_unique<DumpCompilerOptionsAction>();
51 case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
52 case DumpTokens: return llvm::make_unique<DumpTokensAction>();
53 case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
54 case EmitBC: return llvm::make_unique<EmitBCAction>();
55 case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
56 case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
57 case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
58 case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
59 case EmitObj: return llvm::make_unique<EmitObjAction>();
60 case FixIt: return llvm::make_unique<FixItAction>();
62 return llvm::make_unique<GenerateModuleFromModuleMapAction>();
63 case GenerateModuleInterface:
64 return llvm::make_unique<GenerateModuleInterfaceAction>();
65 case GenerateHeaderModule:
66 return llvm::make_unique<GenerateHeaderModuleAction>();
67 case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
68 case InitOnly: return llvm::make_unique<InitOnlyAction>();
69 case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
70 case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
71 case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
72 case TemplightDump: return llvm::make_unique<TemplightDumpAction>();
75 for (FrontendPluginRegistry::iterator it =
76 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
78 if (it->getName() == CI.getFrontendOpts().ActionName) {
79 std::unique_ptr<PluginASTAction> P(it->instantiate());
80 if ((P->getActionType() != PluginASTAction::ReplaceAction &&
81 P->getActionType() != PluginASTAction::Cmdline) ||
82 !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
88 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
89 << CI.getFrontendOpts().ActionName;
93 case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
94 case PrintPreprocessedInput: {
95 if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
96 CI.getPreprocessorOutputOpts().RewriteImports)
97 return llvm::make_unique<RewriteIncludesAction>();
98 return llvm::make_unique<PrintPreprocessedAction>();
101 case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
102 case RewriteTest: return llvm::make_unique<RewriteTestAction>();
103 #if CLANG_ENABLE_OBJC_REWRITER
104 case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
106 case RewriteObjC: Action = "RewriteObjC"; break;
108 #if CLANG_ENABLE_ARCMT
110 return llvm::make_unique<arcmt::MigrateSourceAction>();
112 case MigrateSource: Action = "MigrateSource"; break;
114 #if CLANG_ENABLE_STATIC_ANALYZER
115 case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
117 case RunAnalysis: Action = "RunAnalysis"; break;
119 case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
122 #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
123 || !CLANG_ENABLE_OBJC_REWRITER
124 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
127 llvm_unreachable("Invalid program action!");
131 std::unique_ptr<FrontendAction>
132 CreateFrontendAction(CompilerInstance &CI) {
133 // Create the underlying action.
134 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
138 const FrontendOptions &FEOpts = CI.getFrontendOpts();
140 if (FEOpts.FixAndRecompile) {
141 Act = llvm::make_unique<FixItRecompile>(std::move(Act));
144 #if CLANG_ENABLE_ARCMT
145 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
146 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
147 // Potentially wrap the base FE action in an ARC Migrate Tool action.
148 switch (FEOpts.ARCMTAction) {
149 case FrontendOptions::ARCMT_None:
151 case FrontendOptions::ARCMT_Check:
152 Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
154 case FrontendOptions::ARCMT_Modify:
155 Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
157 case FrontendOptions::ARCMT_Migrate:
158 Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
160 FEOpts.ARCMTMigrateReportOut,
161 FEOpts.ARCMTMigrateEmitARCErrors);
165 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
166 Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
168 FEOpts.ObjCMTAction);
173 // If there are any AST files to merge, create a frontend action
174 // adaptor to perform the merge.
175 if (!FEOpts.ASTMergeFiles.empty())
176 Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
177 FEOpts.ASTMergeFiles);
182 bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
184 if (Clang->getFrontendOpts().ShowHelp) {
185 std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
186 Opts->PrintHelp(llvm::outs(), "clang -cc1 [options] file...",
187 "LLVM 'Clang' Compiler: http://clang.llvm.org",
188 /*Include=*/driver::options::CC1Option,
189 /*Exclude=*/0, /*ShowAllAliases=*/false);
195 // FIXME: Use a better -version message?
196 if (Clang->getFrontendOpts().ShowVersion) {
197 llvm::cl::PrintVersionMessage();
201 // Load any requested plugins.
203 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
204 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
206 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
207 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
211 // Check if any of the loaded plugins replaces the main AST action
212 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
213 ie = FrontendPluginRegistry::end();
215 std::unique_ptr<PluginASTAction> P(it->instantiate());
216 if (P->getActionType() == PluginASTAction::ReplaceAction) {
217 Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
218 Clang->getFrontendOpts().ActionName = it->getName();
225 // FIXME: Remove this, one day.
226 // This should happen AFTER plugins have been loaded!
227 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
228 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
229 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
230 Args[0] = "clang (LLVM option parsing)";
231 for (unsigned i = 0; i != NumArgs; ++i)
232 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
233 Args[NumArgs + 1] = nullptr;
234 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
237 #if CLANG_ENABLE_STATIC_ANALYZER
238 // Honor -analyzer-checker-help.
239 // This should happen AFTER plugins have been loaded!
240 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
241 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins,
242 Clang->getDiagnostics());
246 // Honor -analyzer-list-enabled-checkers.
247 if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
248 ento::printEnabledCheckerList(llvm::outs(),
249 Clang->getFrontendOpts().Plugins,
250 *Clang->getAnalyzerOpts(),
251 Clang->getDiagnostics());
254 // Honor -analyzer-config-help.
255 if (Clang->getAnalyzerOpts()->ShowConfigOptionsList) {
256 ento::printAnalyzerConfigList(llvm::outs());
261 // If there were errors in processing arguments, don't do anything else.
262 if (Clang->getDiagnostics().hasErrorOccurred())
264 // Create and execute the frontend action.
265 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
268 bool Success = Clang->ExecuteAction(*Act);
269 if (Clang->getFrontendOpts().DisableFree)
270 llvm::BuryPointer(std::move(Act));