1 //===--- Rewriter.cpp - Code rewriting interface --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Rewriter class, which is used for code
13 //===----------------------------------------------------------------------===//
15 #include "clang/Rewrite/Core/Rewriter.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/PrettyPrinter.h"
18 #include "clang/AST/Stmt.h"
19 #include "clang/Basic/DiagnosticIDs.h"
20 #include "clang/Basic/FileManager.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Lex/Lexer.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace clang;
28 raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
29 // FIXME: eliminate the copy by writing out each chunk at a time
30 os << std::string(begin(), end());
34 /// \brief Return true if this character is non-new-line whitespace:
35 /// ' ', '\\t', '\\f', '\\v', '\\r'.
36 static inline bool isWhitespace(unsigned char c) {
49 void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
50 bool removeLineIfEmpty) {
51 // Nothing to remove, exit early.
52 if (Size == 0) return;
54 unsigned RealOffset = getMappedOffset(OrigOffset, true);
55 assert(RealOffset+Size < Buffer.size() && "Invalid location");
57 // Remove the dead characters.
58 Buffer.erase(RealOffset, Size);
60 // Add a delta so that future changes are offset correctly.
61 AddReplaceDelta(OrigOffset, -Size);
63 if (removeLineIfEmpty) {
64 // Find the line that the remove occurred and if it is completely empty
65 // remove the line as well.
67 iterator curLineStart = begin();
68 unsigned curLineStartOffs = 0;
69 iterator posI = begin();
70 for (unsigned i = 0; i != RealOffset; ++i) {
74 curLineStartOffs = i + 1;
79 unsigned lineSize = 0;
81 while (posI != end() && isWhitespace(*posI)) {
85 if (posI != end() && *posI == '\n') {
86 Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
87 AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
92 void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
95 // Nothing to insert, exit early.
96 if (Str.empty()) return;
98 unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
99 Buffer.insert(RealOffset, Str.begin(), Str.end());
101 // Add a delta so that future changes are offset correctly.
102 AddInsertDelta(OrigOffset, Str.size());
105 /// ReplaceText - This method replaces a range of characters in the input
106 /// buffer with a new string. This is effectively a combined "remove+insert"
108 void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
110 unsigned RealOffset = getMappedOffset(OrigOffset, true);
111 Buffer.erase(RealOffset, OrigLength);
112 Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
113 if (OrigLength != NewStr.size())
114 AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
118 //===----------------------------------------------------------------------===//
120 //===----------------------------------------------------------------------===//
122 /// getRangeSize - Return the size in bytes of the specified range if they
123 /// are in the same file. If not, this returns -1.
124 int Rewriter::getRangeSize(const CharSourceRange &Range,
125 RewriteOptions opts) const {
126 if (!isRewritable(Range.getBegin()) ||
127 !isRewritable(Range.getEnd())) return -1;
129 FileID StartFileID, EndFileID;
130 unsigned StartOff, EndOff;
132 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
133 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
135 if (StartFileID != EndFileID)
138 // If edits have been made to this buffer, the delta between the range may
140 std::map<FileID, RewriteBuffer>::const_iterator I =
141 RewriteBuffers.find(StartFileID);
142 if (I != RewriteBuffers.end()) {
143 const RewriteBuffer &RB = I->second;
144 EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
145 StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
149 // Adjust the end offset to the end of the last token, instead of being the
150 // start of the last token if this is a token range.
151 if (Range.isTokenRange())
152 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
154 return EndOff-StartOff;
157 int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
158 return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
162 /// getRewrittenText - Return the rewritten form of the text in the specified
163 /// range. If the start or end of the range was unrewritable or if they are
164 /// in different buffers, this returns an empty string.
166 /// Note that this method is not particularly efficient.
168 std::string Rewriter::getRewrittenText(SourceRange Range) const {
169 if (!isRewritable(Range.getBegin()) ||
170 !isRewritable(Range.getEnd()))
173 FileID StartFileID, EndFileID;
174 unsigned StartOff, EndOff;
175 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
176 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
178 if (StartFileID != EndFileID)
179 return ""; // Start and end in different buffers.
181 // If edits have been made to this buffer, the delta between the range may
183 std::map<FileID, RewriteBuffer>::const_iterator I =
184 RewriteBuffers.find(StartFileID);
185 if (I == RewriteBuffers.end()) {
186 // If the buffer hasn't been rewritten, just return the text from the input.
187 const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
189 // Adjust the end offset to the end of the last token, instead of being the
190 // start of the last token.
191 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
192 return std::string(Ptr, Ptr+EndOff-StartOff);
195 const RewriteBuffer &RB = I->second;
196 EndOff = RB.getMappedOffset(EndOff, true);
197 StartOff = RB.getMappedOffset(StartOff);
199 // Adjust the end offset to the end of the last token, instead of being the
200 // start of the last token.
201 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
203 // Advance the iterators to the right spot, yay for linear time algorithms.
204 RewriteBuffer::iterator Start = RB.begin();
205 std::advance(Start, StartOff);
206 RewriteBuffer::iterator End = Start;
207 std::advance(End, EndOff-StartOff);
209 return std::string(Start, End);
212 unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
214 assert(Loc.isValid() && "Invalid location");
215 std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
221 /// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
223 RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
224 std::map<FileID, RewriteBuffer>::iterator I =
225 RewriteBuffers.lower_bound(FID);
226 if (I != RewriteBuffers.end() && I->first == FID)
228 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
230 StringRef MB = SourceMgr->getBufferData(FID);
231 I->second.Initialize(MB.begin(), MB.end());
236 /// InsertText - Insert the specified string at the specified location in the
238 bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
239 bool InsertAfter, bool indentNewLines) {
240 if (!isRewritable(Loc)) return true;
242 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
244 SmallString<128> indentedStr;
245 if (indentNewLines && Str.find('\n') != StringRef::npos) {
246 StringRef MB = SourceMgr->getBufferData(FID);
248 unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
249 const SrcMgr::ContentCache *
250 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
251 unsigned lineOffs = Content->SourceLineCache[lineNo];
253 // Find the whitespace at the start of the line.
254 StringRef indentSpace;
256 unsigned i = lineOffs;
257 while (isWhitespace(MB[i]))
259 indentSpace = MB.substr(lineOffs, i-lineOffs);
262 SmallVector<StringRef, 4> lines;
263 Str.split(lines, "\n");
265 for (unsigned i = 0, e = lines.size(); i != e; ++i) {
266 indentedStr += lines[i];
269 indentedStr += indentSpace;
272 Str = indentedStr.str();
275 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
279 bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
280 if (!isRewritable(Loc)) return true;
282 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
283 RewriteOptions rangeOpts;
284 rangeOpts.IncludeInsertsAtBeginOfRange = false;
285 StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
286 getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
290 /// RemoveText - Remove the specified text region.
291 bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
292 RewriteOptions opts) {
293 if (!isRewritable(Start)) return true;
295 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
296 getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
300 /// ReplaceText - This method replaces a range of characters in the input
301 /// buffer with a new string. This is effectively a combined "remove/insert"
303 bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
305 if (!isRewritable(Start)) return true;
307 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
309 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
313 bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
314 if (!isRewritable(range.getBegin())) return true;
315 if (!isRewritable(range.getEnd())) return true;
316 if (replacementRange.isInvalid()) return true;
317 SourceLocation start = range.getBegin();
318 unsigned origLength = getRangeSize(range);
319 unsigned newLength = getRangeSize(replacementRange);
321 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
323 StringRef MB = SourceMgr->getBufferData(FID);
324 return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
327 /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
328 /// printer to generate the replacement code. This returns true if the input
329 /// could not be rewritten, or false if successful.
330 bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
331 // Measaure the old text.
332 int Size = getRangeSize(From->getSourceRange());
338 llvm::raw_string_ostream S(SStr);
339 To->printPretty(S, 0, PrintingPolicy(*LangOpts));
340 const std::string &Str = S.str();
342 ReplaceText(From->getLocStart(), Size, Str);
346 std::string Rewriter::ConvertToString(Stmt *From) {
348 llvm::raw_string_ostream S(SStr);
349 From->printPretty(S, 0, PrintingPolicy(*LangOpts));
353 bool Rewriter::IncreaseIndentation(CharSourceRange range,
354 SourceLocation parentIndent) {
355 if (range.isInvalid()) return true;
356 if (!isRewritable(range.getBegin())) return true;
357 if (!isRewritable(range.getEnd())) return true;
358 if (!isRewritable(parentIndent)) return true;
360 FileID StartFileID, EndFileID, parentFileID;
361 unsigned StartOff, EndOff, parentOff;
363 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
364 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
365 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
367 if (StartFileID != EndFileID || StartFileID != parentFileID)
369 if (StartOff > EndOff)
372 FileID FID = StartFileID;
373 StringRef MB = SourceMgr->getBufferData(FID);
375 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
376 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
377 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
379 const SrcMgr::ContentCache *
380 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
382 // Find where the lines start.
383 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
384 unsigned startLineOffs = Content->SourceLineCache[startLineNo];
386 // Find the whitespace at the start of each line.
387 StringRef parentSpace, startSpace;
389 unsigned i = parentLineOffs;
390 while (isWhitespace(MB[i]))
392 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
395 while (isWhitespace(MB[i]))
397 startSpace = MB.substr(startLineOffs, i-startLineOffs);
399 if (parentSpace.size() >= startSpace.size())
401 if (!startSpace.startswith(parentSpace))
404 StringRef indent = startSpace.substr(parentSpace.size());
406 // Indent the lines between start/end offsets.
407 RewriteBuffer &RB = getEditBuffer(FID);
408 for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
409 unsigned offs = Content->SourceLineCache[lineNo];
411 while (isWhitespace(MB[i]))
413 StringRef origIndent = MB.substr(offs, i-offs);
414 if (origIndent.startswith(startSpace))
415 RB.InsertText(offs, indent, /*InsertAfter=*/false);
422 // A wrapper for a file stream that atomically overwrites the target.
424 // Creates a file output stream for a temporary file in the constructor,
425 // which is later accessible via getStream() if ok() return true.
426 // Flushes the stream and moves the temporary file to the target location
427 // in the destructor.
428 class AtomicallyMovedFile {
430 AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,
432 : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
433 TempFilename = Filename;
434 TempFilename += "-%%%%%%%%";
436 if (llvm::sys::fs::unique_file(TempFilename.str(), FD, TempFilename,
437 /*makeAbsolute=*/true, 0664)) {
439 Diagnostics.Report(clang::diag::err_unable_to_make_temp)
442 FileStream.reset(new llvm::raw_fd_ostream(FD, /*shouldClose=*/true));
446 ~AtomicallyMovedFile() {
451 // Win32 does not allow rename/removing opened files.
454 if (llvm::error_code ec =
455 llvm::sys::fs::rename(TempFilename.str(), Filename)) {
457 Diagnostics.Report(clang::diag::err_unable_to_rename_temp)
458 << TempFilename << Filename << ec.message();
460 // If the remove fails, there's not a lot we can do - this is already an
462 llvm::sys::fs::remove(TempFilename.str(), existed);
466 bool ok() { return FileStream; }
467 raw_ostream &getStream() { return *FileStream; }
470 DiagnosticsEngine &Diagnostics;
472 SmallString<128> TempFilename;
473 OwningPtr<llvm::raw_fd_ostream> FileStream;
476 } // end anonymous namespace
478 bool Rewriter::overwriteChangedFiles() {
479 bool AllWritten = true;
480 for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
481 const FileEntry *Entry =
482 getSourceMgr().getFileEntryForID(I->first);
483 AtomicallyMovedFile File(getSourceMgr().getDiagnostics(), Entry->getName(),
486 I->second.write(File.getStream());