ExecuteCompilerInvocation.cpp 10 KB

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