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