Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Rewrite/Rewriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Rewriter.cpp - Code rewriting interface ----------------------------===//
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
//  This file defines the Rewriter class, which is used for code
10
//  transformations.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Rewrite/Core/Rewriter.h"
15
#include "clang/Basic/Diagnostic.h"
16
#include "clang/Basic/DiagnosticIDs.h"
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Basic/SourceLocation.h"
19
#include "clang/Basic/SourceManager.h"
20
#include "clang/Lex/Lexer.h"
21
#include "clang/Rewrite/Core/RewriteBuffer.h"
22
#include "clang/Rewrite/Core/RewriteRope.h"
23
#include "llvm/ADT/SmallString.h"
24
#include "llvm/ADT/SmallVector.h"
25
#include "llvm/ADT/StringRef.h"
26
#include "llvm/Support/FileSystem.h"
27
#include "llvm/Support/raw_ostream.h"
28
#include <cassert>
29
#include <iterator>
30
#include <map>
31
#include <memory>
32
#include <system_error>
33
#include <utility>
34
35
using namespace clang;
36
37
20.3k
raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
38
  // Walk RewriteRope chunks efficiently using MoveToNextPiece() instead of the
39
  // character iterator.
40
167k
  for (RopePieceBTreeIterator I = begin(), E = end(); I != E;
41
147k
       I.MoveToNextPiece())
42
147k
    os << I.piece();
43
20.3k
  return os;
44
20.3k
}
45
46
/// Return true if this character is non-new-line whitespace:
47
/// ' ', '\\t', '\\f', '\\v', '\\r'.
48
1.70k
static inline bool isWhitespaceExceptNL(unsigned char c) {
49
1.70k
  switch (c) {
50
1.19k
  case ' ':
51
1.19k
  case '\t':
52
1.19k
  case '\f':
53
1.19k
  case '\v':
54
1.19k
  case '\r':
55
1.19k
    return true;
56
504
  default:
57
504
    return false;
58
1.70k
  }
59
1.70k
}
60
61
void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
62
404
                               bool removeLineIfEmpty) {
63
  // Nothing to remove, exit early.
64
404
  if (Size == 0) 
return0
;
65
66
404
  unsigned RealOffset = getMappedOffset(OrigOffset, true);
67
404
  assert(RealOffset+Size <= Buffer.size() && "Invalid location");
68
69
  // Remove the dead characters.
70
0
  Buffer.erase(RealOffset, Size);
71
72
  // Add a delta so that future changes are offset correctly.
73
404
  AddReplaceDelta(OrigOffset, -Size);
74
75
404
  if (removeLineIfEmpty) {
76
    // Find the line that the remove occurred and if it is completely empty
77
    // remove the line as well.
78
79
402
    iterator curLineStart = begin();
80
402
    unsigned curLineStartOffs = 0;
81
402
    iterator posI = begin();
82
350k
    for (unsigned i = 0; i != RealOffset; 
++i350k
) {
83
350k
      if (*posI == '\n') {
84
16.6k
        curLineStart = posI;
85
16.6k
        ++curLineStart;
86
16.6k
        curLineStartOffs = i + 1;
87
16.6k
      }
88
350k
      ++posI;
89
350k
    }
90
91
402
    unsigned lineSize = 0;
92
402
    posI = curLineStart;
93
1.31k
    while (posI != end() && isWhitespaceExceptNL(*posI)) {
94
910
      ++posI;
95
910
      ++lineSize;
96
910
    }
97
402
    if (posI != end() && *posI == '\n') {
98
74
      Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
99
      // FIXME: Here, the offset of the start of the line is supposed to be
100
      // expressed in terms of the original input not the "real" rewrite
101
      // buffer.  How do we compute that reliably?  It might be tempting to use
102
      // curLineStartOffs + OrigOffset - RealOffset, but that assumes the
103
      // difference between the original and real offset is the same at the
104
      // removed text and at the start of the line, but that's not true if
105
      // edits were previously made earlier on the line.  This bug is also
106
      // documented by a FIXME on the definition of
107
      // clang::Rewriter::RewriteOptions::RemoveLineIfEmpty.  A reproducer for
108
      // the implementation below is the test RemoveLineIfEmpty in
109
      // clang/unittests/Rewrite/RewriteBufferTest.cpp.
110
74
      AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
111
74
    }
112
402
  }
113
404
}
114
115
void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
116
79.3k
                               bool InsertAfter) {
117
  // Nothing to insert, exit early.
118
79.3k
  if (Str.empty()) 
return5
;
119
120
79.3k
  unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
121
79.3k
  Buffer.insert(RealOffset, Str.begin(), Str.end());
122
123
  // Add a delta so that future changes are offset correctly.
124
79.3k
  AddInsertDelta(OrigOffset, Str.size());
125
79.3k
}
126
127
/// ReplaceText - This method replaces a range of characters in the input
128
/// buffer with a new string.  This is effectively a combined "remove+insert"
129
/// operation.
130
void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
131
71.9k
                                StringRef NewStr) {
132
71.9k
  unsigned RealOffset = getMappedOffset(OrigOffset, true);
133
71.9k
  Buffer.erase(RealOffset, OrigLength);
134
71.9k
  Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
135
71.9k
  if (OrigLength != NewStr.size())
136
53.5k
    AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
137
71.9k
}
138
139
//===----------------------------------------------------------------------===//
140
// Rewriter class
141
//===----------------------------------------------------------------------===//
142
143
/// getRangeSize - Return the size in bytes of the specified range if they
144
/// are in the same file.  If not, this returns -1.
145
int Rewriter::getRangeSize(const CharSourceRange &Range,
146
2.74k
                           RewriteOptions opts) const {
147
2.74k
  if (!isRewritable(Range.getBegin()) ||
148
2.74k
      !isRewritable(Range.getEnd())) 
return -10
;
149
150
2.74k
  FileID StartFileID, EndFileID;
151
2.74k
  unsigned StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
152
2.74k
  unsigned EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
153
154
2.74k
  if (StartFileID != EndFileID)
155
0
    return -1;
156
157
  // If edits have been made to this buffer, the delta between the range may
158
  // have changed.
159
2.74k
  std::map<FileID, RewriteBuffer>::const_iterator I =
160
2.74k
    RewriteBuffers.find(StartFileID);
161
2.74k
  if (I != RewriteBuffers.end()) {
162
2.60k
    const RewriteBuffer &RB = I->second;
163
2.60k
    EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
164
2.60k
    StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
165
2.60k
  }
166
167
  // Adjust the end offset to the end of the last token, instead of being the
168
  // start of the last token if this is a token range.
169
2.74k
  if (Range.isTokenRange())
170
676
    EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
171
172
2.74k
  return EndOff-StartOff;
173
2.74k
}
174
175
675
int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
176
675
  return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
