File indexing completed on 2026-05-10 08:36:40
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_CLANG_AST_RAWCOMMENTLIST_H
0010 #define LLVM_CLANG_AST_RAWCOMMENTLIST_H
0011
0012 #include "clang/Basic/CommentOptions.h"
0013 #include "clang/Basic/SourceLocation.h"
0014 #include "llvm/ADT/ArrayRef.h"
0015 #include "llvm/ADT/DenseMap.h"
0016 #include "llvm/Support/Allocator.h"
0017 #include <map>
0018
0019 namespace clang {
0020
0021 class ASTContext;
0022 class ASTReader;
0023 class Decl;
0024 class DiagnosticsEngine;
0025 class Preprocessor;
0026 class SourceManager;
0027
0028 namespace comments {
0029 class FullComment;
0030 }
0031
0032 class RawComment {
0033 public:
0034 enum CommentKind {
0035 RCK_Invalid,
0036 RCK_OrdinaryBCPL,
0037 RCK_OrdinaryC,
0038 RCK_BCPLSlash,
0039 RCK_BCPLExcl,
0040 RCK_JavaDoc,
0041 RCK_Qt,
0042 RCK_Merged
0043 };
0044
0045 RawComment() : Kind(RCK_Invalid), IsAlmostTrailingComment(false) { }
0046
0047 RawComment(const SourceManager &SourceMgr, SourceRange SR,
0048 const CommentOptions &CommentOpts, bool Merged);
0049
0050 CommentKind getKind() const LLVM_READONLY {
0051 return (CommentKind) Kind;
0052 }
0053
0054 bool isInvalid() const LLVM_READONLY {
0055 return Kind == RCK_Invalid;
0056 }
0057
0058 bool isMerged() const LLVM_READONLY {
0059 return Kind == RCK_Merged;
0060 }
0061
0062
0063 bool isAttached() const LLVM_READONLY {
0064 return IsAttached;
0065 }
0066
0067 void setAttached() {
0068 IsAttached = true;
0069 }
0070
0071
0072
0073
0074
0075
0076 bool isTrailingComment() const LLVM_READONLY {
0077 return IsTrailingComment;
0078 }
0079
0080
0081
0082
0083 bool isAlmostTrailingComment() const LLVM_READONLY {
0084 return IsAlmostTrailingComment;
0085 }
0086
0087
0088 bool isOrdinary() const LLVM_READONLY {
0089 return ((Kind == RCK_OrdinaryBCPL) || (Kind == RCK_OrdinaryC));
0090 }
0091
0092
0093 bool isDocumentation() const LLVM_READONLY {
0094 return !isInvalid() && !isOrdinary();
0095 }
0096
0097
0098 StringRef getRawText(const SourceManager &SourceMgr) const {
0099 if (RawTextValid)
0100 return RawText;
0101
0102 RawText = getRawTextSlow(SourceMgr);
0103 RawTextValid = true;
0104 return RawText;
0105 }
0106
0107 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
0108 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
0109 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
0110
0111 const char *getBriefText(const ASTContext &Context) const {
0112 if (BriefTextValid)
0113 return BriefText;
0114
0115 return extractBriefText(Context);
0116 }
0117
0118 bool hasUnsupportedSplice(const SourceManager &SourceMgr) const {
0119 if (!isInvalid())
0120 return false;
0121 StringRef Text = getRawText(SourceMgr);
0122 if (Text.size() < 6 || Text[0] != '/')
0123 return false;
0124 if (Text[1] == '*')
0125 return Text[Text.size() - 1] != '/' || Text[Text.size() - 2] != '*';
0126 return Text[1] != '/';
0127 }
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 std::string getFormattedText(const SourceManager &SourceMgr,
0151 DiagnosticsEngine &Diags) const;
0152
0153 struct CommentLine {
0154 std::string Text;
0155 PresumedLoc Begin;
0156 PresumedLoc End;
0157
0158 CommentLine(StringRef Text, PresumedLoc Begin, PresumedLoc End)
0159 : Text(Text), Begin(Begin), End(End) {}
0160 };
0161
0162
0163
0164
0165 std::vector<CommentLine> getFormattedLines(const SourceManager &SourceMgr,
0166 DiagnosticsEngine &Diags) const;
0167
0168
0169 comments::FullComment *parse(const ASTContext &Context,
0170 const Preprocessor *PP, const Decl *D) const;
0171
0172 private:
0173 SourceRange Range;
0174
0175 mutable StringRef RawText;
0176 mutable const char *BriefText = nullptr;
0177
0178 LLVM_PREFERRED_TYPE(bool)
0179 mutable unsigned RawTextValid : 1;
0180 LLVM_PREFERRED_TYPE(bool)
0181 mutable unsigned BriefTextValid : 1;
0182
0183 LLVM_PREFERRED_TYPE(CommentKind)
0184 unsigned Kind : 3;
0185
0186
0187 LLVM_PREFERRED_TYPE(bool)
0188 unsigned IsAttached : 1;
0189
0190 LLVM_PREFERRED_TYPE(bool)
0191 unsigned IsTrailingComment : 1;
0192 LLVM_PREFERRED_TYPE(bool)
0193 unsigned IsAlmostTrailingComment : 1;
0194
0195
0196 RawComment(SourceRange SR, CommentKind K, bool IsTrailingComment,
0197 bool IsAlmostTrailingComment) :
0198 Range(SR), RawTextValid(false), BriefTextValid(false), Kind(K),
0199 IsAttached(false), IsTrailingComment(IsTrailingComment),
0200 IsAlmostTrailingComment(IsAlmostTrailingComment)
0201 { }
0202
0203 StringRef getRawTextSlow(const SourceManager &SourceMgr) const;
0204
0205 const char *extractBriefText(const ASTContext &Context) const;
0206
0207 friend class ASTReader;
0208 };
0209
0210
0211
0212 class RawCommentList {
0213 public:
0214 RawCommentList(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
0215
0216 void addComment(const RawComment &RC, const CommentOptions &CommentOpts,
0217 llvm::BumpPtrAllocator &Allocator);
0218
0219
0220
0221 const std::map<unsigned, RawComment *> *getCommentsInFile(FileID File) const;
0222
0223 bool empty() const;
0224
0225 unsigned getCommentBeginLine(RawComment *C, FileID File,
0226 unsigned Offset) const;
0227 unsigned getCommentEndOffset(RawComment *C) const;
0228
0229 private:
0230 SourceManager &SourceMgr;
0231
0232 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> OrderedComments;
0233 mutable llvm::DenseMap<RawComment *, unsigned> CommentBeginLine;
0234 mutable llvm::DenseMap<RawComment *, unsigned> CommentEndOffset;
0235
0236 friend class ASTReader;
0237 friend class ASTWriter;
0238 };
0239
0240 }
0241
0242 #endif