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/Stmt.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/Basic/DiagnosticIDs.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Lex/Lexer.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/Support/FileSystem.h"
24 using namespace clang;
26 raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
27 // FIXME: eliminate the copy by writing out each chunk at a time
28 os << std::string(begin(), end());
32 /// \brief Return true if this character is non-new-line whitespace:
33 /// ' ', '\\t', '\\f', '\\v', '\\r'.
34 static inline bool isWhitespace(unsigned char c) {
47 void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
48 bool removeLineIfEmpty) {
49 // Nothing to remove, exit early.
50 if (Size == 0) return;
52 unsigned RealOffset = getMappedOffset(OrigOffset, true);
53 assert(RealOffset+Size < Buffer.size() && "Invalid location");
55 // Remove the dead characters.
56 Buffer.erase(RealOffset, Size);
58 // Add a delta so that future changes are offset correctly.
59 AddReplaceDelta(OrigOffset, -Size);
61 if (removeLineIfEmpty) {
62 // Find the line that the remove occurred and if it is completely empty
63 // remove the line as well.
65 iterator curLineStart = begin();
66 unsigned curLineStartOffs = 0;
67 iterator posI = begin();
68 for (unsigned i = 0; i != RealOffset; ++i) {
72 curLineStartOffs = i + 1;
77 unsigned lineSize = 0;
79 while (posI != end() && isWhitespace(*posI)) {
83 if (posI != end() && *posI == '\n') {
84 Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
85 AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
90 void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
93 // Nothing to insert, exit early.
94 if (Str.empty()) return;
96 unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
97 Buffer.insert(RealOffset, Str.begin(), Str.end());
99 // Add a delta so that future changes are offset correctly.
100 AddInsertDelta(OrigOffset, Str.size());
103 /// ReplaceText - This method replaces a range of characters in the input
104 /// buffer with a new string. This is effectively a combined "remove+insert"
106 void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
108 unsigned RealOffset = getMappedOffset(OrigOffset, true);
109 Buffer.erase(RealOffset, OrigLength);
110 Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
111 if (OrigLength != NewStr.size())
112 AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
116 //===----------------------------------------------------------------------===//
118 //===----------------------------------------------------------------------===//
120 /// getRangeSize - Return the size in bytes of the specified range if they
121 /// are in the same file. If not, this returns -1.
122 int Rewriter::getRangeSize(const CharSourceRange &Range,
123 RewriteOptions opts) const {
124 if (!isRewritable(Range.getBegin()) ||
125 !isRewritable(Range.getEnd())) return -1;
127 FileID StartFileID, EndFileID;
128 unsigned StartOff, EndOff;
130 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
131 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
133 if (StartFileID != EndFileID)
136 // If edits have been made to this buffer, the delta between the range may
138 std::map<FileID, RewriteBuffer>::const_iterator I =
139 RewriteBuffers.find(StartFileID);
140 if (I != RewriteBuffers.end()) {
141 const RewriteBuffer &RB = I->second;
142 EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
143 StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
147 // Adjust the end offset to the end of the last token, instead of being the
148 // start of the last token if this is a token range.
149 if (Range.isTokenRange())
150 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
152 return EndOff-StartOff;
155 int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
156 return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
160 /// getRewrittenText - Return the rewritten form of the text in the specified
161 /// range. If the start or end of the range was unrewritable or if they are
162 /// in different buffers, this returns an empty string.
164 /// Note that this method is not particularly efficient.
166 std::string Rewriter::getRewrittenText(SourceRange Range) const {
167 if (!isRewritable(Range.getBegin()) ||
168 !isRewritable(Range.getEnd()))
171 FileID StartFileID, EndFileID;
172 unsigned StartOff, EndOff;
173 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
174 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
176 if (StartFileID != EndFileID)
177 return ""; // Start and end in different buffers.
179 // If edits have been made to this buffer, the delta between the range may
181 std::map<FileID, RewriteBuffer>::const_iterator I =
182 RewriteBuffers.find(StartFileID);
183 if (I == RewriteBuffers.end()) {
184 // If the buffer hasn't been rewritten, just return the text from the input.
185 const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
187 // Adjust the end offset to the end of the last token, instead of being the
188 // start of the last token.
189 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
190 return std::string(Ptr, Ptr+EndOff-StartOff);
193 const RewriteBuffer &RB = I->second;
194 EndOff = RB.getMappedOffset(EndOff, true);
195 StartOff = RB.getMappedOffset(StartOff);
197 // Adjust the end offset to the end of the last token, instead of being the
198 // start of the last token.
199 EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
201 // Advance the iterators to the right spot, yay for linear time algorithms.
202 RewriteBuffer::iterator Start = RB.begin();
203 std::advance(Start, StartOff);
204 RewriteBuffer::iterator End = Start;
205 std::advance(End, EndOff-StartOff);
207 return std::string(Start, End);
210 unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
212 assert(Loc.isValid() && "Invalid location");
213 std::pair<FileID,unsigned> V = SourceMgr->getDecomposedLoc(Loc);
219 /// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
221 RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
222 std::map<FileID, RewriteBuffer>::iterator I =
223 RewriteBuffers.lower_bound(FID);
224 if (I != RewriteBuffers.end() && I->first == FID)
226 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
228 StringRef MB = SourceMgr->getBufferData(FID);
229 I->second.Initialize(MB.begin(), MB.end());
234 /// InsertText - Insert the specified string at the specified location in the
236 bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
237 bool InsertAfter, bool indentNewLines) {
238 if (!isRewritable(Loc)) return true;
240 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
242 SmallString<128> indentedStr;
243 if (indentNewLines && Str.find('\n') != StringRef::npos) {
244 StringRef MB = SourceMgr->getBufferData(FID);
246 unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
247 const SrcMgr::ContentCache *
248 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
249 unsigned lineOffs = Content->SourceLineCache[lineNo];
251 // Find the whitespace at the start of the line.
252 StringRef indentSpace;
254 unsigned i = lineOffs;
255 while (isWhitespace(MB[i]))
257 indentSpace = MB.substr(lineOffs, i-lineOffs);
260 SmallVector<StringRef, 4> lines;
261 Str.split(lines, "\n");
263 for (unsigned i = 0, e = lines.size(); i != e; ++i) {
264 indentedStr += lines[i];
267 indentedStr += indentSpace;
270 Str = indentedStr.str();
273 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
277 bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
278 if (!isRewritable(Loc)) return true;
280 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
281 RewriteOptions rangeOpts;
282 rangeOpts.IncludeInsertsAtBeginOfRange = false;
283 StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
284 getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
288 /// RemoveText - Remove the specified text region.
289 bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
290 RewriteOptions opts) {
291 if (!isRewritable(Start)) return true;
293 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
294 getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
298 /// ReplaceText - This method replaces a range of characters in the input
299 /// buffer with a new string. This is effectively a combined "remove/insert"
301 bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
303 if (!isRewritable(Start)) return true;
305 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
307 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
311 bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
312 if (!isRewritable(range.getBegin())) return true;
313 if (!isRewritable(range.getEnd())) return true;
314 if (replacementRange.isInvalid()) return true;
315 SourceLocation start = range.getBegin();
316 unsigned origLength = getRangeSize(range);
317 unsigned newLength = getRangeSize(replacementRange);
319 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
321 StringRef MB = SourceMgr->getBufferData(FID);
322 return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
325 /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
326 /// printer to generate the replacement code. This returns true if the input
327 /// could not be rewritten, or false if successful.
328 bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
329 // Measaure the old text.
330 int Size = getRangeSize(From->getSourceRange());
336 llvm::raw_string_ostream S(SStr);
337 To->printPretty(S, 0, PrintingPolicy(*LangOpts));
338 const std::string &Str = S.str();
340 ReplaceText(From->getLocStart(), Size, Str);
344 std::string Rewriter::ConvertToString(Stmt *From) {
346 llvm::raw_string_ostream S(SStr);
347 From->printPretty(S, 0, PrintingPolicy(*LangOpts));
351 bool Rewriter::IncreaseIndentation(CharSourceRange range,
352 SourceLocation parentIndent) {
353 if (range.isInvalid()) return true;
354 if (!isRewritable(range.getBegin())) return true;
355 if (!isRewritable(range.getEnd())) return true;
356 if (!isRewritable(parentIndent)) return true;
358 FileID StartFileID, EndFileID, parentFileID;
359 unsigned StartOff, EndOff, parentOff;
361 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
362 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
363 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
365 if (StartFileID != EndFileID || StartFileID != parentFileID)
367 if (StartOff > EndOff)
370 FileID FID = StartFileID;
371 StringRef MB = SourceMgr->getBufferData(FID);
373 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
374 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
375 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
377 const SrcMgr::ContentCache *
378 Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache();
380 // Find where the lines start.
381 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
382 unsigned startLineOffs = Content->SourceLineCache[startLineNo];
384 // Find the whitespace at the start of each line.
385 StringRef parentSpace, startSpace;
387 unsigned i = parentLineOffs;
388 while (isWhitespace(MB[i]))
390 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
393 while (isWhitespace(MB[i]))
395 startSpace = MB.substr(startLineOffs, i-startLineOffs);
397 if (parentSpace.size() >= startSpace.size())
399 if (!startSpace.startswith(parentSpace))
402 StringRef indent = startSpace.substr(parentSpace.size());
404 // Indent the lines between start/end offsets.
405 RewriteBuffer &RB = getEditBuffer(FID);
406 for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
407 unsigned offs = Content->SourceLineCache[lineNo];
409 while (isWhitespace(MB[i]))
411 StringRef origIndent = MB.substr(offs, i-offs);
412 if (origIndent.startswith(startSpace))
413 RB.InsertText(offs, indent, /*InsertAfter=*/false);
419 // A wrapper for a file stream that atomically overwrites the target.
421 // Creates a file output stream for a temporary file in the constructor,
422 // which is later accessible via getStream() if ok() return true.
423 // Flushes the stream and moves the temporary file to the target location
424 // in the destructor.
425 class AtomicallyMovedFile {
427 AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,
429 : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
430 TempFilename = Filename;
431 TempFilename += "-%%%%%%%%";
433 if (llvm::sys::fs::unique_file(TempFilename.str(), FD, TempFilename,
434 /*makeAbsolute=*/true, 0664)) {
436 Diagnostics.Report(clang::diag::err_unable_to_make_temp)
439 FileStream.reset(new llvm::raw_fd_ostream(FD, /*shouldClose=*/true));
443 ~AtomicallyMovedFile() {
448 // Win32 does not allow rename/removing opened files.
451 if (llvm::error_code ec =
452 llvm::sys::fs::rename(TempFilename.str(), Filename)) {
454 Diagnostics.Report(clang::diag::err_unable_to_rename_temp)
455 << TempFilename << Filename << ec.message();
457 // If the remove fails, there's not a lot we can do - this is already an
459 llvm::sys::fs::remove(TempFilename.str(), existed);
463 bool ok() { return FileStream; }
464 llvm::raw_ostream &getStream() { return *FileStream; }
467 DiagnosticsEngine &Diagnostics;
469 SmallString<128> TempFilename;
470 OwningPtr<llvm::raw_fd_ostream> FileStream;
474 bool Rewriter::overwriteChangedFiles() {
475 bool AllWritten = true;
476 for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
477 const FileEntry *Entry =
478 getSourceMgr().getFileEntryForID(I->first);
479 AtomicallyMovedFile File(getSourceMgr().getDiagnostics(), Entry->getName(),
482 I->second.write(File.getStream());