177
675
}
178
179
/// getRewrittenText - Return the rewritten form of the text in the specified
180
/// range.  If the start or end of the range was unrewritable or if they are
181
/// in different buffers, this returns an empty string.
182
///
183
/// Note that this method is not particularly efficient.
184
183
std::string Rewriter::getRewrittenText(CharSourceRange Range) const {
185
183
  if (!isRewritable(Range.getBegin()) ||
186
183
      !isRewritable(Range.getEnd()))
187
0
    return {};
188
189
183
  FileID StartFileID, EndFileID;
190
183
  unsigned StartOff, EndOff;
191
183
  StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
192
183
  EndOff   = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
193
194
183
  if (StartFileID != EndFileID)
195
0
    return {}; // Start and end in different buffers.
196
197
  // If edits have been made to this buffer, the delta between the range may
198
  // have changed.
199
183
  std::map<FileID, RewriteBuffer>::const_iterator I =
200
183
    RewriteBuffers.find(StartFileID);
201
183
  if (I == RewriteBuffers.end()) {
202
    // If the buffer hasn't been rewritten, just return the text from the input.
203
29
    const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
204
205
    // Adjust the end offset to the end of the last token, instead of being the
206
    // start of the last token.
207
29
    if (Range.isTokenRange())
208
28
      EndOff +=
209
28
          Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
210
29
    return std::string(Ptr, Ptr+EndOff-StartOff);
211
29
  }
212
213
154
  const RewriteBuffer &RB = I->second;
214
154
  EndOff = RB.getMappedOffset(EndOff, true);
215
154
  StartOff = RB.getMappedOffset(StartOff);
216
217
  // Adjust the end offset to the end of the last token, instead of being the
218
  // start of the last token.
219
154
  if (Range.isTokenRange())
220
150
    EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
221
222
  // Advance the iterators to the right spot, yay for linear time algorithms.
223
154
  RewriteBuffer::iterator Start = RB.begin();
224
154
  std::advance(Start, StartOff);
225
154
  RewriteBuffer::iterator End = Start;
226
154
  assert(EndOff >= StartOff && "Invalid iteration distance");
227
0
  std::advance(End, EndOff-StartOff);
228
229
154
  return std::string(Start, End);
230
183
}
231
232
unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
233
78.4k
                                              FileID &FID) const {
234
78.4k
  assert(Loc.isValid() && "Invalid location");
235
0
  std::pair<FileID, unsigned> V = SourceMgr->getDecomposedLoc(Loc);
236
78.4k
  FID = V.first;
237
78.4k
  return V.second;
238
78.4k
}
239
240
/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
241
94.3k
RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
242
94.3k
  std::map<FileID, RewriteBuffer>::iterator I =
