Coverage Report

Created: 2021-01-19 06:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/RawCommentList.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- RawCommentList.cpp - Processing raw comments -----------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/AST/RawCommentList.h"
10
#include "clang/AST/ASTContext.h"
11
#include "clang/AST/Comment.h"
12
#include "clang/AST/CommentBriefParser.h"
13
#include "clang/AST/CommentCommandTraits.h"
14
#include "clang/AST/CommentLexer.h"
15
#include "clang/AST/CommentParser.h"
16
#include "clang/AST/CommentSema.h"
17
#include "clang/Basic/CharInfo.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/Support/Allocator.h"
20
21
using namespace clang;
22
23
namespace {
24
/// Get comment kind and bool describing if it is a trailing comment.
25
std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment,
26
24.6M
                                                        bool ParseAllComments) {
27
24.6M
  const size_t MinCommentLength = ParseAllComments ? 
2246
: 3;
28
24.6M
  if ((Comment.size() < MinCommentLength) || 
Comment[0] != '/'24.5M
)
29
38.4k
    return std::make_pair(RawComment::RCK_Invalid, false);
30
31
24.5M
  RawComment::CommentKind K;
32
24.5M
  if (Comment[1] == '/') {
33
3.09M
    if (Comment.size() < 3)
34
2
      return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
35
36
3.09M
    if (Comment[2] == '/')
37
214k
      K = RawComment::RCK_BCPLSlash;
38
2.88M
    else if (Comment[2] == '!')
39
31
      K = RawComment::RCK_BCPLExcl;
40
2.88M
    else
41
2.88M
      return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
42
21.4M
  } else {
43
21.4M
    assert(Comment.size() >= 4);
44
45
    // Comment lexer does not understand escapes in comment markers, so pretend
46
    // that this is not a comment.
47
21.4M
    if (Comment[1] != '*' ||
48
21.4M
        Comment[Comment.size() - 2] != '*' ||
49
21.4M
        Comment[Comment.size() - 1] != '/')
50
11
      return std::make_pair(RawComment::RCK_Invalid, false);
51
52
21.4M
    if (Comment[2] == '*')
53
877
      K = RawComment::RCK_JavaDoc;
54
21.4M
    else if (Comment[2] == '!')
55
93
      K = RawComment::RCK_Qt;
56
21.4M
    else
57
21.4M
      return std::make_pair(RawComment::RCK_OrdinaryC, false);
58
215k
  }
59
215k
  const bool TrailingComment = (Comment.size() > 3) && 
(Comment[3] == '<')208k
;
60
215k
  return std::make_pair(K, TrailingComment);
61
215k
}
62
63
96.3k
bool mergedCommentIsTrailingComment(StringRef Comment) {
64
96.3k
  return (Comment.size() > 3) && (Comment[3] == '<');
65
96.3k
}
66
67
/// Returns true if R1 and R2 both have valid locations that start on the same
68
/// column.
69
bool commentsStartOnSameColumn(const SourceManager &SM, const RawComment &R1,
70
8
                               const RawComment &R2) {
71
8
  SourceLocation L1 = R1.getBeginLoc();
72
8
  SourceLocation L2 = R2.getBeginLoc();
73
8
  bool Invalid = false;
74
8
  unsigned C1 = SM.getPresumedColumnNumber(L1, &Invalid);
75
8
  if (!Invalid) {
76
8
    unsigned C2 = SM.getPresumedColumnNumber(L2, &Invalid);
77
8
    return !Invalid && (C1 == C2);
78
8
  }
79
0
  return false;
80
0
}
81
} // unnamed namespace
82
83
/// Determines whether there is only whitespace in `Buffer` between `P`
84
/// and the previous line.
85
/// \param Buffer The buffer to search in.
86
/// \param P The offset from the beginning of `Buffer` to start from.
87
/// \return true if all of the characters in `Buffer` ranging from the closest
88
/// line-ending character before `P` (or the beginning of `Buffer`) to `P - 1`
89
/// are whitespace.
90
201
static bool onlyWhitespaceOnLineBefore(const char *Buffer, unsigned P) {
91
  // Search backwards until we see linefeed or carriage return.
92
393
  for (unsigned I = P; I != 0; 
--I192
) {
93
393
    char C = Buffer[I - 1];
94
393
    if (isVerticalWhitespace(C))
95
177
      return true;
96
216
    if (!isHorizontalWhitespace(C))
97
24
      return false;
98
216
  }
99
  // We hit the beginning of the buffer.
100
0
  return true;
101
201
}
102
103
/// Returns whether `K` is an ordinary comment kind.
104
616
static bool isOrdinaryKind(RawComment::CommentKind K) {
105
616
  return (K == RawComment::RCK_OrdinaryBCPL) ||
106
412
         (K == RawComment::RCK_OrdinaryC);
107
616
}
108
109
RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
110
                       const CommentOptions &CommentOpts, bool Merged) :
