Warning: That file was not part of the compilation database. It may have many parsing errors.

1//===--- RawCommentList.h - Classes for processing raw comments -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_AST_RAWCOMMENTLIST_H
11#define LLVM_CLANG_AST_RAWCOMMENTLIST_H
12
13#include "clang/Basic/CommentOptions.h"
14#include "clang/Basic/SourceManager.h"
15#include "llvm/ADT/ArrayRef.h"
16
17namespace clang {
18
19class ASTContext;
20class ASTReader;
21class Decl;
22class Preprocessor;
23
24namespace comments {
25 class FullComment;
26} // end namespace comments
27
28class RawComment {
29public:
30 enum CommentKind {
31 RCK_Invalid, ///< Invalid comment
32 RCK_OrdinaryBCPL, ///< Any normal BCPL comments
33 RCK_OrdinaryC, ///< Any normal C comment
34 RCK_BCPLSlash, ///< \code /// stuff \endcode
35 RCK_BCPLExcl, ///< \code //! stuff \endcode
36 RCK_JavaDoc, ///< \code /** stuff */ \endcode
37 RCK_Qt, ///< \code /*! stuff */ \endcode, also used by HeaderDoc
38 RCK_Merged ///< Two or more documentation comments merged together
39 };
40
41 RawComment() : Kind(RCK_Invalid), IsAlmostTrailingComment(false) { }
42
43 RawComment(const SourceManager &SourceMgr, SourceRange SR,
44 const CommentOptions &CommentOpts, bool Merged);
45
46 CommentKind getKind() const LLVM_READONLY {
47 return (CommentKind) Kind;
48 }
49
50 bool isInvalid() const LLVM_READONLY {
51 return Kind == RCK_Invalid;
52 }
53
54 bool isMerged() const LLVM_READONLY {
55 return Kind == RCK_Merged;
56 }
57
58 /// Is this comment attached to any declaration?
59 bool isAttached() const LLVM_READONLY {
60 return IsAttached;
61 }
62
63 void setAttached() {
64 IsAttached = true;
65 }
66
67 /// Returns true if it is a comment that should be put after a member:
68 /// \code ///< stuff \endcode
69 /// \code //!< stuff \endcode
70 /// \code /**< stuff */ \endcode
71 /// \code /*!< stuff */ \endcode
72 bool isTrailingComment() const LLVM_READONLY {
73 return IsTrailingComment;
74 }
75
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;
81 }
82
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));
86 }
87
88 /// Returns true if this comment any kind of a documentation comment.
89 bool isDocumentation() const LLVM_READONLY {
90 return !isInvalid() && !isOrdinary();
91 }
92
93 /// Returns raw comment text with comment markers.
94 StringRef getRawText(const SourceManager &SourceMgr) const {
95 if (RawTextValid)
96 return RawText;
97
98 RawText = getRawTextSlow(SourceMgr);
99 RawTextValid = true;
100 return RawText;
101 }
102
103 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
104 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
105 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
106 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
107 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
108
109 const char *getBriefText(const ASTContext &Context) const {
110 if (BriefTextValid)
111 return BriefText;
112
113 return extractBriefText(Context);
114 }
115
116 /// Returns sanitized comment text, suitable for presentation in editor UIs.
117 /// E.g. will transform:
118 /// // This is a long multiline comment.
119 /// // Parts of it might be indented.
120 /// /* The comments styles might be mixed. */
121 /// into
122 /// "This is a long multiline comment.\n"
123 /// " Parts of it might be indented.\n"
124 /// "The comments styles might be mixed."
125 /// Also removes leading indentation and sanitizes some common cases:
126 /// /* This is a first line.
127 /// * This is a second line. It is indented.
128 /// * This is a third line. */
129 /// and
130 /// /* This is a first line.
131 /// This is a second line. It is indented.
132 /// This is a third line. */
133 /// will both turn into:
134 /// "This is a first line.\n"
135 /// " This is a second line. It is indented.\n"
136 /// "This is a third line."
137 std::string getFormattedText(const SourceManager &SourceMgr,
138 DiagnosticsEngine &Diags) const;
139
140 /// Parse the comment, assuming it is attached to decl \c D.
141 comments::FullComment *parse(const ASTContext &Context,
142 const Preprocessor *PP, const Decl *D) const;
143
144private:
145 SourceRange Range;
146
147 mutable StringRef RawText;
148 mutable const char *BriefText;
149
150 mutable bool RawTextValid : 1; ///< True if RawText is valid
151 mutable bool BriefTextValid : 1; ///< True if BriefText is valid
152
153 unsigned Kind : 3;
154
155 /// True if comment is attached to a declaration in ASTContext.
156 bool IsAttached : 1;
157
158 bool IsTrailingComment : 1;
159 bool IsAlmostTrailingComment : 1;
160
161 /// Constructor for AST deserialization.
162 RawComment(SourceRange SR, CommentKind K, bool IsTrailingComment,
163 bool IsAlmostTrailingComment) :
164 Range(SR), RawTextValid(false), BriefTextValid(false), Kind(K),
165 IsAttached(false), IsTrailingComment(IsTrailingComment),
166 IsAlmostTrailingComment(IsAlmostTrailingComment)
167 { }
168
169 StringRef getRawTextSlow(const SourceManager &SourceMgr) const;
170
171 const char *extractBriefText(const ASTContext &Context) const;
172
173 friend class ASTReader;
174};
175
176/// Compare comments' source locations.
177template<>
178class BeforeThanCompare<RawComment> {
179 const SourceManager &SM;
180
181public:
182 explicit BeforeThanCompare(const SourceManager &SM) : SM(SM) { }
183
184 bool operator()(const RawComment &LHS, const RawComment &RHS) {
185 return SM.isBeforeInTranslationUnit(LHS.getLocStart(), RHS.getLocStart());
186 }
187
188 bool operator()(const RawComment *LHS, const RawComment *RHS) {
189 return operator()(*LHS, *RHS);
190 }
191};
192
193/// This class represents all comments included in the translation unit,
194/// sorted in order of appearance in the translation unit.
195class RawCommentList {
196public:
197 RawCommentList(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
198
199 void addComment(const RawComment &RC, const CommentOptions &CommentOpts,
200 llvm::BumpPtrAllocator &Allocator);
201
202 ArrayRef<RawComment *> getComments() const {
203 return Comments;
204 }
205
206private:
207 SourceManager &SourceMgr;
208 std::vector<RawComment *> Comments;
209
210 void addDeserializedComments(ArrayRef<RawComment *> DeserializedComments);
211
212 friend class ASTReader;
213};
214
215} // end namespace clang
216
217#endif
218

Warning: That file was not part of the compilation database. It may have many parsing errors.