1 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 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. 12 // 13 //===----------------------------------------------------------------------===// 14 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; 33 34 static std::unique_ptr<FrontendAction> 35 CreateFrontendBaseAction(CompilerInstance &CI) { 36 using namespace clang::frontend; 37 StringRef Action("unknown"); 38 (void)Action; 39 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>(); 55 case GenerateModule: return llvm::make_unique<GenerateModuleAction>(); 56 case GeneratePCH: return llvm::make_unique<GeneratePCHAction>(); 57 case GeneratePTH: return llvm::make_unique<GeneratePTHAction>(); 58 case InitOnly: return llvm::make_unique<InitOnlyAction>(); 59 case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>(); 60 case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>(); 61 case VerifyPCH: return llvm::make_unique<VerifyPCHAction>(); 62 63 case PluginAction: { 64 for (FrontendPluginRegistry::iterator it = 65 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); 66 it != ie; ++it) { 67 if (it->getName() == CI.getFrontendOpts().ActionName) { 68 std::unique_ptr<PluginASTAction> P(it->instantiate()); 69 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) 70 return nullptr; 71 return std::move(P); 72 } 73 } 74 75 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) 76 << CI.getFrontendOpts().ActionName; 77 return nullptr; 78 } 79 80 case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>(); 81 case PrintPreamble: return llvm::make_unique<PrintPreambleAction>(); 82 case PrintPreprocessedInput: { 83 if (CI.getPreprocessorOutputOpts().RewriteIncludes) 84 return llvm::make_unique<RewriteIncludesAction>(); 85 return llvm::make_unique<PrintPreprocessedAction>(); 86 } 87 88 case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>(); 89 case RewriteTest: return llvm::make_unique<RewriteTestAction>(); 90 #ifdef CLANG_ENABLE_OBJC_REWRITER 91 case RewriteObjC: return llvm::make_unique<RewriteObjCAction>(); 92 #else 93 case RewriteObjC: Action = "RewriteObjC"; break; 94 #endif 95 #ifdef CLANG_ENABLE_ARCMT 96 case MigrateSource: 97 return llvm::make_unique<arcmt::MigrateSourceAction>(); 98 #else 99 case MigrateSource: Action = "MigrateSource"; break; 100 #endif 101 #ifdef CLANG_ENABLE_STATIC_ANALYZER 102 case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>(); 103 #else 104 case RunAnalysis: Action = "RunAnalysis"; break; 105 #endif 106 case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>(); 107 } 108 109 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \ 110 || !defined(CLANG_ENABLE_OBJC_REWRITER) 111 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action; 112 return 0; 113 #else 114 llvm_unreachable("Invalid program action!"); 115 #endif 116 } 117 118 static std::unique_ptr<FrontendAction> 119 CreateFrontendAction(CompilerInstance &CI) { 120 // Create the underlying action. 121 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI); 122 if (!Act) 123 return nullptr; 124 125 const FrontendOptions &FEOpts = CI.getFrontendOpts(); 126 127 if (FEOpts.FixAndRecompile) { 128 Act = llvm::make_unique<FixItRecompile>(std::move(Act)); 129 } 130 131 #ifdef CLANG_ENABLE_ARCMT 132 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource && 133 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) { 134 // Potentially wrap the base FE action in an ARC Migrate Tool action. 135 switch (FEOpts.ARCMTAction) { 136 case FrontendOptions::ARCMT_None: 137 break; 138 case FrontendOptions::ARCMT_Check: 139 Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act)); 140 break; 141 case FrontendOptions::ARCMT_Modify: 142 Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act)); 143 break; 144 case FrontendOptions::ARCMT_Migrate: 145 Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act), 146 FEOpts.MTMigrateDir, 147 FEOpts.ARCMTMigrateReportOut, 148 FEOpts.ARCMTMigrateEmitARCErrors); 149 break; 150 } 151 152 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) { 153 Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act), 154 FEOpts.MTMigrateDir, 155 FEOpts.ObjCMTAction); 156 } 157 } 158 #endif 159 160 // If there are any AST files to merge, create a frontend action 161 // adaptor to perform the merge. 162 if (!FEOpts.ASTMergeFiles.empty()) 163 Act = llvm::make_unique<ASTMergeAction>(std::move(Act), 164 FEOpts.ASTMergeFiles); 165 166 return Act; 167 } 168 169 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) { 170 // Honor -help. 171 if (Clang->getFrontendOpts().ShowHelp) { 172 std::unique_ptr<OptTable> Opts(driver::createDriverOptTable()); 173 Opts->PrintHelp(llvm::outs(), "clang -cc1", 174 "LLVM 'Clang' Compiler: http://clang.llvm.org", 175 /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0); 176 return true; 177 } 178 179 // Honor -version. 180 // 181 // FIXME: Use a better -version message? 182 if (Clang->getFrontendOpts().ShowVersion) { 183 llvm::cl::PrintVersionMessage(); 184 return true; 185 } 186 187 // Load any requested plugins. 188 for (unsigned i = 0, 189 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) { 190 const std::string &Path = Clang->getFrontendOpts().Plugins[i]; 191 std::string Error; 192 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) 193 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin) 194 << Path << Error; 195 } 196 197 // Honor -mllvm. 198 // 199 // FIXME: Remove this, one day. 200 // This should happen AFTER plugins have been loaded! 201 if (!Clang->getFrontendOpts().LLVMArgs.empty()) { 202 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); 203 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2); 204 Args[0] = "clang (LLVM option parsing)"; 205 for (unsigned i = 0; i != NumArgs; ++i) 206 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); 207 Args[NumArgs + 1] = nullptr; 208 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get()); 209 } 210 211 #ifdef CLANG_ENABLE_STATIC_ANALYZER 212 // Honor -analyzer-checker-help. 213 // This should happen AFTER plugins have been loaded! 214 if (Clang->getAnalyzerOpts()->ShowCheckerHelp) { 215 ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins); 216 return true; 217 } 218 #endif 219 220 // If there were errors in processing arguments, don't do anything else. 221 if (Clang->getDiagnostics().hasErrorOccurred()) 222 return false; 223 // Create and execute the frontend action. 224 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang)); 225 if (!Act) 226 return false; 227 bool Success = Clang->ExecuteAction(*Act); 228 if (Clang->getFrontendOpts().DisableFree) 229 BuryPointer(std::move(Act)); 230 return Success; 231 } 232