111
    Range(SR), RawTextValid(false), BriefTextValid(false),
112
    IsAttached(false), IsTrailingComment(false),
113
24.6M
    IsAlmostTrailingComment(false) {
114
  // Extract raw comment text, if possible.
115
24.6M
  if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
116
2
    Kind = RCK_Invalid;
117
2
    return;
118
2
  }
119
120
  // Guess comment kind.
121
24.6M
  std::pair<CommentKind, bool> K =
122
24.6M
      getCommentKind(RawText, CommentOpts.ParseAllComments);
123
124
  // Guess whether an ordinary comment is trailing.
125
24.6M
  if (CommentOpts.ParseAllComments && 
isOrdinaryKind(K.first)246
) {
126
212
    FileID BeginFileID;
127
212
    unsigned BeginOffset;
128
212
    std::tie(BeginFileID, BeginOffset) =
129
212
        SourceMgr.getDecomposedLoc(Range.getBegin());
130
212
    if (BeginOffset != 0) {
131
201
      bool Invalid = false;
132
201
      const char *Buffer =
133
201
          SourceMgr.getBufferData(BeginFileID, &Invalid).data();
134
201
      IsTrailingComment |=
135
201
          (!Invalid && !onlyWhitespaceOnLineBefore(Buffer, BeginOffset));
136
201
    }
137
212
  }
138
139
24.6M
  if (!Merged) {
140
24.5M
    Kind = K.first;
141
24.5M
    IsTrailingComment |= K.second;
142
143
24.5M
    IsAlmostTrailingComment = RawText.startswith("//<") ||
144
24.5M
                                 RawText.startswith("/*<");
145
96.3k
  } else {
146
96.3k
    Kind = RCK_Merged;
147
96.3k
    IsTrailingComment =
148
96.3k
        IsTrailingComment || 
mergedCommentIsTrailingComment(RawText)96.3k
;
149
96.3k
  }
150
24.6M
}
151
152
24.6M
StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
153
24.6M
  FileID BeginFileID;
154
24.6M
  FileID EndFileID;
155
24.6M
  unsigned BeginOffset;
156
24.6M
  unsigned EndOffset;
157
158
24.6M
  std::tie(BeginFileID, BeginOffset) =
159
24.6M
      SourceMgr.getDecomposedLoc(Range.getBegin());
160
24.6M
  std::tie(EndFileID, EndOffset) = SourceMgr.getDecomposedLoc(Range.getEnd());
161
162
24.6M
  const unsigned Length = EndOffset - BeginOffset;
163
24.6M
  if (Length < 2)
164
2
    return StringRef();
165
166
  // The comment can't begin in one file and end in another.
167
24.6M
  assert(BeginFileID == EndFileID);
168
169
24.6M
  bool Invalid = false;
170
24.6M
  const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
171
24.6M
                                                    &Invalid).data();
172
24.6M
  if (Invalid)
173
0
    return StringRef();
174
175
24.6M
  return StringRef(BufferStart + BeginOffset, Length);
