Coverage Report

Created: 2018-09-25 23:22

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