1 //===--- RawCommentList.h - Classes for processing raw comments -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_AST_RAW_COMMENT_LIST_H
11 #define LLVM_CLANG_AST_RAW_COMMENT_LIST_H
13 #include "clang/Basic/SourceManager.h"
14 #include "llvm/ADT/ArrayRef.h"
25 } // end namespace comments
30 RCK_Invalid, ///< Invalid comment
31 RCK_OrdinaryBCPL, ///< Any normal BCPL comments
32 RCK_OrdinaryC, ///< Any normal C comment
33 RCK_BCPLSlash, ///< \code /// stuff \endcode
34 RCK_BCPLExcl, ///< \code //! stuff \endcode
35 RCK_JavaDoc, ///< \code /** stuff */ \endcode
36 RCK_Qt, ///< \code /*! stuff */ \endcode, also used by HeaderDoc
37 RCK_Merged ///< Two or more documentation comments merged together
40 RawComment() : Kind(RCK_Invalid), IsAlmostTrailingComment(false) { }
42 RawComment(const SourceManager &SourceMgr, SourceRange SR,
45 CommentKind getKind() const LLVM_READONLY {
46 return (CommentKind) Kind;
49 bool isInvalid() const LLVM_READONLY {
50 return Kind == RCK_Invalid;
53 bool isMerged() const LLVM_READONLY {
54 return Kind == RCK_Merged;
57 /// Is this comment attached to any declaration?
58 bool isAttached() const LLVM_READONLY {
66 /// Returns true if it is a comment that should be put after a member:
67 /// \code ///< stuff \endcode
68 /// \code //!< stuff \endcode
69 /// \code /**< stuff */ \endcode
70 /// \code /*!< stuff */ \endcode
71 bool isTrailingComment() const LLVM_READONLY {
72 assert(isDocumentation());
73 return IsTrailingComment;
76 /// Returns true if it is a probable typo:
77 /// \code //< stuff \endcode
78 /// \code /*< stuff */ \endcode
79 bool isAlmostTrailingComment() const LLVM_READONLY {
80 return IsAlmostTrailingComment;
83 /// Returns true if this comment is not a documentation comment.
84 bool isOrdinary() const LLVM_READONLY {
85 return (Kind == RCK_OrdinaryBCPL) || (Kind == RCK_OrdinaryC);
88 /// Returns true if this comment any kind of a documentation comment.
89 bool isDocumentation() const LLVM_READONLY {
90 return !isInvalid() && !isOrdinary();
93 /// Returns raw comment text with comment markers.
94 StringRef getRawText(const SourceManager &SourceMgr) const {
98 RawText = getRawTextSlow(SourceMgr);
103 SourceRange getSourceRange() const LLVM_READONLY {
107 unsigned getBeginLine(const SourceManager &SM) const;
108 unsigned getEndLine(const SourceManager &SM) const;
110 const char *getBriefText(const ASTContext &Context) const {
114 return extractBriefText(Context);
117 /// Parse the comment, assuming it is attached to decl \c D.
118 comments::FullComment *parse(const ASTContext &Context,
119 const Preprocessor *PP, const Decl *D) const;
124 mutable StringRef RawText;
125 mutable const char *BriefText;
127 mutable bool RawTextValid : 1; ///< True if RawText is valid
128 mutable bool BriefTextValid : 1; ///< True if BriefText is valid
132 /// True if comment is attached to a declaration in ASTContext.
135 bool IsTrailingComment : 1;
136 bool IsAlmostTrailingComment : 1;
138 mutable bool BeginLineValid : 1; ///< True if BeginLine is valid
139 mutable bool EndLineValid : 1; ///< True if EndLine is valid
140 mutable unsigned BeginLine; ///< Cached line number
141 mutable unsigned EndLine; ///< Cached line number
143 /// \brief Constructor for AST deserialization.
144 RawComment(SourceRange SR, CommentKind K, bool IsTrailingComment,
145 bool IsAlmostTrailingComment) :
146 Range(SR), RawTextValid(false), BriefTextValid(false), Kind(K),
147 IsAttached(false), IsTrailingComment(IsTrailingComment),
148 IsAlmostTrailingComment(IsAlmostTrailingComment),
149 BeginLineValid(false), EndLineValid(false)
152 StringRef getRawTextSlow(const SourceManager &SourceMgr) const;
154 const char *extractBriefText(const ASTContext &Context) const;
156 friend class ASTReader;
159 /// \brief Compare comments' source locations.
161 class BeforeThanCompare<RawComment> {
162 const SourceManager &SM;
165 explicit BeforeThanCompare(const SourceManager &SM) : SM(SM) { }
167 bool operator()(const RawComment &LHS, const RawComment &RHS) {
168 return SM.isBeforeInTranslationUnit(LHS.getSourceRange().getBegin(),
169 RHS.getSourceRange().getBegin());
172 bool operator()(const RawComment *LHS, const RawComment *RHS) {
173 return operator()(*LHS, *RHS);
177 /// \brief This class represents all comments included in the translation unit,
178 /// sorted in order of appearance in the translation unit.
179 class RawCommentList {
181 RawCommentList(SourceManager &SourceMgr) :
182 SourceMgr(SourceMgr), OnlyWhitespaceSeen(true) { }
184 void addComment(const RawComment &RC, llvm::BumpPtrAllocator &Allocator);
186 ArrayRef<RawComment *> getComments() const {
191 SourceManager &SourceMgr;
192 std::vector<RawComment *> Comments;
193 SourceLocation PrevCommentEndLoc;
194 bool OnlyWhitespaceSeen;
196 void addCommentsToFront(const std::vector<RawComment *> &C) {
197 size_t OldSize = Comments.size();
198 Comments.resize(C.size() + OldSize);
199 std::copy_backward(Comments.begin(), Comments.begin() + OldSize,
201 std::copy(C.begin(), C.end(), Comments.begin());
204 friend class ASTReader;
207 } // end namespace clang