ExecuteCompilerInvocation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 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 GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
  61. case GeneratePTH: return llvm::make_unique<GeneratePTHAction>();
  62. case InitOnly: return llvm::make_unique<InitOnlyAction>();
  63. case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
  64. case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
  65. case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
  66. case TemplightDump: return llvm::make_unique<TemplightDumpAction>();
  67. case PluginAction: {
  68. for (FrontendPluginRegistry::iterator it =
  69. FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
  70. it != ie; ++it) {
  71. if (it->getName() == CI.getFrontendOpts().ActionName) {
  72. std::unique_ptr<PluginASTAction> P(it->instantiate());
  73. if ((P->getActionType() != PluginASTAction::ReplaceAction &&
  74. P->getActionType() != PluginASTAction::Cmdline) ||
  75. !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
  76. return nullptr;
  77. return std::move(P);
  78. }
  79. }
  80. CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
  81. << CI.getFrontendOpts().ActionName;
  82. return nullptr;
  83. }
  84. case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>();
  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. }
  112. #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
  113. || !CLANG_ENABLE_OBJC_REWRITER
  114. CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
  115. return 0;
  116. #else
  117. llvm_unreachable("Invalid program action!");
  118. #endif
  119. }
  120. std::unique_ptr<FrontendAction>
  121. CreateFrontendAction(CompilerInstance &CI) {
  122. // Create the underlying action.
  123. std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
  124. if (!Act)
  125. return nullptr;
  126. const FrontendOptions &FEOpts = CI.getFrontendOpts();
  127. if (FEOpts.FixAndRecompile) {
  128. Act = llvm::make_unique<FixItRecompile>(std::move(Act));
  129. }
  130. #if CLANG_ENABLE_ARCMT
  131. if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
  132. CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
  133. // Potentially wrap the base FE action in an ARC Migrate Tool action.
  134. switch (FEOpts.ARCMTAction) {
  135. case FrontendOptions::ARCMT_None:
  136. break;
  137. case FrontendOptions::ARCMT_Check:
  138. Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
  139. break;
  140. case FrontendOptions::ARCMT_Modify:
  141. Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
  142. break;
  143. case FrontendOptions::ARCMT_Migrate:
  144. Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
  145. FEOpts.MTMigrateDir,
  146. FEOpts.ARCMTMigrateReportOut,
  147. FEOpts.ARCMTMigrateEmitARCErrors);
  148. break;
  149. }
  150. if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
  151. Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
  152. FEOpts.MTMigrateDir,
  153. FEOpts.ObjCMTAction);
  154. }
  155. }
  156. #endif
  157. // If there are any AST files to merge, create a frontend action
  158. // adaptor to perform the merge.
  159. if (!FEOpts.ASTMergeFiles.empty())
  160. Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
  161. FEOpts.ASTMergeFiles);
  162. return Act;
  163. }
  164. bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
  165. // Honor -help.
  166. if (Clang->getFrontendOpts().ShowHelp) {
  167. std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
  168. Opts->PrintHelp(llvm::outs(), "clang -cc1",
  169. "LLVM 'Clang' Compiler: http://clang.llvm.org",
  170. /*Include=*/driver::options::CC1Option,
  171. /*Exclude=*/0, /*ShowAllAliases=*/false);
  172. return true;
  173. }
  174. // Honor -version.
  175. //
  176. // FIXME: Use a better -version message?
  177. if (Clang->getFrontendOpts().ShowVersion) {
  178. llvm::cl::PrintVersionMessage();
  179. return true;
  180. }
  181. // Load any requested plugins.
  182. for (unsigned i = 0,
  183. e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
  184. const std::string &Path = Clang->getFrontendOpts().Plugins[i];
  185. std::string Error;
  186. if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
  187. Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
  188. << Path << Error;
  189. }
  190. // Check if any of the loaded plugins replaces the main AST action
  191. for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
  192. ie = FrontendPluginRegistry::end();
  193. it != ie; ++it) {
  194. std::unique_ptr<PluginASTAction> P(it->instantiate());
  195. if (P->getActionType() == PluginASTAction::ReplaceAction) {
  196. Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
  197. Clang->getFrontendOpts().ActionName = it->getName();
  198. break;
  199. }
  200. }
  201. // Honor -mllvm.
  202. //
  203. // FIXME: Remove this, one day.
  204. // This should happen AFTER plugins have been loaded!
  205. if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
  206. unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
  207. auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
  208. Args[0] = "clang (LLVM option parsing)";
  209. for (unsigned i = 0; i != NumArgs; ++i)
  210. Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
  211. Args[NumArgs + 1] = nullptr;
  212. llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
  213. }
  214. #if CLANG_ENABLE_STATIC_ANALYZER
  215. // Honor -analyzer-checker-help.
  216. // This should happen AFTER plugins have been loaded!
  217. if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
  218. ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
  219. return true;
  220. }
  221. if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
  222. ento::printEnabledCheckerList(llvm::outs(),
  223. Clang->getFrontendOpts().Plugins,
  224. *Clang->getAnalyzerOpts());
  225. }
  226. #endif
  227. // If there were errors in processing arguments, don't do anything else.
  228. if (Clang->getDiagnostics().hasErrorOccurred())
  229. return false;
  230. // Create and execute the frontend action.
  231. std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
  232. if (!Act)
  233. return false;
  234. bool Success = Clang->ExecuteAction(*Act);
  235. if (Clang->getFrontendOpts().DisableFree)
  236. BuryPointer(std::move(Act));
  237. return Success;
  238. }
  239. } // namespace clang