Changeset View
Changeset View
Standalone View
Standalone View
lib/Tooling/Core/Replacement.cpp
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | bool Replacement::isApplicable() const { | ||||
return FilePath != InvalidLocation; | return FilePath != InvalidLocation; | ||||
} | } | ||||
bool Replacement::apply(Rewriter &Rewrite) const { | bool Replacement::apply(Rewriter &Rewrite) const { | ||||
SourceManager &SM = Rewrite.getSourceMgr(); | SourceManager &SM = Rewrite.getSourceMgr(); | ||||
const FileEntry *Entry = SM.getFileManager().getFile(FilePath); | const FileEntry *Entry = SM.getFileManager().getFile(FilePath); | ||||
if (!Entry) | if (!Entry) | ||||
return false; | return false; | ||||
FileID ID; | |||||
// FIXME: Use SM.translateFile directly. | FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User); | ||||
SourceLocation Location = SM.translateFileLineCol(Entry, 1, 1); | |||||
ID = Location.isValid() ? | |||||
SM.getFileID(Location) : | |||||
SM.createFileID(Entry, SourceLocation(), SrcMgr::C_User); | |||||
// FIXME: We cannot check whether Offset + Length is in the file, as | |||||
// the remapping API is not public in the RewriteBuffer. | |||||
const SourceLocation Start = | const SourceLocation Start = | ||||
SM.getLocForStartOfFile(ID). | SM.getLocForStartOfFile(ID). | ||||
getLocWithOffset(ReplacementRange.getOffset()); | getLocWithOffset(ReplacementRange.getOffset()); | ||||
// ReplaceText returns false on success. | // ReplaceText returns false on success. | ||||
// ReplaceText only fails if the source location is not a file location, in | // ReplaceText only fails if the source location is not a file location, in | ||||
// which case we already returned false earlier. | // which case we already returned false earlier. | ||||
bool RewriteSucceeded = !Rewrite.ReplaceText( | bool RewriteSucceeded = !Rewrite.ReplaceText( | ||||
Start, ReplacementRange.getLength(), ReplacementText); | Start, ReplacementRange.getLength(), ReplacementText); | ||||
▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Lines | for (std::vector<Replacement>::const_iterator I = Replaces.begin(), | ||||
} else { | } else { | ||||
Result = false; | Result = false; | ||||
} | } | ||||
} | } | ||||
return Result; | return Result; | ||||
} | } | ||||
std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { | std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { | ||||
if (Replaces.empty()) return Code; | |||||
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( | ||||
new vfs::InMemoryFileSystem); | new vfs::InMemoryFileSystem); | ||||
FileManager Files(FileSystemOptions(), InMemoryFileSystem); | FileManager Files(FileSystemOptions(), InMemoryFileSystem); | ||||
DiagnosticsEngine Diagnostics( | DiagnosticsEngine Diagnostics( | ||||
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), | ||||
new DiagnosticOptions); | new DiagnosticOptions); | ||||
SourceManager SourceMgr(Diagnostics, Files); | SourceManager SourceMgr(Diagnostics, Files); | ||||
Rewriter Rewrite(SourceMgr, LangOptions()); | Rewriter Rewrite(SourceMgr, LangOptions()); | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | private: | ||||
// as the element is only extended to the right. | // as the element is only extended to the right. | ||||
const StringRef FilePath; | const StringRef FilePath; | ||||
const unsigned Offset; | const unsigned Offset; | ||||
unsigned Length; | unsigned Length; | ||||
std::string Text; | std::string Text; | ||||
}; | }; | ||||
} // namespace | } // namespace | ||||
FileToReplacementsMap | |||||
groupReplacementsByFile(const Replacements &Replaces, FileManager &Files) { | |||||
FileToReplacementsMap FileToReplaces; | |||||
for (const auto &Replace : Replaces) { | |||||
const FileEntry *Entry = Files.getFile(Replace.getFilePath()); | |||||
assert(Entry && "Expected an existing file."); | |||||
FileToReplaces[Entry].insert(Replace); | |||||
} | |||||
return FileToReplaces; | |||||
} | |||||
Replacements mergeReplacements(const Replacements &First, | Replacements mergeReplacements(const Replacements &First, | ||||
const Replacements &Second) { | const Replacements &Second) { | ||||
if (First.empty() || Second.empty()) | if (First.empty() || Second.empty()) | ||||
return First.empty() ? Second : First; | return First.empty() ? Second : First; | ||||
// Delta is the amount of characters that replacements from 'Second' need to | // Delta is the amount of characters that replacements from 'Second' need to | ||||
// be shifted so that their offsets refer to the original text. | // be shifted so that their offsets refer to the original text. | ||||
int Delta = 0; | int Delta = 0; | ||||
Show All 32 Lines |