forked from OSchip/llvm-project
Correctly pass ownership of MemoryBuffers.
llvm-svn: 178914
This commit is contained in:
parent
096ea03f31
commit
ce1e53e19c
|
@ -260,7 +260,8 @@ 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,
|
||||
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
|
||||
|
@ -269,9 +270,8 @@ public:
|
|||
///
|
||||
/// \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
|
||||
|
|
|
@ -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,
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -57,8 +57,8 @@ public:
|
|||
}
|
||||
virtual bool validate(raw_ostream &diagnostics);
|
||||
|
||||
|
||||
virtual error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
|
||||
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);
|
||||
|
|
|
@ -32,7 +32,8 @@ public:
|
|||
virtual ErrorOr<std::string> stringFromRelocKind(Reference::Kind kind) const;
|
||||
virtual bool validate(raw_ostream &diagnostics);
|
||||
|
||||
virtual error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
|
||||
virtual error_code
|
||||
parseFile(std::unique_ptr<MemoryBuffer> mb,
|
||||
std::vector<std::unique_ptr<File>> &result) const;
|
||||
|
||||
uint32_t getCPUType() const;
|
||||
|
|
|
@ -39,7 +39,8 @@ 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,
|
||||
virtual error_code
|
||||
parseFile(std::unique_ptr<MemoryBuffer> mb,
|
||||
std::vector<std::unique_ptr<File>> &result) const = 0;
|
||||
|
||||
protected:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -38,8 +38,7 @@ error_code TargetInfo::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 this->parseFile(std::unique_ptr<MemoryBuffer>(opmb.take()), result);
|
||||
}
|
||||
|
||||
error_code TargetInfo::writeFile(const File &linkedFile) const {
|
||||
|
|
|
@ -358,12 +358,12 @@ void CoreTargetInfo::addPasses(PassManager &pm) const {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
error_code CoreTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
|
||||
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);
|
||||
return _reader->parseFile(std::move(mb), result);
|
||||
}
|
||||
|
||||
Writer &CoreTargetInfo::writer() const {
|
||||
|
|
|
@ -91,28 +91,27 @@ bool ELFTargetInfo::isDynamic() const {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
error_code ELFTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
|
||||
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) {
|
||||
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
|
||||
StringRef path = mb->getBufferIdentifier();
|
||||
if ( path.endswith(".objtxt") ) {
|
||||
if (StringRef(path).endswith(".objtxt")) {
|
||||
if (!_yamlReader)
|
||||
_yamlReader = createReaderYAML(*this);
|
||||
ec = _yamlReader->parseFile(mb, result);
|
||||
return _yamlReader->parseFile(std::move(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);
|
||||
}
|
||||
}
|
||||
return ec;
|
||||
return _linkerScriptReader->parseFile(std::move(mb), result);
|
||||
}
|
||||
|
||||
Writer &ELFTargetInfo::writer() const {
|
||||
|
|
|
@ -82,7 +82,7 @@ public:
|
|||
: lld::Reader(ti), _elfTargetInfo(ti), _readerArchive(ti, *this) {
|
||||
}
|
||||
|
||||
error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
|
||||
error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
|
||||
std::vector<std::unique_ptr<File>> &result) const {
|
||||
using llvm::object::ELFType;
|
||||
llvm::sys::LLVMFileType fileType =
|
||||
|
@ -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);
|
||||
|
|
|
@ -209,9 +209,8 @@ void MachOTargetInfo::addPasses(PassManager &pm) const {
|
|||
pm.add(std::unique_ptr<Pass>(new LayoutPass()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
error_code MachOTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
|
||||
error_code MachOTargetInfo::parseFile(
|
||||
std::unique_ptr<MemoryBuffer> mb,
|
||||
std::vector<std::unique_ptr<File>> &result) const {
|
||||
// if (!_machoReader)
|
||||
// _machoReader = createReaderMachO(*this);
|
||||
|
@ -219,7 +218,7 @@ error_code MachOTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
|
|||
// if (ec) {
|
||||
if (!_yamlReader)
|
||||
_yamlReader = createReaderYAML(*this);
|
||||
return _yamlReader->parseFile(mb,result);
|
||||
return _yamlReader->parseFile(std::move(mb), result);
|
||||
// }
|
||||
|
||||
return error_code::success();
|
||||
|
|
|
@ -915,8 +915,8 @@ public:
|
|||
Reader(const TargetInfo &ti)
|
||||
: lld::Reader(ti) {}
|
||||
|
||||
virtual error_code parseFile(
|
||||
std::unique_ptr<MemoryBuffer> &mb,
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -369,7 +369,7 @@ class ReaderCOFF : public Reader {
|
|||
public:
|
||||
ReaderCOFF(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 {
|
||||
llvm::error_code ec;
|
||||
std::unique_ptr<File> f(new FileCOFF(_targetInfo, std::move(mb), ec));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,7 +159,8 @@ 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,
|
||||
error_code
|
||||
ReaderArchive::parseFile(std::unique_ptr<llvm::MemoryBuffer> mb,
|
||||
std::vector<std::unique_ptr<File>> &result) const {
|
||||
error_code ec;
|
||||
|
||||
|
@ -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 {
|
||||
|
|
|
@ -79,8 +79,8 @@ private:
|
|||
} // end anon namespace
|
||||
|
||||
namespace lld {
|
||||
error_code
|
||||
ReaderLinkerScript::parseFile(std::unique_ptr<llvm::MemoryBuffer> &mb,
|
||||
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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue