Correctly pass ownership of MemoryBuffers.

llvm-svn: 178914
This commit is contained in:
Michael J. Spencer 2013-04-05 21:08:30 +00:00
parent 096ea03f31
commit ce1e53e19c
18 changed files with 72 additions and 73 deletions

View File

@ -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.

View File

@ -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);
}

View File

@ -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?

View File

@ -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;

View File

@ -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

View File

@ -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:

View File

@ -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;
};

View File

@ -37,9 +37,8 @@ error_code TargetInfo::readFile(StringRef path,
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 {

View File

@ -358,12 +358,12 @@ void CoreTargetInfo::addPasses(PassManager &pm) const {
}
}
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 {

View File

@ -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 {

View File

@ -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);

View File

@ -209,19 +209,18 @@ void MachOTargetInfo::addPasses(PassManager &pm) const {
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();
}

View File

@ -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);
}
};

View File

@ -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) {

View File

@ -24,7 +24,6 @@ error_code Reader::readFile(StringRef path,
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

View File

@ -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::unique_ptr<llvm::MemoryBuffer> &mb,
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 {

View File

@ -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;

View File

@ -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