FrontendActions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
  14. #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
  15. #include "clang/Frontend/FrontendAction.h"
  16. #include <memory>
  17. #include <string>
  18. #include <vector>
  19. namespace clang {
  20. //===----------------------------------------------------------------------===//
  21. // Custom Consumer Actions
  22. //===----------------------------------------------------------------------===//
  23. class InitOnlyAction : public FrontendAction {
  24. void ExecuteAction() override;
  25. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  26. StringRef InFile) override;
  27. public:
  28. // Don't claim to only use the preprocessor, we want to follow the AST path,
  29. // but do nothing.
  30. bool usesPreprocessorOnly() const override { return false; }
  31. };
  32. /// Preprocessor-based frontend action that also loads PCH files.
  33. class ReadPCHAndPreprocessAction : public FrontendAction {
  34. void ExecuteAction() override;
  35. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  36. StringRef InFile) override;
  37. public:
  38. bool usesPreprocessorOnly() const override { return false; }
  39. };
  40. class DumpCompilerOptionsAction : public FrontendAction {
  41. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  42. StringRef InFile) override {
  43. return nullptr;
  44. }
  45. void ExecuteAction() override;
  46. public:
  47. bool usesPreprocessorOnly() const override { return true; }
  48. };
  49. //===----------------------------------------------------------------------===//
  50. // AST Consumer Actions
  51. //===----------------------------------------------------------------------===//
  52. class ASTPrintAction : public ASTFrontendAction {
  53. protected:
  54. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  55. StringRef InFile) override;
  56. };
  57. class ASTDumpAction : public ASTFrontendAction {
  58. protected:
  59. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  60. StringRef InFile) override;
  61. };
  62. class ASTDeclListAction : public ASTFrontendAction {
  63. protected:
  64. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  65. StringRef InFile) override;
  66. };
  67. class ASTViewAction : public ASTFrontendAction {
  68. protected:
  69. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  70. StringRef InFile) override;
  71. };
  72. class GeneratePCHAction : public ASTFrontendAction {
  73. protected:
  74. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  75. StringRef InFile) override;
  76. TranslationUnitKind getTranslationUnitKind() override {
  77. return TU_Prefix;
  78. }
  79. bool hasASTFileSupport() const override { return false; }
  80. bool shouldEraseOutputFiles() override;
  81. public:
  82. /// Compute the AST consumer arguments that will be used to
  83. /// create the PCHGenerator instance returned by CreateASTConsumer.
  84. ///
  85. /// \returns false if an error occurred, true otherwise.
  86. static bool ComputeASTConsumerArguments(CompilerInstance &CI,
  87. std::string &Sysroot);
  88. /// Creates file to write the PCH into and returns a stream to write it
  89. /// into. On error, returns null.
  90. static std::unique_ptr<llvm::raw_pwrite_stream>
  91. CreateOutputFile(CompilerInstance &CI, StringRef InFile,
  92. std::string &OutputFile);
  93. bool BeginSourceFileAction(CompilerInstance &CI) override;
  94. };
  95. class GenerateModuleAction : public ASTFrontendAction {
  96. virtual std::unique_ptr<raw_pwrite_stream>
  97. CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
  98. protected:
  99. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  100. StringRef InFile) override;
  101. TranslationUnitKind getTranslationUnitKind() override {
  102. return TU_Module;
  103. }
  104. bool hasASTFileSupport() const override { return false; }
  105. bool shouldEraseOutputFiles() override;
  106. };
  107. class GenerateInterfaceStubsAction : public ASTFrontendAction {
  108. protected:
  109. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  110. StringRef InFile) override;
  111. TranslationUnitKind getTranslationUnitKind() override { return TU_Module; }
  112. bool hasASTFileSupport() const override { return false; }
  113. };
  114. class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
  115. private:
  116. bool BeginSourceFileAction(CompilerInstance &CI) override;
  117. std::unique_ptr<raw_pwrite_stream>
  118. CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
  119. };
  120. class GenerateModuleInterfaceAction : public GenerateModuleAction {
  121. private:
  122. bool BeginSourceFileAction(CompilerInstance &CI) override;
  123. std::unique_ptr<raw_pwrite_stream>
  124. CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
  125. };
  126. class GenerateHeaderUnitAction : public GenerateModuleAction {
  127. private:
  128. bool BeginSourceFileAction(CompilerInstance &CI) override;
  129. std::unique_ptr<raw_pwrite_stream>
  130. CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
  131. };
  132. class SyntaxOnlyAction : public ASTFrontendAction {
  133. protected:
  134. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  135. StringRef InFile) override;
  136. public:
  137. ~SyntaxOnlyAction() override;
  138. bool hasCodeCompletionSupport() const override { return true; }
  139. };
  140. /// Dump information about the given module file, to be used for
  141. /// basic debugging and discovery.
  142. class DumpModuleInfoAction : public ASTFrontendAction {
  143. public:
  144. // Allow other tools (ex lldb) to direct output for their use.
  145. llvm::raw_ostream *OutputStream = nullptr;
  146. protected:
  147. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  148. StringRef InFile) override;
  149. bool BeginInvocation(CompilerInstance &CI) override;
  150. void ExecuteAction() override;
  151. public:
  152. bool hasPCHSupport() const override { return false; }
  153. bool hasASTFileSupport() const override { return true; }
  154. bool hasIRSupport() const override { return false; }
  155. bool hasCodeCompletionSupport() const override { return false; }
  156. };
  157. class VerifyPCHAction : public ASTFrontendAction {
  158. protected:
  159. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  160. StringRef InFile) override;
  161. void ExecuteAction() override;
  162. public:
  163. bool hasCodeCompletionSupport() const override { return false; }
  164. };
  165. class TemplightDumpAction : public ASTFrontendAction {
  166. protected:
  167. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  168. StringRef InFile) override;
  169. void ExecuteAction() override;
  170. };
  171. /**
  172. * Frontend action adaptor that merges ASTs together.
  173. *
  174. * This action takes an existing AST file and "merges" it into the AST
  175. * context, producing a merged context. This action is an action
  176. * adaptor, which forwards most of its calls to another action that
  177. * will consume the merged context.
  178. */
  179. class ASTMergeAction : public FrontendAction {
  180. /// The action that the merge action adapts.
  181. std::unique_ptr<FrontendAction> AdaptedAction;
  182. /// The set of AST files to merge.
  183. std::vector<std::string> ASTFiles;
  184. protected:
  185. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  186. StringRef InFile) override;
  187. bool BeginSourceFileAction(CompilerInstance &CI) override;
  188. void ExecuteAction() override;
  189. void EndSourceFileAction() override;
  190. public:
  191. ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
  192. ArrayRef<std::string> ASTFiles);
  193. ~ASTMergeAction() override;
  194. bool usesPreprocessorOnly() const override;
  195. TranslationUnitKind getTranslationUnitKind() override;
  196. bool hasPCHSupport() const override;
  197. bool hasASTFileSupport() const override;
  198. bool hasCodeCompletionSupport() const override;
  199. };
  200. class PrintPreambleAction : public FrontendAction {
  201. protected:
  202. void ExecuteAction() override;
  203. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
  204. StringRef) override {
  205. return nullptr;
  206. }
  207. bool usesPreprocessorOnly() const override { return true; }
  208. };
  209. class PrintDependencyDirectivesSourceMinimizerAction : public FrontendAction {
  210. protected:
  211. void ExecuteAction() override;
  212. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
  213. StringRef) override {
  214. return nullptr;
  215. }
  216. bool usesPreprocessorOnly() const override { return true; }
  217. };
  218. //===----------------------------------------------------------------------===//
  219. // Preprocessor Actions
  220. //===----------------------------------------------------------------------===//
  221. class DumpRawTokensAction : public PreprocessorFrontendAction {
  222. protected:
  223. void ExecuteAction() override;
  224. };
  225. class DumpTokensAction : public PreprocessorFrontendAction {
  226. protected:
  227. void ExecuteAction() override;
  228. };
  229. class PreprocessOnlyAction : public PreprocessorFrontendAction {
  230. protected:
  231. void ExecuteAction() override;
  232. };
  233. class PrintPreprocessedAction : public PreprocessorFrontendAction {
  234. protected:
  235. void ExecuteAction() override;
  236. bool hasPCHSupport() const override { return true; }
  237. };
  238. class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
  239. StringRef ModuleName;
  240. void ExecuteAction() override;
  241. public:
  242. GetDependenciesByModuleNameAction(StringRef ModuleName)
  243. : ModuleName(ModuleName) {}
  244. };
  245. } // end namespace clang
  246. #endif
  247. #ifdef __GNUC__
  248. #pragma GCC diagnostic pop
  249. #endif