#include "clang/Tooling/Core/Replacement.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Core/RewriteBuffer.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <limits>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace clang;
using namespace tooling;
static const char * const InvalidLocation = "";
Replacement::Replacement() : FilePath(InvalidLocation) {}
Replacement::Replacement(StringRef FilePath, unsigned Offset, unsigned Length,
StringRef ReplacementText)
: FilePath(std::string(FilePath)), ReplacementRange(Offset, Length),
ReplacementText(std::string(ReplacementText)) {}
Replacement::Replacement(const SourceManager &Sources, SourceLocation Start,
unsigned Length, StringRef ReplacementText) {
setFromSourceLocation(Sources, Start, Length, ReplacementText);
}
Replacement::Replacement(const SourceManager &Sources,
const CharSourceRange &Range,
StringRef ReplacementText,
const LangOptions &LangOpts) {
setFromSourceRange(Sources, Range, ReplacementText, LangOpts);
}
bool Replacement::isApplicable() const {
return FilePath != InvalidLocation;
}
bool Replacement::apply(Rewriter &Rewrite) const {
SourceManager &SM = Rewrite.getSourceMgr();
auto Entry = SM.getFileManager().getFile(FilePath);
if (!Entry)
return false;
FileID ID = SM.getOrCreateFileID(*Entry, SrcMgr::C_User);
const SourceLocation Start =
SM.getLocForStartOfFile(ID).
getLocWithOffset(ReplacementRange.getOffset());
bool RewriteSucceeded = !Rewrite.ReplaceText(
Start, ReplacementRange.getLength(), ReplacementText);
assert(RewriteSucceeded);
return RewriteSucceeded;
}
std::string Replacement::toString() const {
std::string Result;
llvm::raw_string_ostream Stream(Result);
Stream << FilePath << ": " << ReplacementRange.getOffset() << ":+"
<< ReplacementRange.getLength() << ":\"" << ReplacementText << "\"";
return Stream.str();
}
namespace clang {
namespace tooling {
bool operator<(const Replacement &LHS, const Replacement &RHS) {
if (LHS.getOffset() != RHS.getOffset())
return LHS.getOffset() < RHS.getOffset();
if (LHS.getLength() != RHS.getLength())
return LHS.getLength() < RHS.getLength();
if (LHS.getFilePath() != RHS.getFilePath())
return LHS.getFilePath() < RHS.getFilePath();
return LHS.getReplacementText() < RHS.getReplacementText();
}
bool operator==(const Replacement &LHS, const Replacement &RHS) {
return LHS.getOffset() == RHS.getOffset() &&
LHS.getLength() == RHS.getLength() &&
LHS.getFilePath() == RHS.getFilePath() &&
LHS.getReplacementText() == RHS.getReplacementText();
}
} }
void Replacement::setFromSourceLocation(const SourceManager &Sources,
SourceLocation Start, unsigned Length,
StringRef ReplacementText) {
const std::pair<FileID, unsigned> DecomposedLocation =
Sources.getDecomposedLoc(Start);
const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first);
this->FilePath = std::string(Entry ? Entry->getName() : InvalidLocation);
this->ReplacementRange = Range(DecomposedLocation.second, Length);
this->ReplacementText = std::string(ReplacementText);
}
static int getRangeSize(const SourceManager &Sources,
const CharSourceRange &Range,
const LangOptions &LangOpts) {
SourceLocation SpellingBegin = Sources.getSpellingLoc(Range.getBegin());
SourceLocation SpellingEnd = Sources.getSpellingLoc(Range.getEnd());
std::pair<FileID, unsigned> Start = Sources.getDecomposedLoc(SpellingBegin);
std::pair<FileID, unsigned> End = Sources.getDecomposedLoc(SpellingEnd);
if (Start.first != End.first) return -1;
if (Range.isTokenRange())
End.second += Lexer::MeasureTokenLength(SpellingEnd, Sources, LangOpts);
return End.second - Start.second;
}
void Replacement::setFromSourceRange(const SourceManager &Sources,
const CharSourceRange &Range,
StringRef ReplacementText,
const LangOptions &LangOpts) {
setFromSourceLocation(Sources, Sources.getSpellingLoc(Range.getBegin()),
getRangeSize(Sources, Range, LangOpts),
ReplacementText);
}
Replacement
Replacements::getReplacementInChangedCode(const Replacement &R) const {
unsigned NewStart = getShiftedCodePosition(R.getOffset());
unsigned NewEnd = getShiftedCodePosition(R.getOffset() + R.getLength());
return Replacement(R.getFilePath(), NewStart, NewEnd - NewStart,
R.getReplacementText());
}
static std::string getReplacementErrString(replacement_error Err) {
switch (Err) {
case replacement_error::fail_to_apply:
return "Failed to apply a replacement.";
case replacement_error::wrong_file_path:
return "The new replacement's file path is different from the file path of "
"existing replacements";
case replacement_error::overlap_conflict:
return "The new replacement overlaps with an existing replacement.";
case replacement_error::insert_conflict:
return "The new insertion has the same insert location as an existing "
"replacement.";
}
llvm_unreachable("A value of replacement_error has no message.");
}
std::string ReplacementError::message() const {
std::string Message = getReplacementErrString(Err);
if (NewReplacement)
Message += "\nNew replacement: " + NewReplacement->toString();
if (ExistingReplacement)
Message += "\nExisting replacement: " + ExistingReplacement->toString();
return Message;
}
char ReplacementError::ID = 0;
Replacements Replacements::getCanonicalReplacements() const {
std::vector<Replacement> NewReplaces;
for (const auto &R : Replaces) {
if (NewReplaces.empty()) {
NewReplaces.push_back(R);
continue;
}
auto &Prev = NewReplaces.back();
unsigned PrevEnd = Prev.getOffset() + Prev.getLength();
if (PrevEnd < R.getOffset()) {
NewReplaces.push_back(R);
} else {
assert(PrevEnd == R.getOffset() &&
"Existing replacements must not overlap.");
Replacement NewR(
R.getFilePath(), Prev.getOffset(), Prev.getLength() + R.getLength(),
(Prev.getReplacementText() + R.getReplacementText()).str());
Prev = NewR;
}
}
ReplacementsImpl NewReplacesImpl(NewReplaces.begin(), NewReplaces.end());
return Replacements(NewReplacesImpl.begin(), NewReplacesImpl.end());
}
llvm::Expected<Replacements>
Replacements::mergeIfOrderIndependent(const Replacement &R) const {
Replacements Rs(R);
Replacements RsShiftedByReplaces(getReplacementInChangedCode(R));
Replacements ReplacesShiftedByRs;
for (const auto &Replace : Replaces)
ReplacesShiftedByRs.Replaces.insert(
Rs.getReplacementInChangedCode(Replace));
auto MergeShiftedRs = merge(RsShiftedByReplaces);
auto MergeShiftedReplaces = Rs.merge(ReplacesShiftedByRs);
if (MergeShiftedRs.getCanonicalReplacements() ==
MergeShiftedReplaces.getCanonicalReplacements())
return MergeShiftedRs;
return llvm::make_error<ReplacementError>(replacement_error::overlap_conflict,
R, *Replaces.begin());
}
llvm::Error Replacements::add(const Replacement &R) {
if (!Replaces.empty() && R.getFilePath() != Replaces.begin()->getFilePath())
return llvm::make_error<ReplacementError>(
replacement_error::wrong_file_path, R, *Replaces.begin());
if (R.getOffset() == std::numeric_limits<unsigned>::max()) {
Replaces.insert(R);
return llvm::Error::success();
}
Replacement AtEnd(R.getFilePath(), R.getOffset() + R.getLength(), 0, "");
auto I = Replaces.lower_bound(AtEnd);
if (I != Replaces.end() && R.getOffset() == I->getOffset()) {
assert(R.getLength() == 0);
if (I->getLength() == 0) {
if ((R.getReplacementText() + I->getReplacementText()).str() !=
(I->getReplacementText() + R.getReplacementText()).str())
return llvm::make_error<ReplacementError>(
replacement_error::insert_conflict, R, *I);
Replacement NewR(
R.getFilePath(), R.getOffset(), 0,
(R.getReplacementText() + I->getReplacementText()).str());
Replaces.erase(I);
Replaces.insert(std::move(NewR));
return llvm::Error::success();
}
Replaces.insert(R);
return llvm::Error::success();
}
if (I == Replaces.begin()) {
Replaces.insert(R);
return llvm::Error::success();
}
--I;
auto Overlap = [](const Replacement &R1, const Replacement &R2) -> bool {
return Range(R1.getOffset(), R1.getLength())
.overlapsWith(Range(R2.getOffset(), R2.getLength()));
};
if (!Overlap(R, *I)) {
Replaces.insert(R);
} else {
auto MergeBegin = I;
auto MergeEnd = std::next(I);
while (I != Replaces.begin()) {
--I;
if (!Overlap(R, *I))
break;
MergeBegin = I;
}
Replacements OverlapReplaces(MergeBegin, MergeEnd);
llvm::Expected<Replacements> Merged =
OverlapReplaces.mergeIfOrderIndependent(R);
if (!Merged)
return Merged.takeError();
Replaces.erase(MergeBegin, MergeEnd);
Replaces.insert(Merged->begin(), Merged->end());
}
return llvm::Error::success();
}
namespace {
class MergedReplacement {
public:
MergedReplacement(const Replacement &R, bool MergeSecond, int D)
: MergeSecond(MergeSecond), Delta(D), FilePath(R.getFilePath()),
Offset(R.getOffset() + (MergeSecond ? 0 : Delta)),
Length(R.getLength()), Text(std::string(R.getReplacementText())) {
Delta += MergeSecond ? 0 : Text.size() - Length;
DeltaFirst = MergeSecond ? Text.size() - Length : 0;
}
void merge(const Replacement &R) {
if (MergeSecond) {
unsigned REnd = R.getOffset() + Delta + R.getLength();
unsigned End = Offset + Text.size();
if (REnd > End) {
Length += REnd - End;
MergeSecond = false;
}
StringRef TextRef = Text;
StringRef Head = TextRef.substr(0, R.getOffset() + Delta - Offset);
StringRef Tail = TextRef.substr(REnd - Offset);
Text = (Head + R.getReplacementText() + Tail).str();
Delta += R.getReplacementText().size() - R.getLength();
} else {
unsigned End = Offset + Length;
StringRef RText = R.getReplacementText();
StringRef Tail = RText.substr(End - R.getOffset());
Text = (Text + Tail).str();
if (R.getOffset() + RText.size() > End) {
Length = R.getOffset() + R.getLength() - Offset;
MergeSecond = true;
} else {
Length += R.getLength() - RText.size();
}
DeltaFirst += RText.size() - R.getLength();
}
}
bool endsBefore(const Replacement &R) const {
if (MergeSecond)
return Offset + Text.size() < R.getOffset() + Delta;
return Offset + Length < R.getOffset();
}
bool mergeSecond() const { return MergeSecond; }
int deltaFirst() const { return DeltaFirst; }
Replacement asReplacement() const { return {FilePath, Offset, Length, Text}; }
private:
bool MergeSecond;
int Delta;
int DeltaFirst;
const StringRef FilePath;
const unsigned Offset;
unsigned Length;
std::string Text;
};
}
Replacements Replacements::merge(const Replacements &ReplacesToMerge) const {
if (empty() || ReplacesToMerge.empty())
return empty() ? ReplacesToMerge : *this;
auto &First = Replaces;
auto &Second = ReplacesToMerge.Replaces;
int Delta = 0;
ReplacementsImpl Result;
for (auto FirstI = First.begin(), SecondI = Second.begin();
FirstI != First.end() || SecondI != Second.end();) {
bool NextIsFirst = SecondI == Second.end() ||
(FirstI != First.end() &&
FirstI->getOffset() < SecondI->getOffset() + Delta);
MergedReplacement Merged(NextIsFirst ? *FirstI : *SecondI, NextIsFirst,
Delta);
++(NextIsFirst ? FirstI : SecondI);
while ((Merged.mergeSecond() && SecondI != Second.end()) ||
(!Merged.mergeSecond() && FirstI != First.end())) {
auto &I = Merged.mergeSecond() ? SecondI : FirstI;
if (Merged.endsBefore(*I))
break;
Merged.merge(*I);
++I;
}
Delta -= Merged.deltaFirst();
Result.insert(Merged.asReplacement());
}
return Replacements(Result.begin(), Result.end());
}
static std::vector<Range> combineAndSortRanges(std::vector<Range> Ranges) {
llvm::sort(Ranges, [](const Range &LHS, const Range &RHS) {
if (LHS.getOffset() != RHS.getOffset())
return LHS.getOffset() < RHS.getOffset();
return LHS.getLength() < RHS.getLength();
});
std::vector<Range> Result;
for (const auto &R : Ranges) {
if (Result.empty() ||
Result.back().getOffset() + Result.back().getLength() < R.getOffset()) {
Result.push_back(R);
} else {
unsigned NewEnd =
std::max(Result.back().getOffset() + Result.back().getLength(),
R.getOffset() + R.getLength());
Result[Result.size() - 1] =
Range(Result.back().getOffset(), NewEnd - Result.back().getOffset());
}
}
return Result;
}
namespace clang {
namespace tooling {
std::vector<Range>
calculateRangesAfterReplacements(const Replacements &Replaces,
const std::vector<Range> &Ranges) {
auto MergedRanges = combineAndSortRanges(Ranges);
if (Replaces.empty())
return MergedRanges;
tooling::Replacements FakeReplaces;
for (const auto &R : MergedRanges) {
llvm::cantFail(
FakeReplaces.add(Replacement(Replaces.begin()->getFilePath(),
R.getOffset(), R.getLength(),
std::string(R.getLength(), ' '))),
"Replacements must not conflict since ranges have been merged.");
}
return FakeReplaces.merge(Replaces).getAffectedRanges();
}
} }
std::vector<Range> Replacements::getAffectedRanges() const {
std::vector<Range> ChangedRanges;
int Shift = 0;
for (const auto &R : Replaces) {
unsigned Offset = R.getOffset() + Shift;
unsigned Length = R.getReplacementText().size();
Shift += Length - R.getLength();
ChangedRanges.push_back(Range(Offset, Length));
}
return combineAndSortRanges(ChangedRanges);
}
unsigned Replacements::getShiftedCodePosition(unsigned Position) const {
unsigned Offset = 0;
for (const auto &R : Replaces) {
if (R.getOffset() + R.getLength() <= Position) {
Offset += R.getReplacementText().size() - R.getLength();
continue;
}
if (R.getOffset() < Position &&
R.getOffset() + R.getReplacementText().size() <= Position) {
Position = R.getOffset() + R.getReplacementText().size();
if (!R.getReplacementText().empty())
Position--;
}
break;
}
return Position + Offset;
}
namespace clang {
namespace tooling {
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) {
bool Result = true;
for (auto I = Replaces.rbegin(), E = Replaces.rend(); I != E; ++I) {
if (I->isApplicable()) {
Result = I->apply(Rewrite) && Result;
} else {
Result = false;
}
}
return Result;
}
llvm::Expected<std::string> applyAllReplacements(StringRef Code,
const Replacements &Replaces) {
if (Replaces.empty())
return Code.str();
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
new llvm::vfs::InMemoryFileSystem);
FileManager Files(FileSystemOptions(), InMemoryFileSystem);
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions);
SourceManager SourceMgr(Diagnostics, Files);
Rewriter Rewrite(SourceMgr, LangOptions());
InMemoryFileSystem->addFile(
"<stdin>", 0, llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"));
FileID ID = SourceMgr.createFileID(*Files.getOptionalFileRef("<stdin>"),
SourceLocation(),
clang::SrcMgr::C_User);
for (auto I = Replaces.rbegin(), E = Replaces.rend(); I != E; ++I) {
Replacement Replace("<stdin>", I->getOffset(), I->getLength(),
I->getReplacementText());
if (!Replace.apply(Rewrite))
return llvm::make_error<ReplacementError>(
replacement_error::fail_to_apply, Replace);
}
std::string Result;
llvm::raw_string_ostream OS(Result);
Rewrite.getEditBuffer(ID).write(OS);
OS.flush();
return Result;
}
std::map<std::string, Replacements> groupReplacementsByFile(
FileManager &FileMgr,
const std::map<std::string, Replacements> &FileToReplaces) {
std::map<std::string, Replacements> Result;
llvm::SmallPtrSet<const FileEntry *, 16> ProcessedFileEntries;
for (const auto &Entry : FileToReplaces) {
auto FE = FileMgr.getFile(Entry.first);
if (!FE)
llvm::errs() << "File path " << Entry.first << " is invalid.\n";
else if (ProcessedFileEntries.insert(*FE).second)
Result[Entry.first] = std::move(Entry.second);
}
return Result;
}
} }