CommentVisitor.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CommentVisitor.h - Visitor for Comment subclasses --------*- 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_AST_COMMENTVISITOR_H
  14. #define LLVM_CLANG_AST_COMMENTVISITOR_H
  15. #include "clang/AST/Comment.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. namespace clang {
  19. namespace comments {
  20. template <template <typename> class Ptr, typename ImplClass,
  21. typename RetTy = void, class... ParamTys>
  22. class CommentVisitorBase {
  23. public:
  24. #define PTR(CLASS) typename Ptr<CLASS>::type
  25. #define DISPATCH(NAME, CLASS) \
  26. return static_cast<ImplClass *>(this)->visit##NAME( \
  27. static_cast<PTR(CLASS)>(C), std::forward<ParamTys>(P)...)
  28. RetTy visit(PTR(Comment) C, ParamTys... P) {
  29. if (!C)
  30. return RetTy();
  31. switch (C->getCommentKind()) {
  32. default: llvm_unreachable("Unknown comment kind!");
  33. #define ABSTRACT_COMMENT(COMMENT)
  34. #define COMMENT(CLASS, PARENT) \
  35. case Comment::CLASS##Kind: DISPATCH(CLASS, CLASS);
  36. #include "clang/AST/CommentNodes.inc"
  37. #undef ABSTRACT_COMMENT
  38. #undef COMMENT
  39. }
  40. }
  41. // If the derived class does not implement a certain Visit* method, fall back
  42. // on Visit* method for the superclass.
  43. #define ABSTRACT_COMMENT(COMMENT) COMMENT
  44. #define COMMENT(CLASS, PARENT) \
  45. RetTy visit##CLASS(PTR(CLASS) C, ParamTys... P) { DISPATCH(PARENT, PARENT); }
  46. #include "clang/AST/CommentNodes.inc"
  47. #undef ABSTRACT_COMMENT
  48. #undef COMMENT
  49. RetTy visitComment(PTR(Comment) C, ParamTys... P) { return RetTy(); }
  50. #undef PTR
  51. #undef DISPATCH
  52. };
  53. template <typename ImplClass, typename RetTy = void, class... ParamTys>
  54. class CommentVisitor : public CommentVisitorBase<std::add_pointer, ImplClass,
  55. RetTy, ParamTys...> {};
  56. template <typename ImplClass, typename RetTy = void, class... ParamTys>
  57. class ConstCommentVisitor
  58. : public CommentVisitorBase<llvm::make_const_ptr, ImplClass, RetTy,
  59. ParamTys...> {};
  60. } // namespace comments
  61. } // namespace clang
  62. #endif // LLVM_CLANG_AST_COMMENTVISITOR_H
  63. #ifdef __GNUC__
  64. #pragma GCC diagnostic pop
  65. #endif