2018-03-27 08:01:49 +08:00
|
|
|
//===- Rewriter.cpp - Code rewriting interface ----------------------------===//
|
2007-09-16 06:21:22 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-09-16 06:21:22 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the Rewriter class, which is used for code
|
|
|
|
// transformations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2014-10-30 06:13:46 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-05-23 01:01:35 +08:00
|
|
|
#include "clang/Basic/DiagnosticIDs.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2007-10-12 02:38:32 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-05-23 01:01:35 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include "clang/Rewrite/Core/RewriteBuffer.h"
|
|
|
|
#include "clang/Rewrite/Core/RewriteRope.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-05-23 01:01:35 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-27 08:01:49 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <iterator>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
|
|
|
|
2007-09-16 06:21:22 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
|
2013-12-03 01:02:49 +08:00
|
|
|
// Walk RewriteRope chunks efficiently using MoveToNextPiece() instead of the
|
|
|
|
// character iterator.
|
|
|
|
for (RopePieceBTreeIterator I = begin(), E = end(); I != E;
|
|
|
|
I.MoveToNextPiece())
|
|
|
|
os << I.piece();
|
2010-04-17 02:49:45 +08:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return true if this character is non-new-line whitespace:
|
2012-06-16 06:33:08 +08:00
|
|
|
/// ' ', '\\t', '\\f', '\\v', '\\r'.
|
2015-07-28 12:54:03 +08:00
|
|
|
static inline bool isWhitespaceExceptNL(unsigned char c) {
|
2011-04-08 02:10:12 +08:00
|
|
|
switch (c) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
|
|
|
case '\r':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
|
|
|
|
bool removeLineIfEmpty) {
|
2007-10-13 08:11:23 +08:00
|
|
|
// Nothing to remove, exit early.
|
|
|
|
if (Size == 0) return;
|
|
|
|
|
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, true);
|
2015-03-08 12:00:33 +08:00
|
|
|
assert(RealOffset+Size <= Buffer.size() && "Invalid location");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
// Remove the dead characters.
|
2008-04-14 15:17:29 +08:00
|
|
|
Buffer.erase(RealOffset, Size);
|
2007-10-13 08:11:23 +08:00
|
|
|
|
|
|
|
// Add a delta so that future changes are offset correctly.
|
Fix for PR2386: distinguish between insertion and replacements in the
delta tree.
The issue is roughly a conflict in ReplaceText between two kinds of
uses. One, it should be possible to replace a replacement: for example, the
ObjC rewriter calls ReplaceStmt for an expression, then replaces the resulting
expression with another expression. Two, it should be possible to
replace text that already has text inserted before it: for example, the
HTML rewriter inserts a bunch of tags at the beginning of the line, then
tries to escape the first character on the line. This patch
distinguishes the two cases by storing the deltas separately;
essentially, replacements and insertions no longer interfere with
each other.
Another possibility would be to add some sort of flag to ReplaceText, but
this seems a bit more intuitive and flexible.
There are a few downsides to the current solution: one is that there isn't
any way to remove/replace an insertion without touching additional
surrounding text; if such an operation turns out to be useful, an
additional method or flag can be added. Another is that an insertion
and replacing a string of length zero are distinct operations; I'm not
sure how to resolve this, or whether it will be confusing in practice.
This is relatively sensitive code, so please test and tell me if
anything breaks.
llvm-svn: 72000
2009-05-18 21:56:52 +08:00
|
|
|
AddReplaceDelta(OrigOffset, -Size);
|
2011-04-08 02:10:12 +08:00
|
|
|
|
|
|
|
if (removeLineIfEmpty) {
|
|
|
|
// Find the line that the remove occurred and if it is completely empty
|
|
|
|
// remove the line as well.
|
|
|
|
|
|
|
|
iterator curLineStart = begin();
|
|
|
|
unsigned curLineStartOffs = 0;
|
|
|
|
iterator posI = begin();
|
|
|
|
for (unsigned i = 0; i != RealOffset; ++i) {
|
|
|
|
if (*posI == '\n') {
|
|
|
|
curLineStart = posI;
|
|
|
|
++curLineStart;
|
|
|
|
curLineStartOffs = i + 1;
|
|
|
|
}
|
|
|
|
++posI;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-04-08 02:10:12 +08:00
|
|
|
unsigned lineSize = 0;
|
|
|
|
posI = curLineStart;
|
2015-07-27 14:35:22 +08:00
|
|
|
while (posI != end() && isWhitespaceExceptNL(*posI)) {
|
2011-04-08 02:10:12 +08:00
|
|
|
++posI;
|
|
|
|
++lineSize;
|
|
|
|
}
|
|
|
|
if (posI != end() && *posI == '\n') {
|
|
|
|
Buffer.erase(curLineStartOffs, lineSize + 1/* + '\n'*/);
|
2019-08-16 05:17:48 +08:00
|
|
|
// FIXME: Here, the offset of the start of the line is supposed to be
|
|
|
|
// expressed in terms of the original input not the "real" rewrite
|
|
|
|
// buffer. How do we compute that reliably? It might be tempting to use
|
|
|
|
// curLineStartOffs + OrigOffset - RealOffset, but that assumes the
|
|
|
|
// difference between the original and real offset is the same at the
|
|
|
|
// removed text and at the start of the line, but that's not true if
|
|
|
|
// edits were previously made earlier on the line. This bug is also
|
|
|
|
// documented by a FIXME on the definition of
|
|
|
|
// clang::Rewriter::RewriteOptions::RemoveLineIfEmpty. A reproducer for
|
|
|
|
// the implementation below is the test RemoveLineIfEmpty in
|
|
|
|
// clang/unittests/Rewrite/RewriteBufferTest.cpp.
|
2011-04-08 02:10:12 +08:00
|
|
|
AddReplaceDelta(curLineStartOffs, -(lineSize + 1/* + '\n'*/));
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 06:21:22 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void RewriteBuffer::InsertText(unsigned OrigOffset, StringRef Str,
|
2008-03-19 05:17:59 +08:00
|
|
|
bool InsertAfter) {
|
2007-10-13 08:21:23 +08:00
|
|
|
// Nothing to insert, exit early.
|
2009-08-20 03:10:30 +08:00
|
|
|
if (Str.empty()) return;
|
2008-04-14 15:17:29 +08:00
|
|
|
|
2008-03-19 05:17:59 +08:00
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
|
2009-08-20 03:10:30 +08:00
|
|
|
Buffer.insert(RealOffset, Str.begin(), Str.end());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-13 08:21:23 +08:00
|
|
|
// Add a delta so that future changes are offset correctly.
|
2009-08-20 03:10:30 +08:00
|
|
|
AddInsertDelta(OrigOffset, Str.size());
|
2007-09-16 06:21:22 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
/// ReplaceText - This method replaces a range of characters in the input
|
2008-04-14 15:17:29 +08:00
|
|
|
/// buffer with a new string. This is effectively a combined "remove+insert"
|
2007-10-13 08:11:23 +08:00
|
|
|
/// operation.
|
|
|
|
void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef NewStr) {
|
Fix for PR2386: distinguish between insertion and replacements in the
delta tree.
The issue is roughly a conflict in ReplaceText between two kinds of
uses. One, it should be possible to replace a replacement: for example, the
ObjC rewriter calls ReplaceStmt for an expression, then replaces the resulting
expression with another expression. Two, it should be possible to
replace text that already has text inserted before it: for example, the
HTML rewriter inserts a bunch of tags at the beginning of the line, then
tries to escape the first character on the line. This patch
distinguishes the two cases by storing the deltas separately;
essentially, replacements and insertions no longer interfere with
each other.
Another possibility would be to add some sort of flag to ReplaceText, but
this seems a bit more intuitive and flexible.
There are a few downsides to the current solution: one is that there isn't
any way to remove/replace an insertion without touching additional
surrounding text; if such an operation turns out to be useful, an
additional method or flag can be added. Another is that an insertion
and replacing a string of length zero are distinct operations; I'm not
sure how to resolve this, or whether it will be confusing in practice.
This is relatively sensitive code, so please test and tell me if
anything breaks.
llvm-svn: 72000
2009-05-18 21:56:52 +08:00
|
|
|
unsigned RealOffset = getMappedOffset(OrigOffset, true);
|
2008-04-14 15:17:29 +08:00
|
|
|
Buffer.erase(RealOffset, OrigLength);
|
2009-08-20 03:10:30 +08:00
|
|
|
Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
|
|
|
|
if (OrigLength != NewStr.size())
|
|
|
|
AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
|
2007-10-13 08:11:23 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Rewriter class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-17 06:36:42 +08:00
|
|
|
/// getRangeSize - Return the size in bytes of the specified range if they
|
|
|
|
/// are in the same file. If not, this returns -1.
|
2011-04-08 02:10:12 +08:00
|
|
|
int Rewriter::getRangeSize(const CharSourceRange &Range,
|
2011-04-13 15:15:11 +08:00
|
|
|
RewriteOptions opts) const {
|
2007-10-17 06:36:42 +08:00
|
|
|
if (!isRewritable(Range.getBegin()) ||
|
|
|
|
!isRewritable(Range.getEnd())) return -1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID StartFileID, EndFileID;
|
2018-03-27 08:01:49 +08:00
|
|
|
unsigned StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
|
|
|
|
unsigned EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-17 06:36:42 +08:00
|
|
|
if (StartFileID != EndFileID)
|
|
|
|
return -1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 01:18:59 +08:00
|
|
|
// If edits have been made to this buffer, the delta between the range may
|
|
|
|
// have changed.
|
2009-01-17 14:22:33 +08:00
|
|
|
std::map<FileID, RewriteBuffer>::const_iterator I =
|
2007-10-26 01:17:34 +08:00
|
|
|
RewriteBuffers.find(StartFileID);
|
2007-10-26 01:18:59 +08:00
|
|
|
if (I != RewriteBuffers.end()) {
|
2007-10-26 01:17:34 +08:00
|
|
|
const RewriteBuffer &RB = I->second;
|
2011-04-13 15:15:11 +08:00
|
|
|
EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
|
|
|
|
StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
|
2007-10-26 01:17:34 +08:00
|
|
|
}
|
|
|
|
|
2007-10-18 05:23:07 +08:00
|
|
|
// Adjust the end offset to the end of the last token, instead of being the
|
2010-06-19 06:45:06 +08:00
|
|
|
// start of the last token if this is a token range.
|
|
|
|
if (Range.isTokenRange())
|
|
|
|
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-26 01:18:59 +08:00
|
|
|
return EndOff-StartOff;
|
2007-10-17 06:36:42 +08:00
|
|
|
}
|
|
|
|
|
2011-04-13 15:15:11 +08:00
|
|
|
int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
|
|
|
|
return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
|
2010-06-19 06:45:06 +08:00
|
|
|
}
|
|
|
|
|
2010-01-08 02:00:35 +08:00
|
|
|
/// getRewrittenText - Return the rewritten form of the text in the specified
|
2008-10-04 07:31:16 +08:00
|
|
|
/// range. If the start or end of the range was unrewritable or if they are
|
2009-09-09 23:08:12 +08:00
|
|
|
/// in different buffers, this returns an empty string.
|
2008-10-04 07:31:16 +08:00
|
|
|
///
|
|
|
|
/// Note that this method is not particularly efficient.
|
2019-07-06 10:55:06 +08:00
|
|
|
std::string Rewriter::getRewrittenText(CharSourceRange Range) const {
|
2008-10-04 07:31:16 +08:00
|
|
|
if (!isRewritable(Range.getBegin()) ||
|
|
|
|
!isRewritable(Range.getEnd()))
|
2018-03-27 08:01:49 +08:00
|
|
|
return {};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID StartFileID, EndFileID;
|
|
|
|
unsigned StartOff, EndOff;
|
2008-10-04 07:31:16 +08:00
|
|
|
StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
|
|
|
|
EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
if (StartFileID != EndFileID)
|
2018-03-27 08:01:49 +08:00
|
|
|
return {}; // Start and end in different buffers.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
// If edits have been made to this buffer, the delta between the range may
|
|
|
|
// have changed.
|
2009-01-17 14:22:33 +08:00
|
|
|
std::map<FileID, RewriteBuffer>::const_iterator I =
|
2008-10-04 07:31:16 +08:00
|
|
|
RewriteBuffers.find(StartFileID);
|
|
|
|
if (I == RewriteBuffers.end()) {
|
|
|
|
// If the buffer hasn't been rewritten, just return the text from the input.
|
|
|
|
const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
// Adjust the end offset to the end of the last token, instead of being the
|
|
|
|
// start of the last token.
|
2019-07-06 10:55:06 +08:00
|
|
|
if (Range.isTokenRange())
|
|
|
|
EndOff +=
|
|
|
|
Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
|
2008-10-04 07:31:16 +08:00
|
|
|
return std::string(Ptr, Ptr+EndOff-StartOff);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
const RewriteBuffer &RB = I->second;
|
|
|
|
EndOff = RB.getMappedOffset(EndOff, true);
|
|
|
|
StartOff = RB.getMappedOffset(StartOff);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
// Adjust the end offset to the end of the last token, instead of being the
|
|
|
|
// start of the last token.
|
2019-07-06 10:55:06 +08:00
|
|
|
if (Range.isTokenRange())
|
|
|
|
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts);
|
2008-10-04 07:31:16 +08:00
|
|
|
|
|
|
|
// Advance the iterators to the right spot, yay for linear time algorithms.
|
|
|
|
RewriteBuffer::iterator Start = RB.begin();
|
|
|
|
std::advance(Start, StartOff);
|
|
|
|
RewriteBuffer::iterator End = Start;
|
|
|
|
std::advance(End, EndOff-StartOff);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 07:31:16 +08:00
|
|
|
return std::string(Start, End);
|
|
|
|
}
|
2007-10-17 06:36:42 +08:00
|
|
|
|
2007-10-13 08:11:23 +08:00
|
|
|
unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID &FID) const {
|
2008-05-29 00:35:02 +08:00
|
|
|
assert(Loc.isValid() && "Invalid location");
|
2018-03-27 08:01:49 +08:00
|
|
|
std::pair<FileID, unsigned> V = SourceMgr->getDecomposedLoc(Loc);
|
2009-01-17 14:22:33 +08:00
|
|
|
FID = V.first;
|
2007-10-13 08:11:23 +08:00
|
|
|
return V.second;
|
|
|
|
}
|
|
|
|
|
2007-10-12 02:38:32 +08:00
|
|
|
/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID.
|
2009-01-17 14:22:33 +08:00
|
|
|
RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
|
|
|
|
std::map<FileID, RewriteBuffer>::iterator I =
|
|
|
|
RewriteBuffers.lower_bound(FID);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (I != RewriteBuffers.end() && I->first == FID)
|
2007-10-12 02:38:32 +08:00
|
|
|
return I->second;
|
2009-01-17 14:22:33 +08:00
|
|
|
I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
2010-03-16 22:14:31 +08:00
|
|
|
I->second.Initialize(MB.begin(), MB.end());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-12 02:38:32 +08:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2007-11-03 01:26:47 +08:00
|
|
|
/// InsertText - Insert the specified string at the specified location in the
|
2008-02-01 03:51:04 +08:00
|
|
|
/// original buffer.
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Rewriter::InsertText(SourceLocation Loc, StringRef Str,
|
2011-06-16 07:02:42 +08:00
|
|
|
bool InsertAfter, bool indentNewLines) {
|
2008-02-01 03:37:57 +08:00
|
|
|
if (!isRewritable(Loc)) return true;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> indentedStr;
|
2011-06-16 07:02:42 +08:00
|
|
|
if (indentNewLines && Str.find('\n') != StringRef::npos) {
|
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
|
|
|
|
|
|
|
unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
|
2020-10-16 22:21:50 +08:00
|
|
|
const SrcMgr::ContentCache *Content =
|
|
|
|
&SourceMgr->getSLocEntry(FID).getFile().getContentCache();
|
2011-06-16 07:02:42 +08:00
|
|
|
unsigned lineOffs = Content->SourceLineCache[lineNo];
|
|
|
|
|
|
|
|
// Find the whitespace at the start of the line.
|
|
|
|
StringRef indentSpace;
|
|
|
|
{
|
|
|
|
unsigned i = lineOffs;
|
2015-07-27 14:35:22 +08:00
|
|
|
while (isWhitespaceExceptNL(MB[i]))
|
2011-06-16 07:02:42 +08:00
|
|
|
++i;
|
|
|
|
indentSpace = MB.substr(lineOffs, i-lineOffs);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<StringRef, 4> lines;
|
2011-06-16 07:02:42 +08:00
|
|
|
Str.split(lines, "\n");
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = lines.size(); i != e; ++i) {
|
|
|
|
indentedStr += lines[i];
|
|
|
|
if (i < e-1) {
|
|
|
|
indentedStr += '\n';
|
|
|
|
indentedStr += indentSpace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Str = indentedStr.str();
|
|
|
|
}
|
|
|
|
|
2009-08-20 03:10:30 +08:00
|
|
|
getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
|
2008-02-01 03:37:57 +08:00
|
|
|
return false;
|
2007-11-03 01:26:47 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) {
|
2011-04-08 02:10:12 +08:00
|
|
|
if (!isRewritable(Loc)) return true;
|
|
|
|
FileID FID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
|
2011-04-13 15:15:11 +08:00
|
|
|
RewriteOptions rangeOpts;
|
|
|
|
rangeOpts.IncludeInsertsAtBeginOfRange = false;
|
|
|
|
StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
|
2011-04-08 02:10:12 +08:00
|
|
|
getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-01 03:51:04 +08:00
|
|
|
/// RemoveText - Remove the specified text region.
|
2011-04-08 02:10:12 +08:00
|
|
|
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
|
2011-04-13 15:15:11 +08:00
|
|
|
RewriteOptions opts) {
|
2008-02-01 03:51:04 +08:00
|
|
|
if (!isRewritable(Start)) return true;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID;
|
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
|
2011-04-13 15:15:11 +08:00
|
|
|
getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
|
2008-02-01 03:51:04 +08:00
|
|
|
return false;
|
2007-10-17 06:51:17 +08:00
|
|
|
}
|
2007-10-12 02:38:32 +08:00
|
|
|
|
2007-10-17 06:51:17 +08:00
|
|
|
/// ReplaceText - This method replaces a range of characters in the input
|
|
|
|
/// buffer with a new string. This is effectively a combined "remove/insert"
|
|
|
|
/// operation.
|
2008-02-01 03:51:04 +08:00
|
|
|
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef NewStr) {
|
2008-02-01 03:51:04 +08:00
|
|
|
if (!isRewritable(Start)) return true;
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID StartFileID;
|
2007-10-13 08:11:23 +08:00
|
|
|
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-20 03:10:30 +08:00
|
|
|
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
|
2008-02-01 03:51:04 +08:00
|
|
|
return false;
|
2007-10-12 02:38:32 +08:00
|
|
|
}
|
2007-10-18 06:35:30 +08:00
|
|
|
|
2011-04-08 02:10:12 +08:00
|
|
|
bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
|
|
|
|
if (!isRewritable(range.getBegin())) return true;
|
|
|
|
if (!isRewritable(range.getEnd())) return true;
|
|
|
|
if (replacementRange.isInvalid()) return true;
|
|
|
|
SourceLocation start = range.getBegin();
|
|
|
|
unsigned origLength = getRangeSize(range);
|
|
|
|
unsigned newLength = getRangeSize(replacementRange);
|
|
|
|
FileID FID;
|
|
|
|
unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
|
|
|
|
FID);
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
2011-04-08 02:10:12 +08:00
|
|
|
return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
|
|
|
|
}
|
|
|
|
|
2011-04-16 09:03:33 +08:00
|
|
|
bool Rewriter::IncreaseIndentation(CharSourceRange range,
|
|
|
|
SourceLocation parentIndent) {
|
2011-06-16 07:02:42 +08:00
|
|
|
if (range.isInvalid()) return true;
|
2011-04-16 09:03:33 +08:00
|
|
|
if (!isRewritable(range.getBegin())) return true;
|
|
|
|
if (!isRewritable(range.getEnd())) return true;
|
|
|
|
if (!isRewritable(parentIndent)) return true;
|
|
|
|
|
|
|
|
FileID StartFileID, EndFileID, parentFileID;
|
|
|
|
unsigned StartOff, EndOff, parentOff;
|
|
|
|
|
|
|
|
StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
|
|
|
|
EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
|
|
|
|
parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
|
|
|
|
|
|
|
|
if (StartFileID != EndFileID || StartFileID != parentFileID)
|
|
|
|
return true;
|
2011-06-16 07:02:42 +08:00
|
|
|
if (StartOff > EndOff)
|
2011-04-16 09:03:33 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
FileID FID = StartFileID;
|
|
|
|
StringRef MB = SourceMgr->getBufferData(FID);
|
|
|
|
|
|
|
|
unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
|
|
|
|
unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
|
|
|
|
unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2020-10-16 22:21:50 +08:00
|
|
|
const SrcMgr::ContentCache *Content =
|
|
|
|
&SourceMgr->getSLocEntry(FID).getFile().getContentCache();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// Find where the lines start.
|
2011-04-16 09:03:33 +08:00
|
|
|
unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
|
|
|
|
unsigned startLineOffs = Content->SourceLineCache[startLineNo];
|
|
|
|
|
|
|
|
// Find the whitespace at the start of each line.
|
2011-06-16 07:02:42 +08:00
|
|
|
StringRef parentSpace, startSpace;
|
2011-04-16 09:03:33 +08:00
|
|
|
{
|
|
|
|
unsigned i = parentLineOffs;
|
2015-07-27 14:35:22 +08:00
|
|
|
while (isWhitespaceExceptNL(MB[i]))
|
2011-04-16 09:03:33 +08:00
|
|
|
++i;
|
|
|
|
parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
|
|
|
|
|
|
|
|
i = startLineOffs;
|
2015-07-27 14:35:22 +08:00
|
|
|
while (isWhitespaceExceptNL(MB[i]))
|
2011-04-16 09:03:33 +08:00
|
|
|
++i;
|
|
|
|
startSpace = MB.substr(startLineOffs, i-startLineOffs);
|
|
|
|
}
|
|
|
|
if (parentSpace.size() >= startSpace.size())
|
|
|
|
return true;
|
|
|
|
if (!startSpace.startswith(parentSpace))
|
|
|
|
return true;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef indent = startSpace.substr(parentSpace.size());
|
2011-04-16 09:03:33 +08:00
|
|
|
|
|
|
|
// Indent the lines between start/end offsets.
|
|
|
|
RewriteBuffer &RB = getEditBuffer(FID);
|
2011-06-16 07:02:42 +08:00
|
|
|
for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
|
|
|
|
unsigned offs = Content->SourceLineCache[lineNo];
|
|
|
|
unsigned i = offs;
|
2015-07-27 14:35:22 +08:00
|
|
|
while (isWhitespaceExceptNL(MB[i]))
|
2011-06-16 07:02:42 +08:00
|
|
|
++i;
|
|
|
|
StringRef origIndent = MB.substr(offs, i-offs);
|
|
|
|
if (origIndent.startswith(startSpace))
|
|
|
|
RB.InsertText(offs, indent, /*InsertAfter=*/false);
|
2011-04-16 09:03:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-23 01:01:35 +08:00
|
|
|
|
2012-12-02 04:58:01 +08:00
|
|
|
namespace {
|
2018-03-27 08:01:49 +08:00
|
|
|
|
2012-05-23 01:01:35 +08:00
|
|
|
// A wrapper for a file stream that atomically overwrites the target.
|
|
|
|
//
|
|
|
|
// Creates a file output stream for a temporary file in the constructor,
|
|
|
|
// which is later accessible via getStream() if ok() return true.
|
|
|
|
// Flushes the stream and moves the temporary file to the target location
|
|
|
|
// in the destructor.
|
|
|
|
class AtomicallyMovedFile {
|
|
|
|
public:
|
|
|
|
AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,
|
|
|
|
bool &AllWritten)
|
2018-03-27 08:01:49 +08:00
|
|
|
: Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {
|
2012-05-23 01:01:35 +08:00
|
|
|
TempFilename = Filename;
|
|
|
|
TempFilename += "-%%%%%%%%";
|
|
|
|
int FD;
|
2015-07-27 14:35:22 +08:00
|
|
|
if (llvm::sys::fs::createUniqueFile(TempFilename, FD, TempFilename)) {
|
2012-05-23 01:01:35 +08:00
|
|
|
AllWritten = false;
|
|
|
|
Diagnostics.Report(clang::diag::err_unable_to_make_temp)
|
|
|
|
<< TempFilename;
|
|
|
|
} else {
|
|
|
|
FileStream.reset(new llvm::raw_fd_ostream(FD, /*shouldClose=*/true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AtomicallyMovedFile() {
|
|
|
|
if (!ok()) return;
|
|
|
|
|
2015-07-27 14:35:22 +08:00
|
|
|
// Close (will also flush) theFileStream.
|
|
|
|
FileStream->close();
|
|
|
|
if (std::error_code ec = llvm::sys::fs::rename(TempFilename, Filename)) {
|
2012-05-23 01:01:35 +08:00
|
|
|
AllWritten = false;
|
|
|
|
Diagnostics.Report(clang::diag::err_unable_to_rename_temp)
|
|
|
|
<< TempFilename << Filename << ec.message();
|
|
|
|
// If the remove fails, there's not a lot we can do - this is already an
|
|
|
|
// error.
|
2015-07-27 14:35:22 +08:00
|
|
|
llvm::sys::fs::remove(TempFilename);
|
2012-05-23 01:01:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 03:51:06 +08:00
|
|
|
bool ok() { return (bool)FileStream; }
|
2013-01-13 03:30:44 +08:00
|
|
|
raw_ostream &getStream() { return *FileStream; }
|
2012-05-23 01:01:35 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
DiagnosticsEngine &Diagnostics;
|
|
|
|
StringRef Filename;
|
|
|
|
SmallString<128> TempFilename;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<llvm::raw_fd_ostream> FileStream;
|
2012-05-23 01:01:35 +08:00
|
|
|
bool &AllWritten;
|
|
|
|
};
|
2018-03-27 08:01:49 +08:00
|
|
|
|
|
|
|
} // namespace
|
2012-05-23 01:01:35 +08:00
|
|
|
|
|
|
|
bool Rewriter::overwriteChangedFiles() {
|
|
|
|
bool AllWritten = true;
|
|
|
|
for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
|
|
|
|
const FileEntry *Entry =
|
|
|
|
getSourceMgr().getFileEntryForID(I->first);
|
|
|
|
AtomicallyMovedFile File(getSourceMgr().getDiagnostics(), Entry->getName(),
|
|
|
|
AllWritten);
|
|
|
|
if (File.ok()) {
|
|
|
|
I->second.write(File.getStream());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !AllWritten;
|
|
|
|
}
|