std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { FileManager Files((FileSystemOptions())); DiagnosticsEngine Diagnostics( IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), new DiagnosticOptions); SourceManager SourceMgr(Diagnostics, Files); Rewriter Rewrite(SourceMgr, LangOptions()); std::unique_ptr<llvm::MemoryBuffer> Buf = llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"); const clang::FileEntry *Entry = Files.getVirtualFile("<stdin>", Buf->getBufferSize(), 0); SourceMgr.overrideFileContents(Entry, std::move(Buf)); FileID ID = SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); I != E; ++I) { Replacement Replace("<stdin>", I->getOffset(), I->getLength(), I->getReplacementText()); if (!Replace.apply(Rewrite)) return ""; } std::string Result; llvm::raw_string_ostream OS(Result); Rewrite.getEditBuffer(ID).write(OS); OS.flush(); return Result; }
std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( new 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.getFile("<stdin>"), SourceLocation(), clang::SrcMgr::C_User); for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); I != E; ++I) { Replacement Replace("<stdin>", I->getOffset(), I->getLength(), I->getReplacementText()); if (!Replace.apply(Rewrite)) return ""; } std::string Result; llvm::raw_string_ostream OS(Result); Rewrite.getEditBuffer(ID).write(OS); OS.flush(); return Result; }
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) { bool Result = true; for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); I != E; ++I) { if (I->isApplicable()) { Result = I->apply(Rewrite) && Result; } else { Result = false; } } return Result; }
bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite) { bool Result = true; for(Replacements::iterator B = Replaces.begin(), I = B, E = Replaces.end(); I != E; ++I) { Replacements::iterator S = std::search(I+1,E,I,I+1,Replacement::Equal()); if (S != E) { I = Replaces.erase(I)-1; E = Replaces.end(); } } for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); I != E; ++I) { if (I->isApplicable()) { Result = I->apply(Rewrite) && Result; } else { Result = false; } } return Result; }