ExecuteCompilerInvocation.cpp 11 KB

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