ExecuteCompilerInvocation.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/StaticAnalyzer/Frontend/FrontendActions.h"
  16. #include "clang/ARCMigrate/ARCMTActions.h"
  17. #include "clang/CodeGen/CodeGenAction.h"
  18. #include "clang/Driver/CC1Options.h"
  19. #include "clang/Driver/OptTable.h"
  20. #include "clang/Frontend/CompilerInvocation.h"
  21. #include "clang/Frontend/CompilerInstance.h"
  22. #include "clang/Frontend/FrontendActions.h"
  23. #include "clang/Frontend/FrontendDiagnostic.h"
  24. #include "clang/Frontend/FrontendPluginRegistry.h"
  25. #include "clang/Rewrite/FrontendActions.h"
  26. #include "llvm/Support/ErrorHandling.h"
  27. #include "llvm/Support/DynamicLibrary.h"
  28. using namespace clang;
  29. static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
  30. using namespace clang::frontend;
  31. switch (CI.getFrontendOpts().ProgramAction) {
  32. case ASTDump: return new ASTDumpAction();
  33. case ASTDumpXML: return new ASTDumpXMLAction();
  34. case ASTPrint: return new ASTPrintAction();
  35. case ASTView: return new ASTViewAction();
  36. case DumpRawTokens: return new DumpRawTokensAction();
  37. case DumpTokens: return new DumpTokensAction();
  38. case EmitAssembly: return new EmitAssemblyAction();
  39. case EmitBC: return new EmitBCAction();
  40. case EmitHTML: return new HTMLPrintAction();
  41. case EmitLLVM: return new EmitLLVMAction();
  42. case EmitLLVMOnly: return new EmitLLVMOnlyAction();
  43. case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
  44. case EmitObj: return new EmitObjAction();
  45. case FixIt: return new FixItAction();
  46. case GenerateModule: return new GenerateModuleAction;
  47. case GeneratePCH: return new GeneratePCHAction;
  48. case GeneratePTH: return new GeneratePTHAction();
  49. case InitOnly: return new InitOnlyAction();
  50. case ParseSyntaxOnly: return new SyntaxOnlyAction();
  51. case PluginAction: {
  52. for (FrontendPluginRegistry::iterator it =
  53. FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
  54. it != ie; ++it) {
  55. if (it->getName() == CI.getFrontendOpts().ActionName) {
  56. OwningPtr<PluginASTAction> P(it->instantiate());
  57. if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
  58. return 0;
  59. return P.take();
  60. }
  61. }
  62. CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
  63. << CI.getFrontendOpts().ActionName;
  64. return 0;
  65. }
  66. case PrintDeclContext: return new DeclContextPrintAction();
  67. case PrintPreamble: return new PrintPreambleAction();
  68. case PrintPreprocessedInput: return new PrintPreprocessedAction();
  69. case RewriteMacros: return new RewriteMacrosAction();
  70. case RewriteObjC: return new RewriteObjCAction();
  71. case RewriteTest: return new RewriteTestAction();
  72. case RunAnalysis: return new ento::AnalysisAction();
  73. case RunPreprocessorOnly: return new PreprocessOnlyAction();
  74. }
  75. llvm_unreachable("Invalid program action!");
  76. }
  77. static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
  78. // Create the underlying action.
  79. FrontendAction *Act = CreateFrontendBaseAction(CI);
  80. if (!Act)
  81. return 0;
  82. const FrontendOptions &FEOpts = CI.getFrontendOpts();
  83. if (FEOpts.FixAndRecompile) {
  84. Act = new FixItRecompile(Act);
  85. }
  86. // Potentially wrap the base FE action in an ARC Migrate Tool action.
  87. switch (FEOpts.ARCMTAction) {
  88. case FrontendOptions::ARCMT_None:
  89. break;
  90. case FrontendOptions::ARCMT_Check:
  91. Act = new arcmt::CheckAction(Act);
  92. break;
  93. case FrontendOptions::ARCMT_Modify:
  94. Act = new arcmt::ModifyAction(Act);
  95. break;
  96. case FrontendOptions::ARCMT_Migrate:
  97. Act = new arcmt::MigrateAction(Act,
  98. FEOpts.ARCMTMigrateDir,
  99. FEOpts.ARCMTMigrateReportOut,
  100. FEOpts.ARCMTMigrateEmitARCErrors);
  101. break;
  102. }
  103. // If there are any AST files to merge, create a frontend action
  104. // adaptor to perform the merge.
  105. if (!FEOpts.ASTMergeFiles.empty())
  106. Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
  107. return Act;
  108. }
  109. bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
  110. // Honor -help.
  111. if (Clang->getFrontendOpts().ShowHelp) {
  112. OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable());
  113. Opts->PrintHelp(llvm::outs(), "clang -cc1",
  114. "LLVM 'Clang' Compiler: http://clang.llvm.org");
  115. return 0;
  116. }
  117. // Honor -version.
  118. //
  119. // FIXME: Use a better -version message?
  120. if (Clang->getFrontendOpts().ShowVersion) {
  121. llvm::cl::PrintVersionMessage();
  122. return 0;
  123. }
  124. // Load any requested plugins.
  125. for (unsigned i = 0,
  126. e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
  127. const std::string &Path = Clang->getFrontendOpts().Plugins[i];
  128. std::string Error;
  129. if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
  130. Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
  131. << Path << Error;
  132. }
  133. // Honor -mllvm.
  134. //
  135. // FIXME: Remove this, one day.
  136. // This should happen AFTER plugins have been loaded!
  137. if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
  138. unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
  139. const char **Args = new const char*[NumArgs + 2];
  140. Args[0] = "clang (LLVM option parsing)";
  141. for (unsigned i = 0; i != NumArgs; ++i)
  142. Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
  143. Args[NumArgs + 1] = 0;
  144. llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args));
  145. }
  146. // Honor -analyzer-checker-help.
  147. // This should happen AFTER plugins have been loaded!
  148. if (Clang->getAnalyzerOpts().ShowCheckerHelp) {
  149. ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
  150. return 0;
  151. }
  152. // If there were errors in processing arguments, don't do anything else.
  153. bool Success = false;
  154. if (!Clang->getDiagnostics().hasErrorOccurred()) {
  155. // Create and execute the frontend action.
  156. OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
  157. if (Act) {
  158. Success = Clang->ExecuteAction(*Act);
  159. if (Clang->getFrontendOpts().DisableFree)
  160. Act.take();
  161. }
  162. }
  163. return Success;
  164. }