| 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 |
|
| 14 |
#include "clang/Rewrite/Core/Rewriter.h" |
| 15 |
#include "clang/Basic/Diagnostic.h" |
| 16 |
#include "clang/Basic/DiagnosticIDs.h" |
| 17 |
#include "clang/Basic/SourceLocation.h" |
| 18 |
#include "clang/Basic/SourceManager.h" |
| 19 |
#include "clang/Lex/Lexer.h" |
| 20 |
#include "llvm/ADT/RewriteBuffer.h" |
| 21 |
#include "llvm/ADT/RewriteRope.h" |
| 22 |
#include "llvm/ADT/SmallVector.h" |
| 23 |
#include "llvm/ADT/StringRef.h" |
| 24 |
#include "llvm/Support/Error.h" |
| 25 |
#include "llvm/Support/IOSandbox.h" |
| 26 |
#include "llvm/Support/raw_ostream.h" |
| 27 |
#include <cassert> |
| 28 |
#include <iterator> |
| 29 |
#include <map> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
using namespace clang; |
| 33 |
using llvm::RewriteBuffer; |
| 34 |
|
| 35 |
//===----------------------------------------------------------------------===// |
| 36 |
// Rewriter class |
| 37 |
//===----------------------------------------------------------------------===// |
| 38 |
|
| 39 |
/// Return true if this character is non-new-line whitespace: |
| 40 |
/// ' ', '\\t', '\\f', '\\v', '\\r'. |
| 41 |
static inline bool isWhitespaceExceptNL(unsigned char c) { |
| 42 |
return c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r'; |
| 43 |
} |
| 44 |
|
| 45 |
/// getRangeSize - Return the size in bytes of the specified range if they |
| 46 |
/// are in the same file. If not, this returns -1. |
| 47 |
int Rewriter::getRangeSize(const CharSourceRange &Range, |
| 48 |
RewriteOptions opts) const { |
| 49 |
if (!isRewritable(Range.getBegin()) || |
| 50 |
!isRewritable(Range.getEnd())) return -1; |
| 51 |
|
| 52 |
FileID StartFileID, EndFileID; |
| 53 |
unsigned StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); |
| 54 |
unsigned EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); |
| 55 |
|
| 56 |
if (StartFileID != EndFileID) |
| 57 |
return -1; |
| 58 |
|
| 59 |
// If edits have been made to this buffer, the delta between the range may |
| 60 |
// have changed. |
| 61 |
std::map<FileID, RewriteBuffer>::const_iterator I = |
| 62 |
RewriteBuffers.find(StartFileID); |
| 63 |
if (I != RewriteBuffers.end()) { |
| 64 |
const RewriteBuffer &RB = I->second; |
| 65 |
EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange); |
| 66 |
StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange); |
| 67 |
} |
| 68 |
|
| 69 |
// Adjust the end offset to the end of the last token, instead of being the |
| 70 |
// start of the last token if this is a token range. |
| 71 |
if (Range.isTokenRange()) |
| 72 |
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); |
| 73 |
|
| 74 |
return EndOff-StartOff; |
| 75 |
} |
| 76 |
|
| 77 |
int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const { |
| 78 |
return getRangeSize(CharSourceRange::getTokenRange(Range), opts); |
| 79 |
} |
| 80 |
|
| 81 |
/// getRewrittenText - Return the rewritten form of the text in the specified |
| 82 |
/// range. If the start or end of the range was unrewritable or if they are |
| 83 |
/// in different buffers, this returns an empty string. |
| 84 |
/// |
| 85 |
/// Note that this method is not particularly efficient. |
| 86 |
std::string Rewriter::getRewrittenText(CharSourceRange Range) const { |
| 87 |
if (!isRewritable(Range.getBegin()) || |
| 88 |
!isRewritable(Range.getEnd())) |
| 89 |
return {}; |
| 90 |
|
| 91 |
FileID StartFileID, EndFileID; |
| 92 |
unsigned StartOff, EndOff; |
| 93 |
StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); |
| 94 |
EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); |
| 95 |
|
| 96 |
if (StartFileID != EndFileID) |
| 97 |
return {}; // Start and end in different buffers. |
| 98 |
|
| 99 |
// If edits have been made to this buffer, the delta between the range may |
| 100 |
// have changed. |
| 101 |
std::map<FileID, RewriteBuffer>::const_iterator I = |
| 102 |
RewriteBuffers.find(StartFileID); |
| 103 |
if (I == RewriteBuffers.end()) { |
| 104 |
// If the buffer hasn't been rewritten, just return the text from the input. |
| 105 |
const char *Ptr = SourceMgr->getCharacterData(Range.getBegin()); |
| 106 |
|
| 107 |
// Adjust the end offset to the end of the last token, instead of being the |
| 108 |
// start of the last token. |
| 109 |
if (Range.isTokenRange()) |
| 110 |
EndOff += |
| 111 |
Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); |
| 112 |
return std::string(Ptr, Ptr+EndOff-StartOff); |
| 113 |
} |
| 114 |
|
| 115 |
const RewriteBuffer &RB = I->second; |
| 116 |
EndOff = RB.getMappedOffset(EndOff, true); |
| 117 |
StartOff = RB.getMappedOffset(StartOff); |
| 118 |
|
| 119 |
// Adjust the end offset to the end of the last token, instead of being the |
| 120 |
// start of the last token. |
| 121 |
if (Range.isTokenRange()) |
| 122 |
EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); |
| 123 |
|
| 124 |
// Advance the iterators to the right spot, yay for linear time algorithms. |
| 125 |
RewriteBuffer::iterator Start = RB.begin(); |
| 126 |
std::advance(Start, StartOff); |
| 127 |
RewriteBuffer::iterator End = Start; |
| 128 |
assert(EndOff >= StartOff && "Invalid iteration distance"); |
| 129 |
std::advance(End, EndOff-StartOff); |
| 130 |
|
| 131 |
return std::string(Start, End); |
| 132 |
} |
| 133 |
|
| 134 |
unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, |
| 135 |
FileID &FID) const { |
| 136 |
assert(Loc.isValid() && "Invalid location"); |
| 137 |
FileIDAndOffset V = SourceMgr->getDecomposedLoc(Loc); |
| 138 |
FID = V.first; |
| 139 |
return V.second; |
| 140 |
} |
| 141 |
|
| 142 |
/// getEditBuffer - Get or create a RewriteBuffer for the specified FileID. |
| 143 |
RewriteBuffer &Rewriter::getEditBuffer(FileID FID) { |
| 144 |
std::map<FileID, RewriteBuffer>::iterator I = |
| 145 |
RewriteBuffers.lower_bound(FID); |
| 146 |
if (I != RewriteBuffers.end() && I->first == FID) |
| 147 |
return I->second; |
| 148 |
I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer())); |
| 149 |
|
| 150 |
StringRef MB = SourceMgr->getBufferData(FID); |
| 151 |
I->second.Initialize(MB.begin(), MB.end()); |
| 152 |
|
| 153 |
return I->second; |
| 154 |
} |
| 155 |
|
| 156 |
/// InsertText - Insert the specified string at the specified location in the |
| 157 |
/// original buffer. |
| 158 |
bool Rewriter::InsertText(SourceLocation Loc, StringRef Str, |
| 159 |
bool InsertAfter, bool indentNewLines) { |
| 160 |
if (!isRewritable(Loc)) return true; |
| 161 |
FileID FID; |
| 162 |
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID); |
| 163 |
|
| 164 |
SmallString<128> indentedStr; |
| 165 |
if (indentNewLines && Str.contains('\n')) { |
| 166 |
StringRef MB = SourceMgr->getBufferData(FID); |
| 167 |
|
| 168 |
unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1; |
| 169 |
const SrcMgr::ContentCache *Content = |
| 170 |
&SourceMgr->getSLocEntry(FID).getFile().getContentCache(); |
| 171 |
unsigned lineOffs = Content->SourceLineCache[lineNo]; |
| 172 |
|
| 173 |
// Find the whitespace at the start of the line. |
| 174 |
StringRef indentSpace; |
| 175 |
{ |
| 176 |
unsigned i = lineOffs; |
| 177 |
while (isWhitespaceExceptNL(MB[i])) |
| 178 |
++i; |
| 179 |
indentSpace = MB.substr(lineOffs, i-lineOffs); |
| 180 |
} |
| 181 |
|
| 182 |
SmallVector<StringRef, 4> lines; |
| 183 |
Str.split(lines, "\n"); |
| 184 |
|
| 185 |
for (unsigned i = 0, e = lines.size(); i != e; ++i) { |
| 186 |
indentedStr += lines[i]; |
| 187 |
if (i < e-1) { |
| 188 |
indentedStr += '\n'; |
| 189 |
indentedStr += indentSpace; |
| 190 |
} |
| 191 |
} |
| 192 |
Str = indentedStr.str(); |
| 193 |
} |
| 194 |
|
| 195 |
getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter); |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
bool Rewriter::InsertTextAfterToken(SourceLocation Loc, StringRef Str) { |
| 200 |
if (!isRewritable(Loc)) return true; |
| 201 |
FileID FID; |
| 202 |
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID); |
| 203 |
RewriteOptions rangeOpts; |
| 204 |
rangeOpts.IncludeInsertsAtBeginOfRange = false; |
| 205 |
StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts); |
| 206 |
getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true); |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
/// RemoveText - Remove the specified text region. |
| 211 |
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length, |
| 212 |
RewriteOptions opts) { |
| 213 |
if (!isRewritable(Start)) return true; |
| 214 |
FileID FID; |
| 215 |
unsigned StartOffs = getLocationOffsetAndFileID(Start, FID); |
| 216 |
getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty); |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
/// ReplaceText - This method replaces a range of characters in the input |
| 221 |
/// buffer with a new string. This is effectively a combined "remove/insert" |
| 222 |
/// operation. |
| 223 |
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 224 |
StringRef NewStr) { |
| 225 |
if (!isRewritable(Start)) return true; |
| 226 |
FileID StartFileID; |
| 227 |
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID); |
| 228 |
|
| 229 |
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr); |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) { |
| 234 |
if (!isRewritable(range.getBegin())) return true; |
| 235 |
if (!isRewritable(range.getEnd())) return true; |
| 236 |
if (replacementRange.isInvalid()) return true; |
| 237 |
SourceLocation start = range.getBegin(); |
| 238 |
unsigned origLength = getRangeSize(range); |
| 239 |
unsigned newLength = getRangeSize(replacementRange); |
| 240 |
FileID FID; |
| 241 |
unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(), |
| 242 |
FID); |
| 243 |
StringRef MB = SourceMgr->getBufferData(FID); |
| 244 |
return ReplaceText(start, origLength, MB.substr(newOffs, newLength)); |
| 245 |
} |
| 246 |
|
| 247 |
bool Rewriter::IncreaseIndentation(CharSourceRange range, |
| 248 |
SourceLocation parentIndent) { |
| 249 |
if (range.isInvalid()) return true; |
| 250 |
if (!isRewritable(range.getBegin())) return true; |
| 251 |
if (!isRewritable(range.getEnd())) return true; |
| 252 |
if (!isRewritable(parentIndent)) return true; |
| 253 |
|
| 254 |
FileID StartFileID, EndFileID, parentFileID; |
| 255 |
unsigned StartOff, EndOff, parentOff; |
| 256 |
|
| 257 |
StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID); |
| 258 |
EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID); |
| 259 |
parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID); |
| 260 |
|
| 261 |
if (StartFileID != EndFileID || StartFileID != parentFileID) |
| 262 |
return true; |
| 263 |
if (StartOff > EndOff) |
| 264 |
return true; |
| 265 |
|
| 266 |
FileID FID = StartFileID; |
| 267 |
StringRef MB = SourceMgr->getBufferData(FID); |
| 268 |
|
| 269 |
unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1; |
| 270 |
unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1; |
| 271 |
unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1; |
| 272 |
|
| 273 |
const SrcMgr::ContentCache *Content = |
| 274 |
&SourceMgr->getSLocEntry(FID).getFile().getContentCache(); |
| 275 |
|
| 276 |
// Find where the lines start. |
| 277 |
unsigned parentLineOffs = Content->SourceLineCache[parentLineNo]; |
| 278 |
unsigned startLineOffs = Content->SourceLineCache[startLineNo]; |
| 279 |
|
| 280 |
// Find the whitespace at the start of each line. |
| 281 |
StringRef parentSpace, startSpace; |
| 282 |
{ |
| 283 |
unsigned i = parentLineOffs; |
| 284 |
while (isWhitespaceExceptNL(MB[i])) |
| 285 |
++i; |
| 286 |
parentSpace = MB.substr(parentLineOffs, i-parentLineOffs); |
| 287 |
|
| 288 |
i = startLineOffs; |
| 289 |
while (isWhitespaceExceptNL(MB[i])) |
| 290 |
++i; |
| 291 |
startSpace = MB.substr(startLineOffs, i-startLineOffs); |
| 292 |
} |
| 293 |
if (parentSpace.size() >= startSpace.size()) |
| 294 |
return true; |
| 295 |
if (!startSpace.starts_with(parentSpace)) |
| 296 |
return true; |
| 297 |
|
| 298 |
StringRef indent = startSpace.substr(parentSpace.size()); |
| 299 |
|
| 300 |
// Indent the lines between start/end offsets. |
| 301 |
RewriteBuffer &RB = getEditBuffer(FID); |
| 302 |
for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) { |
| 303 |
unsigned offs = Content->SourceLineCache[lineNo]; |
| 304 |
unsigned i = offs; |
| 305 |
while (isWhitespaceExceptNL(MB[i])) |
| 306 |
++i; |
| 307 |
StringRef origIndent = MB.substr(offs, i-offs); |
| 308 |
if (origIndent.starts_with(startSpace)) |
| 309 |
RB.InsertText(offs, indent, /*InsertAfter=*/false); |
| 310 |
} |
| 311 |
|
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
bool Rewriter::overwriteChangedFiles() { |
| 316 |
bool AllWritten = true; |
| 317 |
auto& Diag = getSourceMgr().getDiagnostics(); |
| 318 |
unsigned OverwriteFailure = Diag.getCustomDiagID( |
| 319 |
DiagnosticsEngine::Error, "unable to overwrite file %0: %1"); |
| 320 |
for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) { |
| 321 |
OptionalFileEntryRef Entry = getSourceMgr().getFileEntryRefForID(I->first); |
| 322 |
llvm::SmallString<128> Path(Entry->getName()); |
| 323 |
getSourceMgr().getFileManager().makeAbsolutePath(Path); |
| 324 |
// FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`. |
| 325 |
auto BypassSandbox = llvm::sys::sandbox::scopedDisable(); |
| 326 |
if (auto Error = llvm::writeToOutput(Path, [&](llvm::raw_ostream &OS) { |
| 327 |
I->second.write(OS); |
| 328 |
return llvm::Error::success(); |
| 329 |
})) { |
| 330 |
Diag.Report(OverwriteFailure) |
| 331 |
<< Entry->getName() << llvm::toString(std::move(Error)); |
| 332 |
AllWritten = false; |
| 333 |
} |
| 334 |
} |
| 335 |
return !AllWritten; |
| 336 |
} |