ExecuteCompilerInvocation.cpp 9.7 KB

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