RawCommentList.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. struct CommentLine {
  124. std::string Text;
  125. PresumedLoc Begin;
  126. PresumedLoc End;
  127. CommentLine(StringRef Text, PresumedLoc Begin, PresumedLoc End)
  128. : Text(Text), Begin(Begin), End(End) {}
  129. };
  130. /// Returns sanitized comment text as separated lines with locations in
  131. /// source, suitable for further processing and rendering requiring source
  132. /// locations.
  133. std::vector<CommentLine> getFormattedLines(const SourceManager &SourceMgr,
  134. DiagnosticsEngine &Diags) const;
  135. /// Parse the comment, assuming it is attached to decl \c D.
  136. comments::FullComment *parse(const ASTContext &Context,
  137. const Preprocessor *PP, const Decl *D) const;
  138. private:
  139. SourceRange Range;
  140. mutable StringRef RawText;
  141. mutable const char *BriefText;
  142. mutable bool RawTextValid : 1; ///< True if RawText is valid
  143. mutable bool BriefTextValid : 1; ///< True if BriefText is valid
  144. unsigned Kind : 3;
  145. /// True if comment is attached to a declaration in ASTContext.
  146. bool IsAttached : 1;
  147. bool IsTrailingComment : 1;
  148. bool IsAlmostTrailingComment : 1;
  149. /// Constructor for AST deserialization.
  150. RawComment(SourceRange SR, CommentKind K, bool IsTrailingComment,
  151. bool IsAlmostTrailingComment) :
  152. Range(SR), RawTextValid(false), BriefTextValid(false), Kind(K),
  153. IsAttached(false), IsTrailingComment(IsTrailingComment),
  154. IsAlmostTrailingComment(IsAlmostTrailingComment)
  155. { }
  156. StringRef getRawTextSlow(const SourceManager &SourceMgr) const;
  157. const char *extractBriefText(const ASTContext &Context) const;
  158. friend class ASTReader;
  159. };
  160. /// This class represents all comments included in the translation unit,
  161. /// sorted in order of appearance in the translation unit.
  162. class RawCommentList {
  163. public:
  164. RawCommentList(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
  165. void addComment(const RawComment &RC, const CommentOptions &CommentOpts,
  166. llvm::BumpPtrAllocator &Allocator);
  167. /// \returns A mapping from an offset of the start of the comment to the
  168. /// comment itself, or nullptr in case there are no comments in \p File.
  169. const std::map<unsigned, RawComment *> *getCommentsInFile(FileID File) const;
  170. bool empty() const;
  171. unsigned getCommentBeginLine(RawComment *C, FileID File,
  172. unsigned Offset) const;
  173. unsigned getCommentEndOffset(RawComment *C) const;
  174. private:
  175. SourceManager &SourceMgr;
  176. // mapping: FileId -> comment begin offset -> comment
  177. llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> OrderedComments;
  178. mutable llvm::DenseMap<RawComment *, unsigned> CommentBeginLine;
  179. mutable llvm::DenseMap<RawComment *, unsigned> CommentEndOffset;
  180. friend class ASTReader;
  181. friend class ASTWriter;
  182. };
  183. } // end namespace clang
  184. #endif
  185. #ifdef __GNUC__
  186. #pragma GCC diagnostic pop
  187. #endif