176
24.6M
}
177
178
891
const char *RawComment::extractBriefText(const ASTContext &Context) const {
179
  // Lazily initialize RawText using the accessor before using it.
180
891
  (void)getRawText(Context.getSourceManager());
181
182
  // Since we will be copying the resulting text, all allocations made during
183
  // parsing are garbage after resulting string is formed.  Thus we can use
184
  // a separate allocator for all temporary stuff.
185
891
  llvm::BumpPtrAllocator Allocator;
186
187
891
  comments::Lexer L(Allocator, Context.getDiagnostics(),
188
891
                    Context.getCommentCommandTraits(),
189
891
                    Range.getBegin(),
190
891
                    RawText.begin(), RawText.end());
191
891
  comments::BriefParser P(L, Context.getCommentCommandTraits());
192
193
891
  const std::string Result = P.Parse();
194
891
  const unsigned BriefTextLength = Result.size();
195
891
  char *BriefTextPtr = new (Context) char[BriefTextLength + 1];
196
891
  memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
197
891
  BriefText = BriefTextPtr;
198
891
  BriefTextValid = true;
199
200
891
  return BriefTextPtr;
201
891
}
202
203
comments::FullComment *RawComment::parse(const ASTContext &Context,
204
                                         const Preprocessor *PP,
205
1.60k
                                         const Decl *D) const {
206
  // Lazily initialize RawText using the accessor before using it.
207
1.60k
  (void)getRawText(Context.getSourceManager());
208
209
1.60k
  comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(),
210
1.60k
                    Context.getCommentCommandTraits(),
211
1.60k
                    getSourceRange().getBegin(),
212
1.60k
                    RawText.begin(), RawText.end());
213
1.60k
  comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
214
1.60k
                   Context.getDiagnostics(),
215
1.60k
                   Context.getCommentCommandTraits(),
216
1.60k
                   PP);
217
1.60k
  S.setDecl(D);
218
1.60k
  comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
219
1.60k
                     Context.getDiagnostics(),
220
1.60k
                     Context.getCommentCommandTraits());
221
222
1.60k
  return P.parseFullComment();
223
1.60k
}
224
225
static bool onlyWhitespaceBetween(SourceManager &SM,
226
                                  SourceLocation Loc1, SourceLocation Loc2,
227
116k
                                  unsigned MaxNewlinesAllowed) {
228
116k
  std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1);
229
116k
  std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2);
230
231
  // Question does not make sense if locations are in different files.
232
116k
  if (Loc1Info.first != Loc2Info.first)
233
0
    return false;
234
235
116k
  bool Invalid = false;
236
116k
  const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data();
237
116k
  if (Invalid)
238
0
    return false;
239
240
116k
  unsigned NumNewlines = 0;
241
116k
  assert(Loc1Info.second <= Loc2Info.second && "Loc1 after Loc2!");
242
  // Look for non-whitespace characters and remember any newlines seen.
243
324k
  for (unsigned I = Loc1Info.second; I != Loc2Info.second; 
++I207k
) {
244
227k
    switch (Buffer[I]) {
245
19.3k
    default:
246
19.3k
      return false;
247
91.6k
    case ' ':
248
91.6k
    case '\t':
249
91.6k
    case '\f':
250
91.6k
    case '\v':
251
91.6k
      break;
252
0
    case '\r':
253
116k
    case '\n':
254
116k
      ++NumNewlines;
255
256
      // Check if we have found more than the maximum allowed number of
257
      // newlines.
258
116k
      if (NumNewlines > MaxNewlinesAllowed)
259
579
        return false;
260
261
      // Collapse \r\n and \n\r into a single newline.
262
116k
      if (I + 1 != Loc2Info.second &&
263
47.7k
          (Buffer[I + 1] == '\n' || 
Buffer[I + 1] == '\r'47.1k
) &&
264
579
          Buffer[I] != Buffer[I + 1])
265
0
        ++I;
266
116k
      break;
267
227k
    }
268
227k
  }
269
270
96.3k
  return true;
