blob: 7db9175c174a500331996e5de7bf2027e0903564 [file] [log] [blame]
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001//===--- RawCommentList.cpp - 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#include "clang/Comments/RawCommentList.h"
11#include "clang/AST/ASTContext.h"
12#include "llvm/ADT/STLExtras.h"
13
14using namespace clang;
15
16namespace {
17/// Get comment kind and bool describing if it is a trailing comment.
18std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment) {
19 if (Comment.size() < 3 || Comment[0] != '/')
20 return std::make_pair(RawComment::CK_Invalid, false);
21
22 RawComment::CommentKind K;
23 if (Comment[1] == '/') {
24 if (Comment.size() < 3)
25 return std::make_pair(RawComment::CK_OrdinaryBCPL, false);
26
27 if (Comment[2] == '/')
28 K = RawComment::CK_BCPLSlash;
29 else if (Comment[2] == '!')
30 K = RawComment::CK_BCPLExcl;
31 else
32 return std::make_pair(RawComment::CK_OrdinaryBCPL, false);
33 } else {
34 assert(Comment.size() >= 4);
35
36 // Comment lexer does not understand escapes in comment markers, so pretend
37 // that this is not a comment.
38 if (Comment[1] != '*' ||
39 Comment[Comment.size() - 2] != '*' ||
40 Comment[Comment.size() - 1] != '/')
41 return std::make_pair(RawComment::CK_Invalid, false);
42
43 if (Comment[2] == '*')
44 K = RawComment::CK_JavaDoc;
45 else if (Comment[2] == '!')
46 K = RawComment::CK_Qt;
47 else
48 return std::make_pair(RawComment::CK_OrdinaryC, false);
49 }
50 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<');
51 return std::make_pair(K, TrailingComment);
52}
53
54bool mergedCommentIsTrailingComment(StringRef Comment) {
55 return (Comment.size() > 3) && (Comment[3] == '<');
56}
57} // unnamed namespace
58
59RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
60 bool Merged) :
61 Range(SR), RawTextValid(false), IsAlmostTrailingComment(false),
62 BeginLineValid(false), EndLineValid(false) {
63 // Extract raw comment text, if possible.
64 if (getRawText(SourceMgr).empty()) {
65 Kind = CK_Invalid;
66 return;
67 }
68
69 if (!Merged) {
70 // Guess comment kind.
71 std::pair<CommentKind, bool> K = getCommentKind(RawText);
72 Kind = K.first;
73 IsTrailingComment = K.second;
74
75 IsAlmostTrailingComment = RawText.startswith("//<") ||
76 RawText.startswith("/*<");
77 } else {
78 Kind = CK_Merged;
79 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
80 }
81}
82
83unsigned RawComment::getBeginLine(const SourceManager &SM) const {
84 if (BeginLineValid)
85 return BeginLine;
86
87 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getBegin());
88 BeginLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
89 BeginLineValid = true;
90 return BeginLine;
91}
92
93unsigned RawComment::getEndLine(const SourceManager &SM) const {
94 if (EndLineValid)
95 return EndLine;
96
97 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getEnd());
98 EndLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
99 EndLineValid = true;
100 return EndLine;
101}
102
103StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
104 FileID BeginFileID;
105 FileID EndFileID;
106 unsigned BeginOffset;
107 unsigned EndOffset;
108
109 llvm::tie(BeginFileID, BeginOffset) =
110 SourceMgr.getDecomposedLoc(Range.getBegin());
111 llvm::tie(EndFileID, EndOffset) =
112 SourceMgr.getDecomposedLoc(Range.getEnd());
113
114 const unsigned Length = EndOffset - BeginOffset;
115 if (Length < 2)
116 return StringRef();
117
118 // The comment can't begin in one file and end in another.
119 assert(BeginFileID == EndFileID);
120
121 bool Invalid = false;
122 const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
123 &Invalid).data();
124 if (Invalid)
125 return StringRef();
126
127 return StringRef(BufferStart + BeginOffset, Length);
128}
129
130namespace {
131bool containsOnlyWhitespace(StringRef Str) {
132 return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
133}
134
135bool onlyWhitespaceBetweenComments(SourceManager &SM,
136 const RawComment &C1, const RawComment &C2) {
137 std::pair<FileID, unsigned> C1EndLocInfo = SM.getDecomposedLoc(
138 C1.getSourceRange().getEnd());
139 std::pair<FileID, unsigned> C2BeginLocInfo = SM.getDecomposedLoc(
140 C2.getSourceRange().getBegin());
141
142 // Question does not make sense if comments are located in different files.
143 if (C1EndLocInfo.first != C2BeginLocInfo.first)
144 return false;
145
146 bool Invalid = false;
147 const char *Buffer = SM.getBufferData(C1EndLocInfo.first, &Invalid).data();
148 if (Invalid)
149 return false;
150
151 StringRef TextBetweenComments(Buffer + C1EndLocInfo.second,
152 C2BeginLocInfo.second - C1EndLocInfo.second);
153
154 return containsOnlyWhitespace(TextBetweenComments);
155}
156} // unnamed namespace
157
158void RawCommentList::addComment(const RawComment &RC, ASTContext &Context) {
159 if (RC.isInvalid())
160 return;
161
162 assert((Comments.empty() ||
163 SourceMgr.isBeforeInTranslationUnit(
164 Comments[0].getSourceRange().getEnd(),
165 RC.getSourceRange().getBegin())) &&
166 "comments are not coming in source order");
167
168 if (OnlyWhitespaceSeen) {
169 if (!onlyWhitespaceBetweenComments(SourceMgr, LastComment, RC))
170 OnlyWhitespaceSeen = false;
171 }
172
173 LastComment = RC;
174
175 // Ordinary comments are not interesting for us.
176 if (RC.isOrdinary())
177 return;
178
179 // If this is the first Doxygen comment, save it (because there isn't
180 // anything to merge it with).
181 if (Comments.empty()) {
182 Comments.push_back(RC);
183 OnlyWhitespaceSeen = true;
184 return;
185 }
186
187 const RawComment &C1 = Comments.back();
188 const RawComment &C2 = RC;
189
190 // Merge comments only if there is only whitespace between them.
191 // Can't merge trailing and non-trailing comments.
192 // Merge trailing comments if they are on same or consecutive lines.
193 if (OnlyWhitespaceSeen &&
194 (C1.isTrailingComment() == C2.isTrailingComment()) &&
195 (!C1.isTrailingComment() ||
196 C1.getEndLine(SourceMgr) + 1 >= C2.getBeginLine(SourceMgr))) {
197 SourceRange MergedRange(C1.getSourceRange().getBegin(),
198 C2.getSourceRange().getEnd());
199 RawComment Merged(SourceMgr, MergedRange, true);
200 Comments.pop_back();
201 Comments.push_back(Merged);
202 } else
203 Comments.push_back(RC);
204
205 OnlyWhitespaceSeen = true;
206}
207