[sledge][llvm] Code simplification using string allocation functions

Summary:
Using the `cstr_to_string` function that allocates and initializes an
OCaml `string` value enables simplifications in several cases. This
change also has the effect of avoiding calling `memcpy` on NULL
pointers even if only 0 bytes are to be copied.

Upstream Differential Revision: https://reviews.llvm.org/D99474

Reviewed By: ngorogiannis

Differential Revision: D27564875

fbshipit-source-id: b316adfe1
master
Josh Berdine 4 years ago committed by Facebook GitHub Bot
parent e2936c1a54
commit 973e901f82

@ -280,18 +280,14 @@ CAMLprim value llvm_is_string_attr(LLVMAttributeRef A) {
CAMLprim value llvm_get_string_attr_kind(LLVMAttributeRef A) { CAMLprim value llvm_get_string_attr_kind(LLVMAttributeRef A) {
unsigned Length; unsigned Length;
const char *String = LLVMGetStringAttributeKind(A, &Length); const char *String = LLVMGetStringAttributeKind(A, &Length);
value Result = caml_alloc_string(Length); return cstr_to_string (String, Length);
memcpy((char *)String_val(Result), String, Length);
return Result;
} }
/* llattribute -> string */ /* llattribute -> string */
CAMLprim value llvm_get_string_attr_value(LLVMAttributeRef A) { CAMLprim value llvm_get_string_attr_value(LLVMAttributeRef A) {
unsigned Length; unsigned Length;
const char *String = LLVMGetStringAttributeValue(A, &Length); const char *String = LLVMGetStringAttributeValue(A, &Length);
value Result = caml_alloc_string(Length); return cstr_to_string (String, Length);
memcpy((char *)String_val(Result), String, Length);
return Result;
} }
/*===-- Modules -----------------------------------------------------------===*/ /*===-- Modules -----------------------------------------------------------===*/
@ -2612,11 +2608,9 @@ CAMLprim LLVMMemoryBufferRef llvm_memorybuffer_of_string(value Name, value Strin
/* llmemorybuffer -> string */ /* llmemorybuffer -> string */
CAMLprim value llvm_memorybuffer_as_string(LLVMMemoryBufferRef MemBuf) { CAMLprim value llvm_memorybuffer_as_string(LLVMMemoryBufferRef MemBuf) {
value String = caml_alloc_string(LLVMGetBufferSize(MemBuf)); size_t BufferSize = LLVMGetBufferSize(MemBuf);
memcpy((char *)String_val(String), LLVMGetBufferStart(MemBuf), const char *BufferStart = LLVMGetBufferStart(MemBuf);
LLVMGetBufferSize(MemBuf)); return cstr_to_string(BufferStart, BufferSize);
return String;
} }
/* llmemorybuffer -> unit */ /* llmemorybuffer -> unit */

Loading…
Cancel
Save