271
116k
}
272
273
void RawCommentList::addComment(const RawComment &RC,
274
                                const CommentOptions &CommentOpts,
275
24.5M
                                llvm::BumpPtrAllocator &Allocator) {
276
24.5M
  if (RC.isInvalid())
277
38.4k
    return;
278
279
  // Ordinary comments are not interesting for us.
280
24.4M
  if (RC.isOrdinary() && 
!CommentOpts.ParseAllComments24.3M
)
281
24.3M
    return;
282
283
119k
  std::pair<FileID, unsigned> Loc =
284
119k
      SourceMgr.getDecomposedLoc(RC.getBeginLoc());
285
286
119k
  const FileID CommentFile = Loc.first;
287
119k
  const unsigned CommentOffset = Loc.second;
288
289
  // If this is the first Doxygen comment, save it (because there isn't
290
  // anything to merge it with).
291
119k
  if (OrderedComments[CommentFile].empty()) {
292
2.24k
    OrderedComments[CommentFile][CommentOffset] =
293
2.24k
        new (Allocator) RawComment(RC);
294
2.24k
    return;
295
2.24k
  }
296
297
116k
  const RawComment &C1 = *OrderedComments[CommentFile].rbegin()->second;
298
116k
  const RawComment &C2 = RC;
299
300
  // Merge comments only if there is only whitespace between them.
301
  // Can't merge trailing and non-trailing comments unless the second is
302
  // non-trailing ordinary in the same column, as in the case:
303
  //   int x; // documents x
304
  //          // more text
305
  // versus:
306
  //   int x; // documents x
307
  //   int y; // documents y
308
  // or:
309
  //   int x; // documents x
310
  //   // documents y
311
  //   int y;
312
  // Merge comments if they are on same or consecutive lines.
313
116k
  if ((C1.isTrailingComment() == C2.isTrailingComment() ||
314
744
       (C1.isTrailingComment() && 
!C2.isTrailingComment()370
&&
315
370
        isOrdinaryKind(C2.getKind()) &&
316
8
        commentsStartOnSameColumn(SourceMgr, C1, C2))) &&
317
116k
      onlyWhitespaceBetween(SourceMgr, C1.getEndLoc(), C2.getBeginLoc(),
318
96.3k
                            /*MaxNewlinesAllowed=*/1)) {
319
96.3k
    SourceRange MergedRange(C1.getBeginLoc(), C2.getEndLoc());
320
96.3k
    *OrderedComments[CommentFile].rbegin()->second =
321
96.3k
        RawComment(SourceMgr, MergedRange, CommentOpts, true);
322
20.6k
  } else {
323
20.6k
    OrderedComments[CommentFile][CommentOffset] =
324
20.6k
        new (Allocator) RawComment(RC);
325
20.6k
  }
326
116k
}
327
328
const std::map<unsigned, RawComment *> *
329
2.58k
RawCommentList::getCommentsInFile(FileID File) const {
330
2.58k
  auto CommentsInFile = OrderedComments.find(File);
331
2.58k
  if (CommentsInFile == OrderedComments.end())
332
4
    return nullptr;
333
334
2.58k
  return &CommentsInFile->second;
335
2.58k
}
336
337
24.7k
bool RawCommentList::empty() const { return OrderedComments.empty(); }
338
339
unsigned RawCommentList::getCommentBeginLine(RawComment *C, FileID File,
340
134
                                             unsigned Offset) const {
341
134
  auto Cached = CommentBeginLine.find(C);
342
134
  if (Cached != CommentBeginLine.end())
343
18
    return Cached->second;
344
116
  const unsigned Line = SourceMgr.getLineNumber(File, Offset);
345
116
  CommentBeginLine[C] = Line;
346
116
  return Line;
347
116
}
348
349
2.14k
unsigned RawCommentList::getCommentEndOffset(RawComment *C) const {
350
2.14k
  auto Cached = CommentEndOffset.find(C);
351
2.14k
  if (Cached != CommentEndOffset.end())
352
623
    return Cached->second;
353
1.52k
  const unsigned Offset =
354
1.52k
      SourceMgr.getDecomposedLoc(C->getSourceRange().getEnd()).second;
355
1.52k
  CommentEndOffset[C] = Offset;
356
1.52k
  return Offset;
357
1.52k
}
358
359
std::string RawComment::getFormattedText(const SourceManager &SourceMgr,
360
6
                                         DiagnosticsEngine &Diags) const {
361
6
  llvm::StringRef CommentText = getRawText(SourceMgr);
362
6
  if (CommentText.empty())
363
0
    return "";
364
365
6
  llvm::BumpPtrAllocator Allocator;
366
  // We do not parse any commands, so CommentOptions are ignored by
367
  // comments::Lexer. Therefore, we just use default-constructed options.
368
6
  CommentOptions DefOpts;
369
6
  comments::CommandTraits EmptyTraits(Allocator, DefOpts);
370
6
  comments::Lexer L(Allocator, Diags, EmptyTraits, getSourceRange().getBegin(),
371
6
                    CommentText.begin(), CommentText.end(),
372
6
                    /*ParseCommands=*/false);
373
374
6
  std::string Result;
375
  // A column number of the first non-whitespace token in the comment text.
376
  // We skip whitespace up to this column, but keep the whitespace after this
377
  // column. IndentColumn is calculated when lexing the first line and reused
378
  // for the rest of lines.
379
6
  unsigned IndentColumn = 0;
380
381
  // Processes one line of the comment and adds it to the result.
382
  // Handles skipping the indent at the start of the line.
383
  // Returns false when eof is reached and true otherwise.
384
36
  auto LexLine = [&](bool IsFirstLine) -> bool {
385
36
    comments::Token Tok;
386
    // Lex the first token on the line. We handle it separately, because we to
387
    // fix up its indentation.
388
36
    L.lex(Tok);
389
36
    if (Tok.is(comments::tok::eof))
390
6
      return false;
391
30
    if (Tok.is(comments::tok::newline)) {
392
2
      Result += "\n";
393
2
      return true;
394
2
    }
395
28
    llvm::StringRef TokText = L.getSpelling(Tok, SourceMgr);
396
28
    bool LocInvalid = false;
397
28
    unsigned TokColumn =
398
28
        SourceMgr.getSpellingColumnNumber(Tok.getLocation(), &LocInvalid);
399
28
    assert(!LocInvalid && "getFormattedText for invalid location");
400
401
    // Amount of leading whitespace in TokText.
402
28
    size_t WhitespaceLen = TokText.find_first_not_of(" \t");
403
28
    if (WhitespaceLen == StringRef::npos)
404
0
      WhitespaceLen = TokText.size();
405
    // Remember the amount of whitespace we skipped in the first line to remove
406
    // indent up to that column in the following lines.
407
28
    if (IsFirstLine)
408
6
      IndentColumn = TokColumn + WhitespaceLen;
409
410
    // Amount of leading whitespace we actually want to skip.
411
    // For the first line we skip all the whitespace.
412
    // For the rest of the lines, we skip whitespace up to IndentColumn.
413
28
    unsigned SkipLen =
414
28
        IsFirstLine
415
6
            ? WhitespaceLen
416
22
            : std::min<size_t>(
417
22
                  WhitespaceLen,
418
22
                  std::max<int>(static_cast<int>(IndentColumn) - TokColumn, 0));
419
28
    llvm::StringRef Trimmed = TokText.drop_front(SkipLen);
420
28
    Result += Trimmed;
421
    // Lex all tokens in the rest of the line.
422
28
    for (L.lex(Tok); Tok.isNot(comments::tok::eof); 
L.lex(Tok)0
) {
423
28
      if (Tok.is(comments::tok::newline)) {
424
28
        Result += "\n";
425
28
        return true;
426
28
      }
427
0
      Result += L.getSpelling(Tok, SourceMgr);
428
0
    }
429
    // We've reached the end of file token.
430
0
    return false;
431
28
  };
432
433
6
  auto DropTrailingNewLines = [](std::string &Str) {
434
14
    while (!Str.empty() && Str.back() == '\n')
435
8
      Str.pop_back();
436
6
  };
437
438
  // Process first line separately to remember indent for the following lines.
439
6
  if (!LexLine(/*IsFirstLine=*/true)) {
440
0
    DropTrailingNewLines(Result);
441
0
    return Result;
442
0
  }
443
  // Process the rest of the lines.
444
30
  
while (6
LexLine(/*IsFirstLine=*/false))
445
24
    ;
446
6
  DropTrailingNewLines(Result);
447
6
  return Result;
448
6
}