From 78e2a2df22a40a7ed097ab64e4bf70833d115ed3 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 30 Jan 2015 02:11:59 +0000 Subject: [PATCH] Replace SimpleFileWrapper with a function. SimpleFileWrapper was a class to wrap an existing (possibly non-mutable) file as a mutable file. We used instances of the class in RoundTrip* passes, because the passes convert mutable files to non-mutable files, and we needed to convert them back to mutable. That feature can be implemented without defining a new class. Generally speaking, if we can implement a feature without defining a class and using only public interface of exsiting classes, that's preferred way to do that. And this is the case. llvm-svn: 227549 --- lld/include/lld/Core/Simple.h | 26 ++++++++++++-------------- lld/lib/Passes/RoundTripNativePass.cpp | 4 ++-- lld/lib/Passes/RoundTripYAMLPass.cpp | 3 ++- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lld/include/lld/Core/Simple.h b/lld/include/lld/Core/Simple.h index e13c0f4..b1c560d1 100644 --- a/lld/include/lld/Core/Simple.h +++ b/lld/include/lld/Core/Simple.h @@ -25,6 +25,18 @@ namespace lld { +// Copy all atoms from src to dst. Atom ownership is not transferred. +inline void copyAtoms(MutableFile *dst, File *src) { + for (const DefinedAtom *atom : src->defined()) + dst->addAtom(*atom); + for (const UndefinedAtom *atom : src->undefined()) + dst->addAtom(*atom); + for (const SharedLibraryAtom *atom : src->sharedLibrary()) + dst->addAtom(*atom); + for (const AbsoluteAtom *atom : src->absolute()) + dst->addAtom(*atom); +} + class SimpleFile : public MutableFile { public: SimpleFile(StringRef path) : MutableFile(path) {} @@ -77,20 +89,6 @@ protected: atom_collection_vector _absoluteAtoms; }; -class SimpleFileWrapper : public SimpleFile { -public: - SimpleFileWrapper(const File &file) : SimpleFile(file.path()) { - for (const DefinedAtom *atom : file.defined()) - _definedAtoms._atoms.push_back(atom); - for (const UndefinedAtom *atom : file.undefined()) - _undefinedAtoms._atoms.push_back(atom); - for (const SharedLibraryAtom *atom : file.sharedLibrary()) - _sharedLibraryAtoms._atoms.push_back(atom); - for (const AbsoluteAtom *atom : file.absolute()) - _absoluteAtoms._atoms.push_back(atom); - } -}; - class SimpleReference : public Reference { public: SimpleReference(Reference::KindNamespace ns, Reference::KindArch arch, diff --git a/lld/lib/Passes/RoundTripNativePass.cpp b/lld/lib/Passes/RoundTripNativePass.cpp index f769a54..2e7e1a3 100644 --- a/lld/lib/Passes/RoundTripNativePass.cpp +++ b/lld/lib/Passes/RoundTripNativePass.cpp @@ -49,7 +49,7 @@ void RoundTripNativePass::perform(std::unique_ptr &mergedFile) { File *objFile = _nativeFile[0].get(); if (objFile->parse()) llvm_unreachable("native reader parse error"); - mergedFile.reset(new SimpleFileWrapper(*objFile)); - + mergedFile.reset(new SimpleFile(objFile->path())); + copyAtoms(mergedFile.get(), objFile); llvm::sys::fs::remove(tmpNativeFile.str()); } diff --git a/lld/lib/Passes/RoundTripYAMLPass.cpp b/lld/lib/Passes/RoundTripYAMLPass.cpp index 6fcfe58..fbd1c1b 100644 --- a/lld/lib/Passes/RoundTripYAMLPass.cpp +++ b/lld/lib/Passes/RoundTripYAMLPass.cpp @@ -49,6 +49,7 @@ void RoundTripYAMLPass::perform(std::unique_ptr &mergedFile) { File *objFile = _yamlFile[0].get(); if (objFile->parse()) llvm_unreachable("native reader parse error"); - mergedFile.reset(new SimpleFileWrapper(*objFile)); + mergedFile.reset(new SimpleFile(objFile->path())); + copyAtoms(mergedFile.get(), objFile); llvm::sys::fs::remove(tmpYAMLFile.str()); } -- 2.7.4