ExecuteCompilerInvocation.cpp 10 KB

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