Skip to content

rustllvm: Don't require null terminators in files #15779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,30 @@ using namespace llvm::object;

static char *LastError;

#if LLVM_VERSION_MINOR >= 5
extern "C" LLVMMemoryBufferRef
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(Path,
-1,
false);
if (!buf_or) {
LLVMRustSetLastError(buf_or.getError().message().c_str());
return nullptr;
}
return wrap(buf_or.get().release());
}
#else
extern "C" LLVMMemoryBufferRef
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
LLVMMemoryBufferRef MemBuf = NULL;
char *err = NULL;
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf, &err);
if (err != NULL) {
LLVMRustSetLastError(err);
OwningPtr<MemoryBuffer> buf;
error_code err = MemoryBuffer::getFile(Path, buf, -1, false);
if (err) {
LLVMRustSetLastError(err.message().c_str());
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Do we prefer nullptr now instead of NULL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure, I don't think we have much style when it comes to these wrapper files. I think we still run the danger of compiling this file with pre-C++11 compilers though, so maybe not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the latest LLVM requires you to compile it with a C++11 compiler anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVM 3.5+ does, but LLVM before that didn't, and this is sadly specifically for pre-3.5 LLVM versions.

}
return MemBuf;
return wrap(buf.take());
}
#endif

extern "C" char *LLVMRustGetLastError(void) {
char *ret = LastError;
Expand Down Expand Up @@ -658,10 +672,12 @@ LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
#if LLVM_VERSION_MINOR >= 5
extern "C" void*
LLVMRustOpenArchive(char *path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(path);
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(path,
-1,
false);
if (!buf_or) {
LLVMRustSetLastError(buf_or.getError().message().c_str());
return NULL;
return nullptr;
}

std::error_code err;
Expand All @@ -676,7 +692,7 @@ LLVMRustOpenArchive(char *path) {
extern "C" void*
LLVMRustOpenArchive(char *path) {
OwningPtr<MemoryBuffer> buf;
error_code err = MemoryBuffer::getFile(path, buf);
error_code err = MemoryBuffer::getFile(path, buf, -1, false);
if (err) {
LLVMRustSetLastError(err.message().c_str());
return NULL;
Expand Down