ExecuteCompilerInvocation.cpp 11 KB

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