ExecuteCompilerInvocation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/AnalyzerHelpFlags.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. namespace clang {
  35. static std::unique_ptr<FrontendAction>
  36. CreateFrontendBaseAction(CompilerInstance &CI) {
  37. using namespace clang::frontend;
  38. StringRef Action("unknown");
  39. (void)Action;
  40. switch (CI.getFrontendOpts().ProgramAction) {
  41. case ASTDeclList: return std::make_unique<ASTDeclListAction>();
  42. case ASTDump: return std::make_unique<ASTDumpAction>();
  43. case ASTPrint: return std::make_unique<ASTPrintAction>();
  44. case ASTView: return std::make_unique<ASTViewAction>();
  45. case DumpCompilerOptions:
  46. return std::make_unique<DumpCompilerOptionsAction>();
  47. case DumpRawTokens: return std::make_unique<DumpRawTokensAction>();
  48. case DumpTokens: return std::make_unique<DumpTokensAction>();
  49. case EmitAssembly: return std::make_unique<EmitAssemblyAction>();
  50. case EmitBC: return std::make_unique<EmitBCAction>();
  51. case EmitHTML: return std::make_unique<HTMLPrintAction>();
  52. case EmitLLVM: return std::make_unique<EmitLLVMAction>();
  53. case EmitLLVMOnly: return std::make_unique<EmitLLVMOnlyAction>();
  54. case EmitCodeGenOnly: return std::make_unique<EmitCodeGenOnlyAction>();
  55. case EmitObj: return std::make_unique<EmitObjAction>();
  56. case ExtractAPI:
  57. return std::make_unique<ExtractAPIAction>();
  58. case FixIt: return std::make_unique<FixItAction>();
  59. case GenerateModule:
  60. return std::make_unique<GenerateModuleFromModuleMapAction>();
  61. case GenerateModuleInterface:
  62. return std::make_unique<GenerateModuleInterfaceAction>();
  63. case GenerateHeaderModule:
  64. return std::make_unique<GenerateHeaderModuleAction>();
  65. case GeneratePCH: return std::make_unique<GeneratePCHAction>();
  66. case GenerateInterfaceStubs:
  67. return std::make_unique<GenerateInterfaceStubsAction>();
  68. case InitOnly: return std::make_unique<InitOnlyAction>();
  69. case ParseSyntaxOnly: return std::make_unique<SyntaxOnlyAction>();
  70. case ModuleFileInfo: return std::make_unique<DumpModuleInfoAction>();
  71. case VerifyPCH: return std::make_unique<VerifyPCHAction>();
  72. case TemplightDump: return std::make_unique<TemplightDumpAction>();
  73. case PluginAction: {
  74. for (const FrontendPluginRegistry::entry &Plugin :
  75. FrontendPluginRegistry::entries()) {
  76. if (Plugin.getName() == CI.getFrontendOpts().ActionName) {
  77. std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
  78. if ((P->getActionType() != PluginASTAction::ReplaceAction &&
  79. P->getActionType() != PluginASTAction::CmdlineAfterMainAction) ||
  80. !P->ParseArgs(
  81. CI,
  82. CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())]))
  83. return nullptr;
  84. return std::move(P);
  85. }
  86. }
  87. CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
  88. << CI.getFrontendOpts().ActionName;
  89. return nullptr;
  90. }
  91. case PrintPreamble: return std::make_unique<PrintPreambleAction>();
  92. case PrintPreprocessedInput: {
  93. if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
  94. CI.getPreprocessorOutputOpts().RewriteImports)
  95. return std::make_unique<RewriteIncludesAction>();
  96. return std::make_unique<PrintPreprocessedAction>();
  97. }
  98. case RewriteMacros: return std::make_unique<RewriteMacrosAction>();
  99. case RewriteTest: return std::make_unique<RewriteTestAction>();
  100. #if CLANG_ENABLE_OBJC_REWRITER
  101. case RewriteObjC: return std::make_unique<RewriteObjCAction>();
  102. #else
  103. case RewriteObjC: Action = "RewriteObjC"; break;
  104. #endif
  105. #if CLANG_ENABLE_ARCMT
  106. case MigrateSource:
  107. return std::make_unique<arcmt::MigrateSourceAction>();
  108. #else
  109. case MigrateSource: Action = "MigrateSource"; break;
  110. #endif
  111. #if CLANG_ENABLE_STATIC_ANALYZER
  112. case RunAnalysis: return std::make_unique<ento::AnalysisAction>();
  113. #else
  114. case RunAnalysis: Action = "RunAnalysis"; break;
  115. #endif
  116. case RunPreprocessorOnly: return std::make_unique<PreprocessOnlyAction>();
  117. case PrintDependencyDirectivesSourceMinimizerOutput:
  118. return std::make_unique<PrintDependencyDirectivesSourceMinimizerAction>();
  119. }
  120. #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
  121. || !CLANG_ENABLE_OBJC_REWRITER
  122. CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
  123. return 0;
  124. #else
  125. llvm_unreachable("Invalid program action!");
  126. #endif
  127. }
  128. std::unique_ptr<FrontendAction>
  129. CreateFrontendAction(CompilerInstance &CI) {
  130. // Create the underlying action.
  131. std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
  132. if (!Act)
  133. return nullptr;
  134. const FrontendOptions &FEOpts = CI.getFrontendOpts();
  135. if (FEOpts.FixAndRecompile) {
  136. Act = std::make_unique<FixItRecompile>(std::move(Act));
  137. }
  138. #if 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 = std::make_unique<arcmt::CheckAction>(std::move(Act));
  147. break;
  148. case FrontendOptions::ARCMT_Modify:
  149. Act = std::make_unique<arcmt::ModifyAction>(std::move(Act));
  150. break;
  151. case FrontendOptions::ARCMT_Migrate:
  152. Act = std::make_unique<arcmt::MigrateAction>(std::move(Act),
  153. FEOpts.MTMigrateDir,
  154. FEOpts.ARCMTMigrateReportOut,
  155. FEOpts.ARCMTMigrateEmitARCErrors);
  156. break;
  157. }
  158. if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
  159. Act = std::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
  160. FEOpts.MTMigrateDir,
  161. FEOpts.ObjCMTAction);
  162. }
  163. }
  164. #endif
  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 = std::make_unique<ASTMergeAction>(std::move(Act),
  169. FEOpts.ASTMergeFiles);
  170. return Act;
  171. }
  172. bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
  173. // Honor -help.
  174. if (Clang->getFrontendOpts().ShowHelp) {
  175. driver::getDriverOptTable().printHelp(
  176. llvm::outs(), "clang -cc1 [options] file...",
  177. "LLVM 'Clang' Compiler: http://clang.llvm.org",
  178. /*Include=*/driver::options::CC1Option,
  179. /*Exclude=*/0, /*ShowAllAliases=*/false);
  180. return true;
  181. }
  182. // Honor -version.
  183. //
  184. // FIXME: Use a better -version message?
  185. if (Clang->getFrontendOpts().ShowVersion) {
  186. llvm::cl::PrintVersionMessage();
  187. return true;
  188. }
  189. Clang->LoadRequestedPlugins();
  190. // Honor -mllvm.
  191. //
  192. // FIXME: Remove this, one day.
  193. // This should happen AFTER plugins have been loaded!
  194. if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
  195. unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
  196. auto Args = std::make_unique<const char*[]>(NumArgs + 2);
  197. Args[0] = "clang (LLVM option parsing)";
  198. for (unsigned i = 0; i != NumArgs; ++i)
  199. Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
  200. Args[NumArgs + 1] = nullptr;
  201. llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
  202. }
  203. #if CLANG_ENABLE_STATIC_ANALYZER
  204. // These should happen AFTER plugins have been loaded!
  205. AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts();
  206. // Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
  207. if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||
  208. AnOpts.ShowCheckerHelpDeveloper) {
  209. ento::printCheckerHelp(llvm::outs(), *Clang);
  210. return true;
  211. }
  212. // Honor -analyzer-checker-option-help.
  213. if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList ||
  214. AnOpts.ShowCheckerOptionDeveloperList) {
  215. ento::printCheckerConfigList(llvm::outs(), *Clang);
  216. return true;
  217. }
  218. // Honor -analyzer-list-enabled-checkers.
  219. if (AnOpts.ShowEnabledCheckerList) {
  220. ento::printEnabledCheckerList(llvm::outs(), *Clang);
  221. return true;
  222. }
  223. // Honor -analyzer-config-help.
  224. if (AnOpts.ShowConfigOptionsList) {
  225. ento::printAnalyzerConfigList(llvm::outs());
  226. return true;
  227. }
  228. #endif
  229. // If there were errors in processing arguments, don't do anything else.
  230. if (Clang->getDiagnostics().hasErrorOccurred())
  231. return false;
  232. // Create and execute the frontend action.
  233. std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
  234. if (!Act)
  235. return false;
  236. bool Success = Clang->ExecuteAction(*Act);
  237. if (Clang->getFrontendOpts().DisableFree)
  238. llvm::BuryPointer(std::move(Act));
  239. return Success;
  240. }
  241. } // namespace clang