ExecuteCompilerInvocation.cpp 8.6 KB

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