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