clang 17.0.0git
Rewriter.cpp
Go to the documentation of this file.
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
20#include "clang/Lex/Lexer.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
35using namespace clang;
36
37raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
38 // Walk RewriteRope chunks efficiently using MoveToNextPiece() instead of the
39 // character iterator.
40 for (RopePieceBTreeIterator I = begin(), E = end(); I != E;
42 os << I.piece();
43 return os;
44}
45
46/// Return true if this character is non-new-line whitespace:
47/// ' ', '\\t', '\\f', '\\v', '\\r'.
48static inline bool isWhitespaceExceptNL(unsigned char c) {
49 switch (c) {
50 case ' ':
51 case '\t':
52 case '\f':
53 case '\v':
54 case '\r':
55 return true;
56 default:
57 return false;
58 }
59}
60
61void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
62 bool removeLineIfEmpty) {
63 // Nothing to remove, exit early.
64 if (Size == 0) return;
65
66 unsigned RealOffset = getMappedOffset(OrigOffset, true);
67 assert(RealOffset+Size <= Buffer.size() && "Invalid location");
68
69 // Remove the dead characters.
70 Buffer.erase(RealOffset, Size);
71
72 // Add a delta so that future changes are offset correctly.
73 AddReplaceDelta(OrigOffset, -Size);
74
75 if (removeLineIfEmpty) {
76 // Find the line that the remove occurred and if it is completely empty
77 // remove the line as well.
78
79 iterator curLineStart = begin();
80 unsigned curLineStartOffs = 0;
81 iterator posI = begin();
82 for (unsigned i = 0; i != RealOffset; ++i) {
83 if (*posI == '\n') {
84 curLineStart = posI;
85 ++curLineStart;
86 curLineStartOffs = i + 1;
87 }
88 ++posI;
89 }
90
91 unsigned lineSize = 0;
92 posI = curLineStart;
93 while (posI != end() && isWhitespaceExceptNL(*posI)) {
94 ++posI;
95 ++lineSize;
96 }
97 if (posI != end() && *posI == '\n') {
98 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 AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
111 }
112 }
113}
114
115void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
116 bool InsertAfter) {
117 // Nothing to insert, exit early.
118 if (Str.empty()) return;
119
120 unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
121 Buffer.insert(RealOffset, Str.begin(), Str.end());
122
123 // Add a delta so that future changes are offset correctly.
124 AddInsertDelta(OrigOffset, Str.size());
125}
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.
130void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
131 StringRef NewStr) {
132 unsigned RealOffset = getMappedOffset(OrigOffset, true);
133 Buffer.erase(RealOffset, OrigLength);
134 Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
135 if (OrigLength != NewStr.size())
136 AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
137}
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.
146 RewriteOptions opts) const {
147 if (!isRewritable(Range.getBegin()) ||
148 !isRewritable(Range.getEnd())) return -1;
149
150 FileID StartFileID, EndFileID;
151 unsigned StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
152 unsigned EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
153
154 if (StartFileID != EndFileID)
155 return -1;
156
157 // If edits have been made to this buffer, the delta between the range may
158 // have changed.
159 std::map<FileID, RewriteBuffer>::const_iterator I =
160 RewriteBuffers.find(StartFileID);
161 if (I != RewriteBuffers.end()) {
162 const RewriteBuffer &RB = I->second;
163 EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
164 StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
165 }
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 if (Range.isTokenRange())
170 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
171
172 return EndOff-StartOff;
173}
174
176 return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
177}
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.
185 if (!isRewritable(Range.getBegin()) ||
186 !isRewritable(Range.getEnd()))
187 return {};
188
189 FileID StartFileID, EndFileID;
190 unsigned StartOff, EndOff;
191 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
192 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
193
194 if (StartFileID != EndFileID)
195 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 std::map<FileID, RewriteBuffer>::const_iterator I =
200 RewriteBuffers.find(StartFileID);
201 if (I == RewriteBuffers.end()) {
202 // If the buffer hasn't been rewritten, just return the text from the input.
203 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 if (Range.isTokenRange())
208 EndOff +=
209 Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
210 return std::string(Ptr, Ptr+EndOff-StartOff);
211 }
212
213 const RewriteBuffer &RB = I->second;
214 EndOff = RB.getMappedOffset(EndOff, true);
215 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 if (Range.isTokenRange())
220 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
221
222 // Advance the iterators to the right spot, yay for linear time algorithms.
223 RewriteBuffer::iterator Start = RB.begin();
224 std::advance(Start, StartOff);
225 RewriteBuffer::iterator End = Start;
226 assert(EndOff >= StartOff && "Invalid iteration distance");
227 std::advance(End, EndOff-StartOff);
228
229 return std::string(Start, End);
230}
231
232unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
233 FileID &FID) const {
234 assert(Loc.isValid() && "Invalid location");
235 std::pair<FileID, unsigned> V = SourceMgr->getDecomposedLoc(Loc);
236 FID = V.first;
237 return V.second;
238}
239
240/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
242 std::map<FileID, RewriteBuffer>::iterator I =
243 RewriteBuffers.lower_bound(FID);
244 if (I != RewriteBuffers.end() && I->first == FID)
245 return I->second;
246 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
247
248 StringRef MB = SourceMgr->getBufferData(FID);
249 I->second.Initialize(MB.begin(), MB.end());
250
251 return I->second;
252}
253
254/// InsertText - Insert the specified string at the specified location in the
255/// original buffer.
256bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
257 bool InsertAfter, bool indentNewLines) {
258 if (!isRewritable(Loc)) return true;
259 FileID FID;
260 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
261
262 SmallString<128> indentedStr;
263 if (indentNewLines && Str.contains('\n')) {
264 StringRef MB = SourceMgr->getBufferData(FID);
265
266 unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
267 const SrcMgr::ContentCache *Content =
268 &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
269 unsigned lineOffs = Content->SourceLineCache[lineNo];
270
271 // Find the whitespace at the start of the line.
272 StringRef indentSpace;
273 {
274 unsigned i = lineOffs;
275 while (isWhitespaceExceptNL(MB[i]))
276 ++i;
277 indentSpace = MB.substr(lineOffs, i-lineOffs);
278 }
279
281 Str.split(lines, "\n");
282
283 for (unsigned i = 0, e = lines.size(); i != e; ++i) {
284 indentedStr += lines[i];
285 if (i < e-1) {
286 indentedStr += '\n';
287 indentedStr += indentSpace;
288 }
289 }
290 Str = indentedStr.str();
291 }
292
293 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
294 return false;
295}
296
298 if (!isRewritable(Loc)) return true;
299 FileID FID;
300 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
301 RewriteOptions rangeOpts;
302 rangeOpts.IncludeInsertsAtBeginOfRange = false;
303 StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
304 getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
305 return false;
306}
307
308/// RemoveText - Remove the specified text region.
309bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
310 RewriteOptions opts) {
311 if (!isRewritable(Start)) return true;
312 FileID FID;
313 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
314 getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
315 return false;
316}
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.
321bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
322 StringRef NewStr) {
323 if (!isRewritable(Start)) return true;
324 FileID StartFileID;
325 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
326
327 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
328 return false;
329}
330
331bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
332 if (!isRewritable(range.getBegin())) return true;
333 if (!isRewritable(range.getEnd())) return true;
334 if (replacementRange.isInvalid()) return true;
335 SourceLocation start = range.getBegin();
336 unsigned origLength = getRangeSize(range);
337 unsigned newLength = getRangeSize(replacementRange);
338 FileID FID;
339 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
340 FID);
341 StringRef MB = SourceMgr->getBufferData(FID);
342 return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
343}
344
346 SourceLocation parentIndent) {
347 if (range.isInvalid()) return true;
348 if (!isRewritable(range.getBegin())) return true;
349 if (!isRewritable(range.getEnd())) return true;
350 if (!isRewritable(parentIndent)) return true;
351
352 FileID StartFileID, EndFileID, parentFileID;
353 unsigned StartOff, EndOff, parentOff;
354
355 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
356 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
357 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
358
359 if (StartFileID != EndFileID || StartFileID != parentFileID)
360 return true;
361 if (StartOff > EndOff)
362 return true;
363
364 FileID FID = StartFileID;
365 StringRef MB = SourceMgr->getBufferData(FID);
366
367 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
368 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
369 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
370
371 const SrcMgr::ContentCache *Content =
372 &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
373
374 // Find where the lines start.
375 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
376 unsigned startLineOffs = Content->SourceLineCache[startLineNo];
377
378 // Find the whitespace at the start of each line.
379 StringRef parentSpace, startSpace;
380 {
381 unsigned i = parentLineOffs;
382 while (isWhitespaceExceptNL(MB[i]))
383 ++i;
384 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
385
386 i = startLineOffs;
387 while (isWhitespaceExceptNL(MB[i]))
388 ++i;
389 startSpace = MB.substr(startLineOffs, i-startLineOffs);
390 }
391 if (parentSpace.size() >= startSpace.size())
392 return true;
393 if (!startSpace.startswith(parentSpace))
394 return true;
395
396 StringRef indent = startSpace.substr(parentSpace.size());
397
398 // Indent the lines between start/end offsets.
399 RewriteBuffer &RB = getEditBuffer(FID);
400 for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
401 unsigned offs = Content->SourceLineCache[lineNo];
402 unsigned i = offs;
403 while (isWhitespaceExceptNL(MB[i]))
404 ++i;
405 StringRef origIndent = MB.substr(offs, i-offs);
406 if (origIndent.startswith(startSpace))
407 RB.InsertText(offs, indent, /*InsertAfter=*/false);
408 }
409
410 return false;
411}
412
413namespace {
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.
421class AtomicallyMovedFile {
422public:
423 AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,
424 bool &AllWritten)
425 : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
426 TempFilename = Filename;
427 TempFilename += "-%%%%%%%%";
428 int FD;
429 if (llvm::sys::fs::createUniqueFile(TempFilename, FD, TempFilename)) {
430 AllWritten = false;
431 Diagnostics.Report(clang::diag::err_unable_to_make_temp)
432 << TempFilename;
433 } else {
434 FileStream.reset(new llvm::raw_fd_ostream(FD, /*shouldClose=*/true));
435 }
436 }
437
438 ~AtomicallyMovedFile() {
439 if (!ok()) return;
440
441 // Close (will also flush) theFileStream.
442 FileStream->close();
443 if (std::error_code ec = llvm::sys::fs::rename(TempFilename, Filename)) {
444 AllWritten = false;
445 Diagnostics.Report(clang::diag::err_unable_to_rename_temp)
446 << TempFilename << Filename << ec.message();
447 // If the remove fails, there's not a lot we can do - this is already an
448 // error.
449 llvm::sys::fs::remove(TempFilename);
450 }
451 }
452
453 bool ok() { return (bool)FileStream; }
454 raw_ostream &getStream() { return *FileStream; }
455
456private:
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
467 bool AllWritten = true;
468 for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
469 const FileEntry *Entry =
471 AtomicallyMovedFile File(getSourceMgr().getDiagnostics(), Entry->getName(),
472 AllWritten);
473 if (File.ok()) {
474 I->second.write(File.getStream());
475 }
476 }
477 return !AllWritten;
478}
#define V(N, I)
Definition: ASTContext.h:3217
Defines the Diagnostic-related interfaces.
Defines the Diagnostic IDs-related interfaces.
Defines the clang::FileManager interface and associated types.
StringRef Filename
Definition: Format.cpp:2774
static bool isWhitespaceExceptNL(unsigned char c)
Return true if this character is non-new-line whitespace: ' ', '\t', '\f', '\v', '\r'.
Definition: Rewriter.cpp:48
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
__device__ __2f16 float c
Represents a character-granular source range.
static CharSourceRange getTokenRange(SourceRange R)
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1542
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:353
StringRef getName() const
Definition: FileEntry.h:384
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
Definition: Lexer.cpp:450
RewriteBuffer - As code is rewritten, SourceBuffer's from the original input with modifications get a...
Definition: RewriteBuffer.h:25
void RemoveText(unsigned OrigOffset, unsigned Size, bool removeLineIfEmpty=false)
RemoveText - Remove the specified text.
Definition: Rewriter.cpp:61
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
Definition: Rewriter.cpp:37
void InsertText(unsigned OrigOffset, StringRef Str, bool InsertAfter=true)
InsertText - Insert some text at the specified point, where the offset in the buffer is specified rel...
Definition: Rewriter.cpp:115
iterator end() const
Definition: RewriteBuffer.h:38
iterator begin() const
Definition: RewriteBuffer.h:37
void ReplaceText(unsigned OrigOffset, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string.
Definition: Rewriter.cpp:130
unsigned size() const
Definition: RewriteRope.h:189
void erase(unsigned Offset, unsigned NumBytes)
Definition: RewriteRope.h:207
void insert(unsigned Offset, const char *Start, const char *End)
Definition: RewriteRope.h:201
int getRangeSize(SourceRange Range, RewriteOptions opts=RewriteOptions()) const
getRangeSize - Return the size in bytes of the specified range if they are in the same file.
Definition: Rewriter.cpp:175
bool InsertText(SourceLocation Loc, StringRef Str, bool InsertAfter=true, bool indentNewLines=false)
InsertText - Insert the specified string at the specified location in the original buffer.
Definition: Rewriter.cpp:256
bool RemoveText(SourceLocation Start, unsigned Length, RewriteOptions opts=RewriteOptions())
RemoveText - Remove the specified text region.
Definition: Rewriter.cpp:309
static bool isRewritable(SourceLocation Loc)
isRewritable - Return true if this location is a raw file location, which is rewritable.
Definition: Rewriter.h:82
buffer_iterator buffer_end()
Definition: Rewriter.h:206
SourceManager & getSourceMgr() const
Definition: Rewriter.h:77
buffer_iterator buffer_begin()
Definition: Rewriter.h:205
std::map< FileID, RewriteBuffer >::iterator buffer_iterator
Definition: Rewriter.h:65
std::string getRewrittenText(CharSourceRange Range) const
getRewrittenText - Return the rewritten form of the text in the specified range.
Definition: Rewriter.cpp:184
bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent)
Increase indentation for the lines between the given source range.
Definition: Rewriter.cpp:345
bool InsertTextAfterToken(SourceLocation Loc, StringRef Str)
Insert the specified string after the token in the specified location.
Definition: Rewriter.cpp:297
RewriteBuffer & getEditBuffer(FileID FID)
getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to writ...
Definition: Rewriter.cpp:241
bool overwriteChangedFiles()
overwriteChangedFiles - Save all changed files to disk.
Definition: Rewriter.cpp:466
bool ReplaceText(SourceLocation Start, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string.
Definition: Rewriter.cpp:321
RopePieceBTreeIterator - This class provides read-only forward iteration over bytes that are in a Rop...
Definition: RewriteRope.h:86
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer.
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Given a SourceLocation, return the spelling line number for the position indicated.
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
const SrcMgr::SLocEntry & getSLocEntry(FileID FID, bool *Invalid=nullptr) const
A trivial tuple used to represent a source range.
bool isInvalid() const
SourceLocation getBegin() const
One instance of this struct is kept for every file loaded or used.
LineOffsetMapping SourceLineCache
A bump pointer allocated array of offsets for each source line.
const ContentCache & getContentCache() const
const FileInfo & getFile() const
bool IncludeInsertsAtBeginOfRange
Given a source range, true to include previous inserts at the beginning of the range as part of the r...
Definition: Rewriter.h:41
bool IncludeInsertsAtEndOfRange
Given a source range, true to include previous inserts at the end of the range as part of the range i...
Definition: Rewriter.h:45
bool RemoveLineIfEmpty
If true and removing some text leaves a blank line also remove the empty line (false by default).
Definition: Rewriter.h:60