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