243
94.3k
    RewriteBuffers.lower_bound(FID);
244
94.3k
  if (I != RewriteBuffers.end() && 
I->first == FID73.4k
)
245
73.4k
    return I->second;
246
20.8k
  I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
247
248
20.8k
  StringRef MB = SourceMgr->getBufferData(FID);
249
20.8k
  I->second.Initialize(MB.begin(), MB.end());
250
251
20.8k
  return I->second;
252
94.3k
}
253
254
/// InsertText - Insert the specified string at the specified location in the
255
/// original buffer.
256
bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
257
3.66k
                          bool InsertAfter, bool indentNewLines) {
258
3.66k
  if (!isRewritable(Loc)) 
return true0
;
259
3.66k
  FileID FID;
260
3.66k
  unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
261
262
3.66k
  SmallString<128> indentedStr;
263
3.66k
  if (indentNewLines && 
Str.contains('\n')313
) {
264
12
    StringRef MB = SourceMgr->getBufferData(FID);
265
266
12
    unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
267
12
    const SrcMgr::ContentCache *Content =
268
12
        &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
269
12
    unsigned lineOffs = Content->SourceLineCache[lineNo];
270
271
    // Find the whitespace at the start of the line.
272
12
    StringRef indentSpace;
273
12
    {
274
12
      unsigned i = lineOffs;
275
22
      while (isWhitespaceExceptNL(MB[i]))
276
10
        ++i;
277
12
      indentSpace = MB.substr(lineOffs, i-lineOffs);
278
12
    }
279
280
12
    SmallVector<StringRef, 4> lines;
281
12
    Str.split(lines, "\n");
282
283
54
    for (unsigned i = 0, e = lines.size(); i != e; 
++i42
) {
284
42
      indentedStr += lines[i];
285
42
      if (i < e-1) {
286
30
        indentedStr += '\n';
287
30
        indentedStr += indentSpace;
288
30
      }
289
42
    }
290
12
    Str = indentedStr.str();
291
12
  }
292
293
3.66k
  getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
294
3.66k
  return false;
295
3.66k
}
296
297
0
bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
298
0
  if (!isRewritable(Loc)) return true;
299
0
  FileID FID;
300
0
  unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
301
0
  RewriteOptions rangeOpts;
302
0
  rangeOpts.IncludeInsertsAtBeginOfRange = false;
303
0
  StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
304
0
  getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
305
0
  return false;
306
0
}
307
308
/// RemoveText - Remove the specified text region.
309
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
310
403
                          RewriteOptions opts) {
311
403
  if (!isRewritable(Start)) 
return true0
;
312
403
  FileID FID;
313
403
  unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
314
403
  getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
315
403
  return false;
316
403
}
317
318
/// ReplaceText - This method replaces a range of characters in the input
319
/// buffer with a new string.  This is effectively a combined "remove/insert"
320
/// operation.
321
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
322
68.4k
                           StringRef NewStr) {
323
68.4k
  if (!isRewritable(Start)) 
return true0
;
324
68.4k
  FileID StartFileID;
325
68.4k
  unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
326
327
68.4k
  getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
328
68.4k
  return false;
329
68.4k
}
330
331
0
bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
332
0
  if (!isRewritable(range.getBegin())) return true;
333
0
  if (!isRewritable(range.getEnd())) return true;
334
0
  if (replacementRange.isInvalid()) return true;
335
0
  SourceLocation start = range.getBegin();
336
0
  unsigned origLength = getRangeSize(range);
337
0
  unsigned newLength = getRangeSize(replacementRange);
338
0
  FileID FID;
339
0
  unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
340
0
                                                FID);
341
0
  StringRef MB = SourceMgr->getBufferData(FID);
342
0
  return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
