ExecuteCompilerInvocation.cpp 12 KB

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