ExecuteCompilerInvocation.cpp 12 KB

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