343
0
}
344
345
bool Rewriter::IncreaseIndentation(CharSourceRange range,
346
16
                                   SourceLocation parentIndent) {
347
16
  if (range.isInvalid()) 
return true0
;
348
16
  if (!isRewritable(range.getBegin())) 
return true0
;
349
16
  if (!isRewritable(range.getEnd())) 
return true0
;
350
16
  if (!isRewritable(parentIndent)) 
return true0
;
351
352
16
  FileID StartFileID, EndFileID, parentFileID;
353
16
  unsigned StartOff, EndOff, parentOff;
354
355
16
  StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
356
16
  EndOff   = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
357
16
  parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
358
359
16
  if (StartFileID != EndFileID || StartFileID != parentFileID)
360
0
    return true;
361
16
  if (StartOff > EndOff)
362
0
    return true;
363
364
16
  FileID FID = StartFileID;
365
16
  StringRef MB = SourceMgr->getBufferData(FID);
366
367
16
  unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
368
16
  unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
369
16
  unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
370
371
16
  const SrcMgr::ContentCache *Content =
372
16
      &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
373
374
  // Find where the lines start.
375
16
  unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
376
16
  unsigned startLineOffs = Content->SourceLineCache[startLineNo];
377
378
  // Find the whitespace at the start of each line.
379
16
  StringRef parentSpace, startSpace;
380
16
  {
381
16
    unsigned i = parentLineOffs;
382
26
    while (isWhitespaceExceptNL(MB[i]))
383
10
      ++i;
384
16
    parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
385
386
16
    i = startLineOffs;
387
76
    while (isWhitespaceExceptNL(MB[i]))
388
60
      ++i;
389
16
    startSpace = MB.substr(startLineOffs, i-startLineOffs);
390
16
  }
391
16
  if (parentSpace.size() >= startSpace.size())
392
0
    return true;
393
16
  if (!startSpace.startswith(parentSpace))
394
0
    return true;
395
396
16
  StringRef indent = startSpace.substr(parentSpace.size());
397
398
  // Indent the lines between start/end offsets.
399
16
  RewriteBuffer &RB = getEditBuffer(FID);
400
74
  for (unsigned lineNo = startLineNo; lineNo <= endLineNo; 
++lineNo58
) {
401
58
    unsigned offs = Content->SourceLineCache[lineNo];
402
58
    unsigned i = offs;
403
264
    while (isWhitespaceExceptNL(MB[i]))
404
206
      ++i;
405
58
    StringRef origIndent = MB.substr(offs, i-offs);
406
58
    if (origIndent.startswith(startSpace))
407
53
      RB.InsertText(offs, indent, /*InsertAfter=*/false);
408
58
  }
409
410
16
  return false;
411
16
}
412
413
namespace {
414
415
// A wrapper for a file stream that atomically overwrites the target.
416
//
417
// Creates a file output stream for a temporary file in the constructor,
418
// which is later accessible via getStream() if ok() return true.
419
// Flushes the stream and moves the temporary file to the target location
420
// in the destructor.
421
class AtomicallyMovedFile {
422
public:
423
  AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,
424
                      bool &AllWritten)
425
70
      : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
426
70
    TempFilename = Filename;
427
70
    TempFilename += "-%%%%%%%%";
428
70
    int FD;
429
70
    if (llvm::sys::fs::createUniqueFile(TempFilename, FD, TempFilename)) {
430
1
      AllWritten = false;
431
1
      Diagnostics.Report(clang::diag::err_unable_to_make_temp)
432
1
        << TempFilename;
433
69
    } else {
434
69
      FileStream.reset(new llvm::raw_fd_ostream(FD, /*shouldClose=*/true));
435
69
    }
436
70
  }
437
438
70
  ~AtomicallyMovedFile() {
439
70
    if (!ok()) 
return1
;
440
441
    // Close (will also flush) theFileStream.
442
69
    FileStream->close();
443
69
    if (std::error_code ec = llvm::sys::fs::rename(TempFilename, Filename)) {
444
0
      AllWritten = false;
445
0
      Diagnostics.Report(clang::diag::err_unable_to_rename_temp)
446
0
        << TempFilename << Filename << ec.message();
447
      // If the remove fails, there's not a lot we can do - this is already an
448
      // error.
449
0
      llvm::sys::fs::remove(TempFilename);
450
0
    }
451
69
  }
452
453
140
  bool ok() { return (bool)FileStream; }
454
69
  raw_ostream &getStream() { return *FileStream; }
455
456
private:
457
  DiagnosticsEngine &Diagnostics;
458
  StringRef Filename;
459
  SmallString<128> TempFilename;
460
  std::unique_ptr<llvm::raw_fd_ostream> FileStream;
461
  bool &AllWritten;
462
};
463
464
} // namespace
465
466
77
bool Rewriter::overwriteChangedFiles() {
467
77
  bool AllWritten = true;
468
147
  for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; 
++I70
) {
469
70
    const FileEntry *Entry =
470
70
        getSourceMgr().getFileEntryForID(I->first);
471
70
    AtomicallyMovedFile File(getSourceMgr().getDiagnostics(), Entry->getName(),
472
70
                             AllWritten);
473
70
    if (File.ok()) {
474
69
      I->second.write(File.getStream());
475
69
    }
476
70
  }
477
77
  return !AllWritten;
478
77
}