185dd0bd1SPeter Collingbourne //===--- ExecuteCompilerInvocation.cpp ------------------------------------===// 285dd0bd1SPeter Collingbourne // 385dd0bd1SPeter Collingbourne // The LLVM Compiler Infrastructure 485dd0bd1SPeter Collingbourne // 585dd0bd1SPeter Collingbourne // This file is distributed under the University of Illinois Open Source 685dd0bd1SPeter Collingbourne // License. See LICENSE.TXT for details. 785dd0bd1SPeter Collingbourne // 885dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===// 985dd0bd1SPeter Collingbourne // 1085dd0bd1SPeter Collingbourne // This file holds ExecuteCompilerInvocation(). It is split into its own file to 1185dd0bd1SPeter Collingbourne // minimize the impact of pulling in essentially everything else in Clang. 1285dd0bd1SPeter Collingbourne // 1385dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===// 1485dd0bd1SPeter Collingbourne 1585dd0bd1SPeter Collingbourne #include "clang/FrontendTool/Utils.h" 16b5703510SChandler Carruth #include "clang/ARCMigrate/ARCMTActions.h" 1785dd0bd1SPeter Collingbourne #include "clang/CodeGen/CodeGenAction.h" 18a3c85b86SJames Molloy #include "clang/Driver/Options.h" 1985dd0bd1SPeter Collingbourne #include "clang/Frontend/CompilerInstance.h" 203a02247dSChandler Carruth #include "clang/Frontend/CompilerInvocation.h" 2185dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendActions.h" 2285dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendDiagnostic.h" 2385dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendPluginRegistry.h" 24ce2c726eSKostya Serebryany #include "clang/Frontend/Utils.h" 25cdf81490STed Kremenek #include "clang/Rewrite/Frontend/FrontendActions.h" 263a02247dSChandler Carruth #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" 27898229abSReid Kleckner #include "llvm/Option/OptTable.h" 28898229abSReid Kleckner #include "llvm/Option/Option.h" 298aaf4995SMichael J. Spencer #include "llvm/Support/DynamicLibrary.h" 303a02247dSChandler Carruth #include "llvm/Support/ErrorHandling.h" 3185dd0bd1SPeter Collingbourne using namespace clang; 32898229abSReid Kleckner using namespace llvm::opt; 3385dd0bd1SPeter Collingbourne 34d35e98faSArgyrios Kyrtzidis static std::unique_ptr<FrontendAction> 35d35e98faSArgyrios Kyrtzidis CreateFrontendBaseAction(CompilerInstance &CI) { 3685dd0bd1SPeter Collingbourne using namespace clang::frontend; 37ba294d73SAlp Toker StringRef Action("unknown"); 38ba294d73SAlp Toker (void)Action; 3985dd0bd1SPeter Collingbourne 4085dd0bd1SPeter Collingbourne switch (CI.getFrontendOpts().ProgramAction) { 41d35e98faSArgyrios Kyrtzidis case ASTDeclList: return llvm::make_unique<ASTDeclListAction>(); 42d35e98faSArgyrios Kyrtzidis case ASTDump: return llvm::make_unique<ASTDumpAction>(); 43d35e98faSArgyrios Kyrtzidis case ASTPrint: return llvm::make_unique<ASTPrintAction>(); 44d35e98faSArgyrios Kyrtzidis case ASTView: return llvm::make_unique<ASTViewAction>(); 45d35e98faSArgyrios Kyrtzidis case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>(); 46d35e98faSArgyrios Kyrtzidis case DumpTokens: return llvm::make_unique<DumpTokensAction>(); 47d35e98faSArgyrios Kyrtzidis case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>(); 48d35e98faSArgyrios Kyrtzidis case EmitBC: return llvm::make_unique<EmitBCAction>(); 49d35e98faSArgyrios Kyrtzidis case EmitHTML: return llvm::make_unique<HTMLPrintAction>(); 50d35e98faSArgyrios Kyrtzidis case EmitLLVM: return llvm::make_unique<EmitLLVMAction>(); 51d35e98faSArgyrios Kyrtzidis case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>(); 52d35e98faSArgyrios Kyrtzidis case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>(); 53d35e98faSArgyrios Kyrtzidis case EmitObj: return llvm::make_unique<EmitObjAction>(); 54d35e98faSArgyrios Kyrtzidis case FixIt: return llvm::make_unique<FixItAction>(); 55bbcc9f04SRichard Smith case GenerateModule: 56bbcc9f04SRichard Smith return llvm::make_unique<GenerateModuleFromModuleMapAction>(); 57bbcc9f04SRichard Smith case GenerateModuleInterface: 58bbcc9f04SRichard Smith return llvm::make_unique<GenerateModuleInterfaceAction>(); 59d35e98faSArgyrios Kyrtzidis case GeneratePCH: return llvm::make_unique<GeneratePCHAction>(); 60d35e98faSArgyrios Kyrtzidis case GeneratePTH: return llvm::make_unique<GeneratePTHAction>(); 61d35e98faSArgyrios Kyrtzidis case InitOnly: return llvm::make_unique<InitOnlyAction>(); 62d35e98faSArgyrios Kyrtzidis case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>(); 63d35e98faSArgyrios Kyrtzidis case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>(); 64d35e98faSArgyrios Kyrtzidis case VerifyPCH: return llvm::make_unique<VerifyPCHAction>(); 6585dd0bd1SPeter Collingbourne 6685dd0bd1SPeter Collingbourne case PluginAction: { 6785dd0bd1SPeter Collingbourne for (FrontendPluginRegistry::iterator it = 6885dd0bd1SPeter Collingbourne FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); 6985dd0bd1SPeter Collingbourne it != ie; ++it) { 7085dd0bd1SPeter Collingbourne if (it->getName() == CI.getFrontendOpts().ActionName) { 71b8984329SAhmed Charles std::unique_ptr<PluginASTAction> P(it->instantiate()); 726c78974bSJohn Brawn if ((P->getActionType() != PluginASTAction::ReplaceAction && 736c78974bSJohn Brawn P->getActionType() != PluginASTAction::Cmdline) || 746c78974bSJohn Brawn !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) 75236bde3dSCraig Topper return nullptr; 76d35e98faSArgyrios Kyrtzidis return std::move(P); 7785dd0bd1SPeter Collingbourne } 7885dd0bd1SPeter Collingbourne } 7985dd0bd1SPeter Collingbourne 8085dd0bd1SPeter Collingbourne CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) 8185dd0bd1SPeter Collingbourne << CI.getFrontendOpts().ActionName; 82236bde3dSCraig Topper return nullptr; 8385dd0bd1SPeter Collingbourne } 8485dd0bd1SPeter Collingbourne 85d35e98faSArgyrios Kyrtzidis case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>(); 86d35e98faSArgyrios Kyrtzidis case PrintPreamble: return llvm::make_unique<PrintPreambleAction>(); 87e993e4cdSDavid Blaikie case PrintPreprocessedInput: { 880621cb2eSAlp Toker if (CI.getPreprocessorOutputOpts().RewriteIncludes) 89d35e98faSArgyrios Kyrtzidis return llvm::make_unique<RewriteIncludesAction>(); 90d35e98faSArgyrios Kyrtzidis return llvm::make_unique<PrintPreprocessedAction>(); 91e993e4cdSDavid Blaikie } 92e993e4cdSDavid Blaikie 93d35e98faSArgyrios Kyrtzidis case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>(); 94d35e98faSArgyrios Kyrtzidis case RewriteTest: return llvm::make_unique<RewriteTestAction>(); 950621cb2eSAlp Toker #ifdef CLANG_ENABLE_OBJC_REWRITER 96d35e98faSArgyrios Kyrtzidis case RewriteObjC: return llvm::make_unique<RewriteObjCAction>(); 97d93c8c00SRoman Divacky #else 98d93c8c00SRoman Divacky case RewriteObjC: Action = "RewriteObjC"; break; 99d93c8c00SRoman Divacky #endif 100d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_ARCMT 101d35e98faSArgyrios Kyrtzidis case MigrateSource: 102d35e98faSArgyrios Kyrtzidis return llvm::make_unique<arcmt::MigrateSourceAction>(); 103d93c8c00SRoman Divacky #else 104d93c8c00SRoman Divacky case MigrateSource: Action = "MigrateSource"; break; 105d93c8c00SRoman Divacky #endif 106d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_STATIC_ANALYZER 107d35e98faSArgyrios Kyrtzidis case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>(); 108d93c8c00SRoman Divacky #else 109d93c8c00SRoman Divacky case RunAnalysis: Action = "RunAnalysis"; break; 110d93c8c00SRoman Divacky #endif 111d35e98faSArgyrios Kyrtzidis case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>(); 11285dd0bd1SPeter Collingbourne } 113d93c8c00SRoman Divacky 114d93c8c00SRoman Divacky #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \ 1150621cb2eSAlp Toker || !defined(CLANG_ENABLE_OBJC_REWRITER) 116d93c8c00SRoman Divacky CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action; 117d93c8c00SRoman Divacky return 0; 118d93c8c00SRoman Divacky #else 119f47fa304SDavid Blaikie llvm_unreachable("Invalid program action!"); 120d93c8c00SRoman Divacky #endif 12185dd0bd1SPeter Collingbourne } 12285dd0bd1SPeter Collingbourne 123d35e98faSArgyrios Kyrtzidis static std::unique_ptr<FrontendAction> 124d35e98faSArgyrios Kyrtzidis CreateFrontendAction(CompilerInstance &CI) { 12585dd0bd1SPeter Collingbourne // Create the underlying action. 126d35e98faSArgyrios Kyrtzidis std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI); 12785dd0bd1SPeter Collingbourne if (!Act) 128236bde3dSCraig Topper return nullptr; 12985dd0bd1SPeter Collingbourne 1303d97a9beSArgyrios Kyrtzidis const FrontendOptions &FEOpts = CI.getFrontendOpts(); 1313d97a9beSArgyrios Kyrtzidis 1323d97a9beSArgyrios Kyrtzidis if (FEOpts.FixAndRecompile) { 133d35e98faSArgyrios Kyrtzidis Act = llvm::make_unique<FixItRecompile>(std::move(Act)); 13424e9afffSArgyrios Kyrtzidis } 13524e9afffSArgyrios Kyrtzidis 136d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_ARCMT 137d4d55340SArgyrios Kyrtzidis if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource && 138d4d55340SArgyrios Kyrtzidis CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) { 139b5703510SChandler Carruth // Potentially wrap the base FE action in an ARC Migrate Tool action. 1403d97a9beSArgyrios Kyrtzidis switch (FEOpts.ARCMTAction) { 141b5703510SChandler Carruth case FrontendOptions::ARCMT_None: 142b5703510SChandler Carruth break; 143b5703510SChandler Carruth case FrontendOptions::ARCMT_Check: 144d35e98faSArgyrios Kyrtzidis Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act)); 145b5703510SChandler Carruth break; 146b5703510SChandler Carruth case FrontendOptions::ARCMT_Modify: 147d35e98faSArgyrios Kyrtzidis Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act)); 1487fbd97f6SArgyrios Kyrtzidis break; 1497fbd97f6SArgyrios Kyrtzidis case FrontendOptions::ARCMT_Migrate: 150d35e98faSArgyrios Kyrtzidis Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act), 151f7639e1bSTed Kremenek FEOpts.MTMigrateDir, 1523d97a9beSArgyrios Kyrtzidis FEOpts.ARCMTMigrateReportOut, 1533d97a9beSArgyrios Kyrtzidis FEOpts.ARCMTMigrateEmitARCErrors); 154b5703510SChandler Carruth break; 155b5703510SChandler Carruth } 156b5703510SChandler Carruth 157f7639e1bSTed Kremenek if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) { 158d35e98faSArgyrios Kyrtzidis Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act), 159d35e98faSArgyrios Kyrtzidis FEOpts.MTMigrateDir, 160182486c9SFariborz Jahanian FEOpts.ObjCMTAction); 161f7639e1bSTed Kremenek } 1627a2645f9SArgyrios Kyrtzidis } 163d93c8c00SRoman Divacky #endif 164f7639e1bSTed Kremenek 16585dd0bd1SPeter Collingbourne // If there are any AST files to merge, create a frontend action 16685dd0bd1SPeter Collingbourne // adaptor to perform the merge. 1673d97a9beSArgyrios Kyrtzidis if (!FEOpts.ASTMergeFiles.empty()) 168d35e98faSArgyrios Kyrtzidis Act = llvm::make_unique<ASTMergeAction>(std::move(Act), 169d35e98faSArgyrios Kyrtzidis FEOpts.ASTMergeFiles); 17085dd0bd1SPeter Collingbourne 17185dd0bd1SPeter Collingbourne return Act; 17285dd0bd1SPeter Collingbourne } 17385dd0bd1SPeter Collingbourne 17485dd0bd1SPeter Collingbourne bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) { 17585dd0bd1SPeter Collingbourne // Honor -help. 17685dd0bd1SPeter Collingbourne if (Clang->getFrontendOpts().ShowHelp) { 177*0aaa7625SDavid Blaikie std::unique_ptr<OptTable> Opts = driver::createDriverOptTable(); 17885dd0bd1SPeter Collingbourne Opts->PrintHelp(llvm::outs(), "clang -cc1", 179e70ed869SRichard Smith "LLVM 'Clang' Compiler: http://clang.llvm.org", 180898229abSReid Kleckner /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0); 181958297daSRafael Espindola return true; 18285dd0bd1SPeter Collingbourne } 18385dd0bd1SPeter Collingbourne 18485dd0bd1SPeter Collingbourne // Honor -version. 18585dd0bd1SPeter Collingbourne // 18685dd0bd1SPeter Collingbourne // FIXME: Use a better -version message? 18785dd0bd1SPeter Collingbourne if (Clang->getFrontendOpts().ShowVersion) { 18885dd0bd1SPeter Collingbourne llvm::cl::PrintVersionMessage(); 189958297daSRafael Espindola return true; 19085dd0bd1SPeter Collingbourne } 19185dd0bd1SPeter Collingbourne 19285dd0bd1SPeter Collingbourne // Load any requested plugins. 19385dd0bd1SPeter Collingbourne for (unsigned i = 0, 19485dd0bd1SPeter Collingbourne e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) { 19585dd0bd1SPeter Collingbourne const std::string &Path = Clang->getFrontendOpts().Plugins[i]; 19685dd0bd1SPeter Collingbourne std::string Error; 197d8cd0604SNAKAMURA Takumi if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) 19885dd0bd1SPeter Collingbourne Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin) 19985dd0bd1SPeter Collingbourne << Path << Error; 20085dd0bd1SPeter Collingbourne } 20185dd0bd1SPeter Collingbourne 2026c78974bSJohn Brawn // Check if any of the loaded plugins replaces the main AST action 2036c78974bSJohn Brawn for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(), 2046c78974bSJohn Brawn ie = FrontendPluginRegistry::end(); 2056c78974bSJohn Brawn it != ie; ++it) { 2066c78974bSJohn Brawn std::unique_ptr<PluginASTAction> P(it->instantiate()); 2076c78974bSJohn Brawn if (P->getActionType() == PluginASTAction::ReplaceAction) { 2086c78974bSJohn Brawn Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction; 2096c78974bSJohn Brawn Clang->getFrontendOpts().ActionName = it->getName(); 2106c78974bSJohn Brawn break; 2116c78974bSJohn Brawn } 2126c78974bSJohn Brawn } 2136c78974bSJohn Brawn 21411ce9224STobias Grosser // Honor -mllvm. 21511ce9224STobias Grosser // 21611ce9224STobias Grosser // FIXME: Remove this, one day. 21711ce9224STobias Grosser // This should happen AFTER plugins have been loaded! 21811ce9224STobias Grosser if (!Clang->getFrontendOpts().LLVMArgs.empty()) { 21911ce9224STobias Grosser unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); 22081f9e02eSChandler Carruth auto Args = llvm::make_unique<const char*[]>(NumArgs + 2); 22111ce9224STobias Grosser Args[0] = "clang (LLVM option parsing)"; 22211ce9224STobias Grosser for (unsigned i = 0; i != NumArgs; ++i) 22311ce9224STobias Grosser Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); 224236bde3dSCraig Topper Args[NumArgs + 1] = nullptr; 22581f9e02eSChandler Carruth llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get()); 22611ce9224STobias Grosser } 22711ce9224STobias Grosser 228d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_STATIC_ANALYZER 22959cce71aSJordy Rose // Honor -analyzer-checker-help. 23059cce71aSJordy Rose // This should happen AFTER plugins have been loaded! 23140ea0eaaSTed Kremenek if (Clang->getAnalyzerOpts()->ShowCheckerHelp) { 23259cce71aSJordy Rose ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins); 233958297daSRafael Espindola return true; 23459cce71aSJordy Rose } 235c430990dSGabor Horvath if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) { 236c430990dSGabor Horvath ento::printEnabledCheckerList(llvm::outs(), 237c430990dSGabor Horvath Clang->getFrontendOpts().Plugins, 238c430990dSGabor Horvath *Clang->getAnalyzerOpts()); 239c430990dSGabor Horvath } 240d93c8c00SRoman Divacky #endif 24159cce71aSJordy Rose 24285dd0bd1SPeter Collingbourne // If there were errors in processing arguments, don't do anything else. 243aa73d020SSean Silva if (Clang->getDiagnostics().hasErrorOccurred()) 244aa73d020SSean Silva return false; 24585dd0bd1SPeter Collingbourne // Create and execute the frontend action. 246b8984329SAhmed Charles std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang)); 247aa73d020SSean Silva if (!Act) 248aa73d020SSean Silva return false; 249aa73d020SSean Silva bool Success = Clang->ExecuteAction(*Act); 2504f76f4aeSTed Kremenek if (Clang->getFrontendOpts().DisableFree) 251a97eaa1bSDavid Blaikie BuryPointer(std::move(Act)); 25285dd0bd1SPeter Collingbourne return Success; 25385dd0bd1SPeter Collingbourne } 254