1 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file holds ExecuteCompilerInvocation(). It is split into its own file to 10 // minimize the impact of pulling in essentially everything else in Clang. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/ARCMigrate/ARCMTActions.h" 15 #include "clang/CodeGen/CodeGenAction.h" 16 #include "clang/Config/config.h" 17 #include "clang/Driver/Options.h" 18 #include "clang/Frontend/CompilerInstance.h" 19 #include "clang/Frontend/CompilerInvocation.h" 20 #include "clang/Frontend/FrontendActions.h" 21 #include "clang/Frontend/FrontendDiagnostic.h" 22 #include "clang/Frontend/FrontendPluginRegistry.h" 23 #include "clang/Frontend/Utils.h" 24 #include "clang/FrontendTool/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/BuryPointer.h" 30 #include "llvm/Support/DynamicLibrary.h" 31 #include "llvm/Support/ErrorHandling.h" 32 using namespace clang; 33 using namespace llvm::opt; 34 35 namespace clang { 36 37 static std::unique_ptr<FrontendAction> 38 CreateFrontendBaseAction(CompilerInstance &CI) { 39 using namespace clang::frontend; 40 StringRef Action("unknown"); 41 (void)Action; 42 43 switch (CI.getFrontendOpts().ProgramAction) { 44 case ASTDeclList: return llvm::make_unique<ASTDeclListAction>(); 45 case ASTDump: return llvm::make_unique<ASTDumpAction>(); 46 case ASTPrint: return llvm::make_unique<ASTPrintAction>(); 47 case ASTView: return llvm::make_unique<ASTViewAction>(); 48 case DumpCompilerOptions: 49 return llvm::make_unique<DumpCompilerOptionsAction>(); 50 case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>(); 51 case DumpTokens: return llvm::make_unique<DumpTokensAction>(); 52 case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>(); 53 case EmitBC: return llvm::make_unique<EmitBCAction>(); 54 case EmitHTML: return llvm::make_unique<HTMLPrintAction>(); 55 case EmitLLVM: return llvm::make_unique<EmitLLVMAction>(); 56 case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>(); 57 case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>(); 58 case EmitObj: return llvm::make_unique<EmitObjAction>(); 59 case FixIt: return llvm::make_unique<FixItAction>(); 60 case GenerateModule: 61 return llvm::make_unique<GenerateModuleFromModuleMapAction>(); 62 case GenerateModuleInterface: 63 return llvm::make_unique<GenerateModuleInterfaceAction>(); 64 case GenerateHeaderModule: 65 return llvm::make_unique<GenerateHeaderModuleAction>(); 66 case GeneratePCH: return llvm::make_unique<GeneratePCHAction>(); 67 case GenerateInterfaceYAMLExpV1: 68 return llvm::make_unique<GenerateInterfaceYAMLExpV1Action>(); 69 case GenerateInterfaceTBEExpV1: 70 return llvm::make_unique<GenerateInterfaceTBEExpV1Action>(); 71 case InitOnly: return llvm::make_unique<InitOnlyAction>(); 72 case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>(); 73 case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>(); 74 case VerifyPCH: return llvm::make_unique<VerifyPCHAction>(); 75 case TemplightDump: return llvm::make_unique<TemplightDumpAction>(); 76 77 case PluginAction: { 78 for (FrontendPluginRegistry::iterator it = 79 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); 80 it != ie; ++it) { 81 if (it->getName() == CI.getFrontendOpts().ActionName) { 82 std::unique_ptr<PluginASTAction> P(it->instantiate()); 83 if ((P->getActionType() != PluginASTAction::ReplaceAction && 84 P->getActionType() != PluginASTAction::Cmdline) || 85 !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) 86 return nullptr; 87 return std::move(P); 88 } 89 } 90 91 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) 92 << CI.getFrontendOpts().ActionName; 93 return nullptr; 94 } 95 96 case PrintPreamble: return llvm::make_unique<PrintPreambleAction>(); 97 case PrintPreprocessedInput: { 98 if (CI.getPreprocessorOutputOpts().RewriteIncludes || 99 CI.getPreprocessorOutputOpts().RewriteImports) 100 return llvm::make_unique<RewriteIncludesAction>(); 101 return llvm::make_unique<PrintPreprocessedAction>(); 102 } 103 104 case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>(); 105 case RewriteTest: return llvm::make_unique<RewriteTestAction>(); 106 #if CLANG_ENABLE_OBJC_REWRITER 107 case RewriteObjC: return llvm::make_unique<RewriteObjCAction>(); 108 #else 109 case RewriteObjC: Action = "RewriteObjC"; break; 110 #endif 111 #if CLANG_ENABLE_ARCMT 112 case MigrateSource: 113 return llvm::make_unique<arcmt::MigrateSourceAction>(); 114 #else 115 case MigrateSource: Action = "MigrateSource"; break; 116 #endif 117 #if CLANG_ENABLE_STATIC_ANALYZER 118 case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>(); 119 #else 120 case RunAnalysis: Action = "RunAnalysis"; break; 121 #endif 122 case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>(); 123 case PrintDependencyDirectivesSourceMinimizerOutput: 124 return llvm::make_unique<PrintDependencyDirectivesSourceMinimizerAction>(); 125 } 126 127 #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \ 128 || !CLANG_ENABLE_OBJC_REWRITER 129 CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action; 130 return 0; 131 #else 132 llvm_unreachable("Invalid program action!"); 133 #endif 134 } 135 136 std::unique_ptr<FrontendAction> 137 CreateFrontendAction(CompilerInstance &CI) { 138 // Create the underlying action. 139 std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI); 140 if (!Act) 141 return nullptr; 142 143 const FrontendOptions &FEOpts = CI.getFrontendOpts(); 144 145 if (FEOpts.FixAndRecompile) { 146 Act = llvm::make_unique<FixItRecompile>(std::move(Act)); 147 } 148 149 #if CLANG_ENABLE_ARCMT 150 if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource && 151 CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) { 152 // Potentially wrap the base FE action in an ARC Migrate Tool action. 153 switch (FEOpts.ARCMTAction) { 154 case FrontendOptions::ARCMT_None: 155 break; 156 case FrontendOptions::ARCMT_Check: 157 Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act)); 158 break; 159 case FrontendOptions::ARCMT_Modify: 160 Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act)); 161 break; 162 case FrontendOptions::ARCMT_Migrate: 163 Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act), 164 FEOpts.MTMigrateDir, 165 FEOpts.ARCMTMigrateReportOut, 166 FEOpts.ARCMTMigrateEmitARCErrors); 167 break; 168 } 169 170 if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) { 171 Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act), 172 FEOpts.MTMigrateDir, 173 FEOpts.ObjCMTAction); 174 } 175 } 176 #endif 177 178 // If there are any AST files to merge, create a frontend action 179 // adaptor to perform the merge. 180 if (!FEOpts.ASTMergeFiles.empty()) 181 Act = llvm::make_unique<ASTMergeAction>(std::move(Act), 182 FEOpts.ASTMergeFiles); 183 184 return Act; 185 } 186 187 bool ExecuteCompilerInvocation(CompilerInstance *Clang) { 188 // Honor -help. 189 if (Clang->getFrontendOpts().ShowHelp) { 190 std::unique_ptr<OptTable> Opts = driver::createDriverOptTable(); 191 Opts->PrintHelp(llvm::outs(), "clang -cc1 [options] file...", 192 "LLVM 'Clang' Compiler: http://clang.llvm.org", 193 /*Include=*/driver::options::CC1Option, 194 /*Exclude=*/0, /*ShowAllAliases=*/false); 195 return true; 196 } 197 198 // Honor -version. 199 // 200 // FIXME: Use a better -version message? 201 if (Clang->getFrontendOpts().ShowVersion) { 202 llvm::cl::PrintVersionMessage(); 203 return true; 204 } 205 206 // Load any requested plugins. 207 for (unsigned i = 0, 208 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) { 209 const std::string &Path = Clang->getFrontendOpts().Plugins[i]; 210 std::string Error; 211 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) 212 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin) 213 << Path << Error; 214 } 215 216 // Check if any of the loaded plugins replaces the main AST action 217 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(), 218 ie = FrontendPluginRegistry::end(); 219 it != ie; ++it) { 220 std::unique_ptr<PluginASTAction> P(it->instantiate()); 221 if (P->getActionType() == PluginASTAction::ReplaceAction) { 222 Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction; 223 Clang->getFrontendOpts().ActionName = it->getName(); 224 break; 225 } 226 } 227 228 // Honor -mllvm. 229 // 230 // FIXME: Remove this, one day. 231 // This should happen AFTER plugins have been loaded! 232 if (!Clang->getFrontendOpts().LLVMArgs.empty()) { 233 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); 234 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2); 235 Args[0] = "clang (LLVM option parsing)"; 236 for (unsigned i = 0; i != NumArgs; ++i) 237 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); 238 Args[NumArgs + 1] = nullptr; 239 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get()); 240 } 241 242 #if CLANG_ENABLE_STATIC_ANALYZER 243 // These should happen AFTER plugins have been loaded! 244 245 AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts(); 246 // Honor -analyzer-checker-help and -analyzer-checker-help-hidden. 247 if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha || 248 AnOpts.ShowCheckerHelpDeveloper) { 249 ento::printCheckerHelp(llvm::outs(), 250 Clang->getFrontendOpts().Plugins, 251 AnOpts, 252 Clang->getDiagnostics(), 253 Clang->getLangOpts()); 254 return true; 255 } 256 257 // Honor -analyzer-checker-option-help. 258 if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList || 259 AnOpts.ShowCheckerOptionDeveloperList) { 260 ento::printCheckerConfigList(llvm::outs(), 261 Clang->getFrontendOpts().Plugins, 262 *Clang->getAnalyzerOpts(), 263 Clang->getDiagnostics(), 264 Clang->getLangOpts()); 265 return true; 266 } 267 268 // Honor -analyzer-list-enabled-checkers. 269 if (AnOpts.ShowEnabledCheckerList) { 270 ento::printEnabledCheckerList(llvm::outs(), 271 Clang->getFrontendOpts().Plugins, 272 AnOpts, 273 Clang->getDiagnostics(), 274 Clang->getLangOpts()); 275 } 276 277 // Honor -analyzer-config-help. 278 if (AnOpts.ShowConfigOptionsList) { 279 ento::printAnalyzerConfigList(llvm::outs()); 280 return true; 281 } 282 #endif 283 284 // If there were errors in processing arguments, don't do anything else. 285 if (Clang->getDiagnostics().hasErrorOccurred()) 286 return false; 287 // Create and execute the frontend action. 288 std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang)); 289 if (!Act) 290 return false; 291 bool Success = Clang->ExecuteAction(*Act); 292 if (Clang->getFrontendOpts().DisableFree) 293 llvm::BuryPointer(std::move(Act)); 294 return Success; 295 } 296 297 } // namespace clang 298