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