[lld] r178914 - Correctly pass ownership of MemoryBuffers.
Michael J. Spencer
bigcheesegs at gmail.com
Fri Apr 5 14:08:30 PDT 2013
Author: mspencer
Date: Fri Apr 5 16:08:30 2013
New Revision: 178914
URL: http://llvm.org/viewvc/llvm-project?rev=178914&view=rev
Log:
Correctly pass ownership of MemoryBuffers.
Modified:
lld/trunk/include/lld/Core/TargetInfo.h
lld/trunk/include/lld/ReaderWriter/CoreTargetInfo.h
lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h
lld/trunk/include/lld/ReaderWriter/MachOTargetInfo.h
lld/trunk/include/lld/ReaderWriter/Reader.h
lld/trunk/include/lld/ReaderWriter/ReaderArchive.h
lld/trunk/include/lld/ReaderWriter/ReaderLinkerScript.h
lld/trunk/lib/Core/TargetInfo.cpp
lld/trunk/lib/ReaderWriter/CoreTargetInfo.cpp
lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp
lld/trunk/lib/ReaderWriter/ELF/Reader.cpp
lld/trunk/lib/ReaderWriter/MachO/MachOTargetInfo.cpp
lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp
lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
lld/trunk/lib/ReaderWriter/Reader.cpp
lld/trunk/lib/ReaderWriter/ReaderArchive.cpp
lld/trunk/lib/ReaderWriter/ReaderLinkerScript.cpp
lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
Modified: lld/trunk/include/lld/Core/TargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/TargetInfo.h?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/TargetInfo.h (original)
+++ lld/trunk/include/lld/Core/TargetInfo.h Fri Apr 5 16:08:30 2013
@@ -260,19 +260,19 @@ public:
/// \param [out] result The instantiated lld::File object is returned here.
/// The \p result is a vector because some input files parse into more than
/// one lld::File (e.g. YAML).
- virtual error_code parseFile(std::unique_ptr<MemoryBuffer> &inputBuff,
- std::vector<std::unique_ptr<File>> &result) const = 0;
-
+ virtual error_code
+ parseFile(std::unique_ptr<MemoryBuffer> inputBuff,
+ std::vector<std::unique_ptr<File>> &result) const = 0;
+
/// This is a wrapper around parseFile() where the input file is specified
/// by file system path. The default implementation reads the input file
/// into a memory buffer and calls parseFile().
///
/// \param path This is the file system path to the input file.
/// \param [out] result The instantiated lld::File object is returned here.
- virtual error_code readFile(StringRef path,
- std::vector<std::unique_ptr<File>> &result) const;
+ virtual error_code
+ readFile(StringRef path, std::vector<std::unique_ptr<File>> &result) const;
-
/// This method is called by core linking to give the Writer a chance
/// to add file format specific "files" to set of files to be linked. This is
/// how file format specific atoms can be added to the link.
Modified: lld/trunk/include/lld/ReaderWriter/CoreTargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/CoreTargetInfo.h?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/CoreTargetInfo.h (original)
+++ lld/trunk/include/lld/ReaderWriter/CoreTargetInfo.h Fri Apr 5 16:08:30 2013
@@ -30,11 +30,10 @@ public:
virtual ErrorOr<Reference::Kind> relocKindFromString(StringRef str) const;
virtual ErrorOr<std::string> stringFromRelocKind(Reference::Kind kind) const;
+ virtual error_code
+ parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const;
- virtual error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const;
-
-
void addPassNamed(StringRef name) {
_passNames.push_back(name);
}
Modified: lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h (original)
+++ lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h Fri Apr 5 16:08:30 2013
@@ -57,10 +57,10 @@ public:
}
virtual bool validate(raw_ostream &diagnostics);
+ virtual error_code
+ parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const;
- virtual error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const;
-
static std::unique_ptr<ELFTargetInfo> create(llvm::Triple);
/// \brief Does this relocation belong in the dynamic plt relocation table?
Modified: lld/trunk/include/lld/ReaderWriter/MachOTargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOTargetInfo.h?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/MachOTargetInfo.h (original)
+++ lld/trunk/include/lld/ReaderWriter/MachOTargetInfo.h Fri Apr 5 16:08:30 2013
@@ -31,9 +31,10 @@ public:
virtual ErrorOr<Reference::Kind> relocKindFromString(StringRef str) const;
virtual ErrorOr<std::string> stringFromRelocKind(Reference::Kind kind) const;
virtual bool validate(raw_ostream &diagnostics);
-
- virtual error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const;
+
+ virtual error_code
+ parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const;
uint32_t getCPUType() const;
uint32_t getCPUSubType() const;
Modified: lld/trunk/include/lld/ReaderWriter/Reader.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/Reader.h?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/Reader.h (original)
+++ lld/trunk/include/lld/ReaderWriter/Reader.h Fri Apr 5 16:08:30 2013
@@ -39,8 +39,9 @@ public:
/// file) and create a File object.
///
/// On success, the resulting File object takes ownership of the MemoryBuffer.
- virtual error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const = 0;
+ virtual error_code
+ parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const = 0;
protected:
// only concrete subclasses can be instantiated
Modified: lld/trunk/include/lld/ReaderWriter/ReaderArchive.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/ReaderArchive.h?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/ReaderArchive.h (original)
+++ lld/trunk/include/lld/ReaderWriter/ReaderArchive.h Fri Apr 5 16:08:30 2013
@@ -35,7 +35,7 @@ public:
/// \brief Returns a vector of Files that are contained in the archive file
/// pointed to by the Memorybuffer
- error_code parseFile(std::unique_ptr<llvm::MemoryBuffer> &mb,
+ error_code parseFile(std::unique_ptr<llvm::MemoryBuffer> mb,
std::vector<std::unique_ptr<File>> &result) const;
private:
Modified: lld/trunk/include/lld/ReaderWriter/ReaderLinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/ReaderLinkerScript.h?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/ReaderLinkerScript.h (original)
+++ lld/trunk/include/lld/ReaderWriter/ReaderLinkerScript.h Fri Apr 5 16:08:30 2013
@@ -26,7 +26,7 @@ public:
/// \brief Returns a vector of Files that are contained in the archive file
/// pointed to by the Memorybuffer
- error_code parseFile(std::unique_ptr<llvm::MemoryBuffer> &mb,
+ error_code parseFile(std::unique_ptr<llvm::MemoryBuffer> mb,
std::vector<std::unique_ptr<File>> &result) const;
};
Modified: lld/trunk/lib/Core/TargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/TargetInfo.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/Core/TargetInfo.cpp (original)
+++ lld/trunk/lib/Core/TargetInfo.cpp Fri Apr 5 16:08:30 2013
@@ -37,9 +37,8 @@ error_code TargetInfo::readFile(StringRe
OwningPtr<llvm::MemoryBuffer> opmb;
if (error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(path, opmb))
return ec;
-
- std::unique_ptr<MemoryBuffer> mb(opmb.take());
- return this->parseFile(mb, result);
+
+ return this->parseFile(std::unique_ptr<MemoryBuffer>(opmb.take()), result);
}
error_code TargetInfo::writeFile(const File &linkedFile) const {
Modified: lld/trunk/lib/ReaderWriter/CoreTargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/CoreTargetInfo.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/CoreTargetInfo.cpp (original)
+++ lld/trunk/lib/ReaderWriter/CoreTargetInfo.cpp Fri Apr 5 16:08:30 2013
@@ -358,12 +358,12 @@ void CoreTargetInfo::addPasses(PassManag
}
}
-
-error_code CoreTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const {
+error_code
+CoreTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const {
if (!_reader)
- _reader = createReaderYAML(*this);
- return _reader->parseFile(mb,result);
+ _reader = createReaderYAML(*this);
+ return _reader->parseFile(std::move(mb), result);
}
Writer &CoreTargetInfo::writer() const {
Modified: lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp Fri Apr 5 16:08:30 2013
@@ -91,28 +91,27 @@ bool ELFTargetInfo::isDynamic() const {
return false;
}
-
-error_code ELFTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const {
+error_code
+ELFTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const {
if (!_elfReader)
_elfReader = createReaderELF(*this);
- error_code ec = _elfReader->parseFile(mb, result);
- if (ec) {
- // Not an ELF file, check file extension to see if it might be yaml
- StringRef path = mb->getBufferIdentifier();
- if ( path.endswith(".objtxt") ) {
- if (!_yamlReader)
- _yamlReader = createReaderYAML(*this);
- ec = _yamlReader->parseFile(mb, result);
- }
- if (ec) {
- // Not a yaml file, assume it is a linkerscript
- if (!_linkerScriptReader)
- _linkerScriptReader.reset(new ReaderLinkerScript(*this));
- ec = _linkerScriptReader->parseFile(mb, result);
- }
+ std::string path = mb->getBufferIdentifier();
+ auto magic = llvm::sys::fs::identify_magic(mb->getBuffer());
+ if (magic == llvm::sys::fs::file_magic::elf_relocatable ||
+ magic == llvm::sys::fs::file_magic::elf_shared_object ||
+ magic == llvm::sys::fs::file_magic::archive)
+ return _elfReader->parseFile(std::move(mb), result);
+ // Not an ELF file, check file extension to see if it might be yaml
+ if (StringRef(path).endswith(".objtxt")) {
+ if (!_yamlReader)
+ _yamlReader = createReaderYAML(*this);
+ return _yamlReader->parseFile(std::move(mb), result);
}
- return ec;
+ // Not a yaml file, assume it is a linkerscript
+ if (!_linkerScriptReader)
+ _linkerScriptReader.reset(new ReaderLinkerScript(*this));
+ return _linkerScriptReader->parseFile(std::move(mb), result);
}
Writer &ELFTargetInfo::writer() const {
Modified: lld/trunk/lib/ReaderWriter/ELF/Reader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Reader.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Reader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Reader.cpp Fri Apr 5 16:08:30 2013
@@ -82,8 +82,8 @@ public:
: lld::Reader(ti), _elfTargetInfo(ti), _readerArchive(ti, *this) {
}
- error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File> > &result) const {
+ error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const {
using llvm::object::ELFType;
llvm::sys::LLVMFileType fileType =
llvm::sys::IdentifyFileType(mb->getBufferStart(),
@@ -96,7 +96,7 @@ public:
switch (fileType) {
case llvm::sys::ELF_Relocatable_FileType: {
std::unique_ptr<File> f(createELF<ELFFileCreateELFTraits>(
- getElfArchType(&*mb), MaxAlignment, _elfTargetInfo, std::move(mb),
+ getElfArchType(mb.get()), MaxAlignment, _elfTargetInfo, std::move(mb),
ec));
if (ec)
return ec;
@@ -105,14 +105,15 @@ public:
}
case llvm::sys::ELF_SharedObject_FileType: {
auto f = createELF<DynamicFileCreateELFTraits>(
- getElfArchType(&*mb), MaxAlignment, _elfTargetInfo, std::move(mb));
+ getElfArchType(mb.get()), MaxAlignment, _elfTargetInfo,
+ std::move(mb));
if (!f)
return f;
result.push_back(std::move(*f));
break;
}
case llvm::sys::Archive_FileType:
- ec = _readerArchive.parseFile(mb, result);
+ ec = _readerArchive.parseFile(std::move(mb), result);
break;
default:
return llvm::make_error_code(llvm::errc::executable_format_error);
Modified: lld/trunk/lib/ReaderWriter/MachO/MachOTargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachOTargetInfo.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachOTargetInfo.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachOTargetInfo.cpp Fri Apr 5 16:08:30 2013
@@ -209,19 +209,18 @@ void MachOTargetInfo::addPasses(PassMana
pm.add(std::unique_ptr<Pass>(new LayoutPass()));
}
-
-
-error_code MachOTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const {
+error_code MachOTargetInfo::parseFile(
+ std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const {
// if (!_machoReader)
// _machoReader = createReaderMachO(*this);
// error_code ec = _machoReader->parseFile(mb,result);
// if (ec) {
if (!_yamlReader)
_yamlReader = createReaderYAML(*this);
- return _yamlReader->parseFile(mb,result);
+ return _yamlReader->parseFile(std::move(mb), result);
// }
-
+
return error_code::success();
}
Modified: lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp (original)
+++ lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp Fri Apr 5 16:08:30 2013
@@ -915,9 +915,9 @@ public:
Reader(const TargetInfo &ti)
: lld::Reader(ti) {}
- virtual error_code parseFile(
- std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<lld::File> > &result) const {
+ virtual error_code
+ parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<lld::File>> &result) const {
return File::make(_targetInfo, mb, mb->getBufferIdentifier(), result);
}
};
Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp Fri Apr 5 16:08:30 2013
@@ -369,8 +369,8 @@ class ReaderCOFF : public Reader {
public:
ReaderCOFF(const TargetInfo &ti) : Reader(ti) {}
- error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File> > &result) const {
+ error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const {
llvm::error_code ec;
std::unique_ptr<File> f(new FileCOFF(_targetInfo, std::move(mb), ec));
if (ec) {
Modified: lld/trunk/lib/ReaderWriter/Reader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/Reader.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/Reader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/Reader.cpp Fri Apr 5 16:08:30 2013
@@ -24,7 +24,6 @@ error_code Reader::readFile(StringRef pa
if (error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(path, opmb))
return ec;
- std::unique_ptr<MemoryBuffer> mb(opmb.take());
- return this->parseFile(mb, result);
+ return parseFile(std::unique_ptr<MemoryBuffer>(opmb.take()), result);
}
} // end namespace lld
Modified: lld/trunk/lib/ReaderWriter/ReaderArchive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ReaderArchive.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ReaderArchive.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ReaderArchive.cpp Fri Apr 5 16:08:30 2013
@@ -48,7 +48,7 @@ public:
if (_targetInfo.logInputFiles())
llvm::outs() << buff->getBufferIdentifier() << "\n";
std::unique_ptr<MemoryBuffer> mb(buff.take());
- if (_targetInfo.parseFile(mb, result))
+ if (_targetInfo.parseFile(std::move(mb), result))
return nullptr;
assert(result.size() == 1);
@@ -159,8 +159,9 @@ public:
// Returns a vector of Files that are contained in the archive file
// pointed to by the MemoryBuffer
-error_code ReaderArchive::parseFile(std::unique_ptr<llvm::MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File>> &result) const {
+error_code
+ReaderArchive::parseFile(std::unique_ptr<llvm::MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const {
error_code ec;
if (_targetInfo.forceLoadAllArchives()) {
@@ -176,7 +177,7 @@ error_code ReaderArchive::parseFile(std:
std::unique_ptr<MemoryBuffer> mbc(buff.take());
if (_targetInfo.logInputFiles())
llvm::outs() << buff->getBufferIdentifier() << "\n";
- if ((ec = _targetInfo.parseFile(mbc, result)))
+ if ((ec = _targetInfo.parseFile(std::move(mbc), result)))
return ec;
}
} else {
Modified: lld/trunk/lib/ReaderWriter/ReaderLinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ReaderLinkerScript.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ReaderLinkerScript.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ReaderLinkerScript.cpp Fri Apr 5 16:08:30 2013
@@ -79,9 +79,9 @@ private:
} // end anon namespace
namespace lld {
-error_code
-ReaderLinkerScript::parseFile(std::unique_ptr<llvm::MemoryBuffer> &mb,
- std::vector<std::unique_ptr<File> > &result) const {
+error_code ReaderLinkerScript::parseFile(
+ std::unique_ptr<llvm::MemoryBuffer> mb,
+ std::vector<std::unique_ptr<File>> &result) const {
auto lsf = LinkerScriptFile::create(_targetInfo, std::move(mb));
if (!lsf)
return lsf;
Modified: lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp?rev=178914&r1=178913&r2=178914&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp Fri Apr 5 16:08:30 2013
@@ -1342,7 +1342,7 @@ class ReaderYAML : public Reader {
public:
ReaderYAML(const TargetInfo &ti) : Reader(ti) {}
- error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
+ error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
std::vector<std::unique_ptr<File>> &result) const {
// Note: we do not take ownership of the MemoryBuffer. That is
// because yaml may produce multiple File objects, so there is no
More information about the llvm-commits
mailing list