ExecuteCompilerInvocation.cpp 10 KB

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