RawCommentList.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- RawCommentList.h - Classes for processing raw comments -*- 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_RAWCOMMENTLIST_H
  14. #define LLVM_CLANG_AST_RAWCOMMENTLIST_H
  15. #include "clang/Basic/CommentOptions.h"
  16. #include "clang/Basic/SourceLocation.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/Support/Allocator.h"
  20. #include <map>
  21. namespace clang {
  22. class ASTContext;
  23. class ASTReader;
  24. class Decl;
  25. class DiagnosticsEngine;
  26. class Preprocessor;
  27. class SourceManager;
  28. namespace comments {
  29. class FullComment;
  30. } // end namespace comments
  31. class RawComment {
  32. public:
  33. enum CommentKind {
  34. RCK_Invalid, ///< Invalid comment
  35. RCK_OrdinaryBCPL, ///< Any normal BCPL comments
  36. RCK_OrdinaryC, ///< Any normal C comment
  37. RCK_BCPLSlash, ///< \code /// stuff \endcode
  38. RCK_BCPLExcl, ///< \code //! stuff \endcode
  39. RCK_JavaDoc, ///< \code /** stuff */ \endcode
  40. RCK_Qt, ///< \code /*! stuff */ \endcode, also used by HeaderDoc
  41. RCK_Merged ///< Two or more documentation comments merged together
  42. };
  43. RawComment() : Kind(RCK_Invalid), IsAlmostTrailingComment(false) { }
  44. RawComment(const SourceManager &SourceMgr, SourceRange SR,
  45. const CommentOptions &CommentOpts, bool Merged);
  46. CommentKind getKind() const LLVM_READONLY {
  47. return (CommentKind) Kind;
  48. }
  49. bool isInvalid() const LLVM_READONLY {
  50. return Kind == RCK_Invalid;
  51. }
  52. bool isMerged() const LLVM_READONLY {
  53. return Kind == RCK_Merged;
  54. }
  55. /// Is this comment attached to any declaration?
  56. bool isAttached() const LLVM_READONLY {
  57. return IsAttached;
  58. }
  59. void setAttached() {
  60. IsAttached = true;
  61. }
  62. /// Returns true if it is a comment that should be put after a member:
  63. /// \code ///< stuff \endcode
  64. /// \code //!< stuff \endcode
  65. /// \code /**< stuff */ \endcode
  66. /// \code /*!< stuff */ \endcode
  67. bool isTrailingComment() const LLVM_READONLY {
  68. return IsTrailingComment;
  69. }
  70. /// Returns true if it is a probable typo:
  71. /// \code //< stuff \endcode
  72. /// \code /*< stuff */ \endcode
  73. bool isAlmostTrailingComment() const LLVM_READONLY {
  74. return IsAlmostTrailingComment;
  75. }
  76. /// Returns true if this comment is not a documentation comment.
  77. bool isOrdinary() const LLVM_READONLY {
  78. return ((Kind == RCK_OrdinaryBCPL) || (Kind == RCK_OrdinaryC));
  79. }
  80. /// Returns true if this comment any kind of a documentation comment.
  81. bool isDocumentation() const LLVM_READONLY {
  82. return !isInvalid() && !isOrdinary();
  83. }
  84. /// Returns raw comment text with comment markers.
  85. StringRef getRawText(const SourceManager &SourceMgr) const {
  86. if (RawTextValid)
  87. return RawText;
  88. RawText = getRawTextSlow(SourceMgr);
  89. RawTextValid = true;
  90. return RawText;
  91. }
  92. SourceRange getSourceRange() const LLVM_READONLY { return Range; }
  93. SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
  94. SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
  95. const char *getBriefText(const ASTContext &Context) const {
  96. if (BriefTextValid)
  97. return BriefText;
  98. return extractBriefText(Context);
  99. }
  100. /// Returns sanitized comment text, suitable for presentation in editor UIs.
  101. /// E.g. will transform:
  102. /// // This is a long multiline comment.
  103. /// // Parts of it might be indented.
  104. /// /* The comments styles might be mixed. */
  105. /// into
  106. /// "This is a long multiline comment.\n"
  107. /// " Parts of it might be indented.\n"
  108. /// "The comments styles might be mixed."
  109. /// Also removes leading indentation and sanitizes some common cases:
  110. /// /* This is a first line.
  111. /// * This is a second line. It is indented.
  112. /// * This is a third line. */
  113. /// and
  114. /// /* This is a first line.
  115. /// This is a second line. It is indented.
  116. /// This is a third line. */
  117. /// will both turn into:
  118. /// "This is a first line.\n"
  119. /// " This is a second line. It is indented.\n"
  120. /// "This is a third line."
  121. std::string getFormattedText(const SourceManager &SourceMgr,
  122. DiagnosticsEngine &Diags) const;
  123. /// Parse the comment, assuming it is attached to decl \c D.
  124. comments::FullComment *parse(const ASTContext &Context,
  125. const Preprocessor *PP, const Decl *D) const;
  126. private:
  127. SourceRange Range;
  128. mutable StringRef RawText;
  129. mutable const char *BriefText;
  130. mutable bool RawTextValid : 1; ///< True if RawText is valid
  131. mutable bool BriefTextValid : 1; ///< True if BriefText is valid
  132. unsigned Kind : 3;
  133. /// True if comment is attached to a declaration in ASTContext.
  134. bool IsAttached : 1;
  135. bool IsTrailingComment : 1;
  136. bool IsAlmostTrailingComment : 1;
  137. /// Constructor for AST deserialization.
  138. RawComment(SourceRange SR, CommentKind K, bool IsTrailingComment,
  139. bool IsAlmostTrailingComment) :
  140. Range(SR), RawTextValid(false), BriefTextValid(false), Kind(K),
  141. IsAttached(false), IsTrailingComment(IsTrailingComment),
  142. IsAlmostTrailingComment(IsAlmostTrailingComment)
  143. { }
  144. StringRef getRawTextSlow(const SourceManager &SourceMgr) const;
  145. const char *extractBriefText(const ASTContext &Context) const;
  146. friend class ASTReader;
  147. };
  148. /// This class represents all comments included in the translation unit,
  149. /// sorted in order of appearance in the translation unit.
  150. class RawCommentList {
  151. public:
  152. RawCommentList(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
  153. void addComment(const RawComment &RC, const CommentOptions &CommentOpts,
  154. llvm::BumpPtrAllocator &Allocator);
  155. /// \returns A mapping from an offset of the start of the comment to the
  156. /// comment itself, or nullptr in case there are no comments in \p File.
  157. const std::map<unsigned, RawComment *> *getCommentsInFile(FileID File) const;
  158. bool empty() const;
  159. unsigned getCommentBeginLine(RawComment *C, FileID File,
  160. unsigned Offset) const;
  161. unsigned getCommentEndOffset(RawComment *C) const;
  162. private:
  163. SourceManager &SourceMgr;
  164. // mapping: FileId -> comment begin offset -> comment
  165. llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> OrderedComments;
  166. mutable llvm::DenseMap<RawComment *, unsigned> CommentBeginLine;
  167. mutable llvm::DenseMap<RawComment *, unsigned> CommentEndOffset;
  168. friend class ASTReader;
  169. friend class ASTWriter;
  170. };
  171. } // end namespace clang
  172. #endif
  173. #ifdef __GNUC__
  174. #pragma GCC diagnostic pop
  175. #endif