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