From b6f24fdff21f030d2b1ad7bb56fc78112dc33378 Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Thu, 25 Mar 2021 23:24:16 +0000 Subject: [PATCH] [NFC][OCaml] Resolve const and unsigned compilation warnings There are a number of compilation warnings regarding disregarding const qualifiers, and casting between pointers to integer types with different sign. The incompatible sign warnings are due to treating the result of `LLVMGetModuleIdentifier` as `const unsigned char *`, but it is declared as `const char *`. The dropped const qualifiers are due to the code pattern `memcpy(String_val(_),_,_)` which ought to be (following the implementation of the OCaml runtime) `memcpy((char *)String_val(_),_,_)`. The issue is that `String_val` is usually used to get the value of an immutable string. But in the context of the `memcpy` calls, the string is in the process of being initialized, so is not yet constant. Differential Revision: https://reviews.llvm.org/D99392 --- bindings/ocaml/llvm/llvm_ocaml.c | 16 ++++++++-------- bindings/ocaml/llvm/llvm_ocaml.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index af655f94eb2..9a05345ba64 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -44,12 +44,12 @@ CAMLprim value ptr_to_option(void *Ptr) { CAMLreturn(Option); } -CAMLprim value cstr_to_string(const unsigned char *Str, mlsize_t Len) { +CAMLprim value cstr_to_string(const char *Str, mlsize_t Len) { CAMLparam0(); CAMLlocal1(String); if (Str) { String = caml_alloc_string(Len); - memcpy(String_val(String), Str, Len); + memcpy((char *)String_val(String), Str, Len); } else { String = caml_alloc_string(0); } @@ -87,7 +87,7 @@ CAMLprim value llvm_enable_pretty_stacktrace(value Unit) { } CAMLprim value llvm_parse_command_line_options(value Overview, value Args) { - char *COverview; + const char *COverview; if (Overview == Val_int(0)) { COverview = NULL; } else { @@ -258,7 +258,7 @@ CAMLprim value llvm_get_string_attr_kind(LLVMAttributeRef A) { unsigned Length; const char *String = LLVMGetStringAttributeKind(A, &Length); value Result = caml_alloc_string(Length); - memcpy(String_val(Result), String, Length); + memcpy((char *)String_val(Result), String, Length); return Result; } @@ -267,7 +267,7 @@ CAMLprim value llvm_get_string_attr_value(LLVMAttributeRef A) { unsigned Length; const char *String = LLVMGetStringAttributeValue(A, &Length); value Result = caml_alloc_string(Length); - memcpy(String_val(Result), String, Length); + memcpy((char *)String_val(Result), String, Length); return Result; } @@ -884,7 +884,7 @@ CAMLprim value llvm_get_mdstring(LLVMValueRef V) { if ((S = LLVMGetMDString(V, &Len))) { Str = caml_alloc_string(Len); - memcpy(String_val(Str), S, Len); + memcpy((char *)String_val(Str), S, Len); Option = alloc(1,0); Store_field(Option, 0, Str); CAMLreturn(Option); @@ -1053,7 +1053,7 @@ CAMLprim value llvm_string_of_const(LLVMValueRef Const) { if(LLVMIsAConstantDataSequential(Const) && LLVMIsConstantString(Const)) { S = LLVMGetAsString(Const, &Len); Str = caml_alloc_string(Len); - memcpy(String_val(Str), S, Len); + memcpy((char *)String_val(Str), S, Len); Option = alloc(1, 0); Field(Option, 0) = Str; @@ -2595,7 +2595,7 @@ CAMLprim LLVMMemoryBufferRef llvm_memorybuffer_of_string(value Name, value Strin /* llmemorybuffer -> string */ CAMLprim value llvm_memorybuffer_as_string(LLVMMemoryBufferRef MemBuf) { value String = caml_alloc_string(LLVMGetBufferSize(MemBuf)); - memcpy(String_val(String), LLVMGetBufferStart(MemBuf), + memcpy((char *)String_val(String), LLVMGetBufferStart(MemBuf), LLVMGetBufferSize(MemBuf)); return String; diff --git a/bindings/ocaml/llvm/llvm_ocaml.h b/bindings/ocaml/llvm/llvm_ocaml.h index c52f7ed6365..1202cc79f2c 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.h +++ b/bindings/ocaml/llvm/llvm_ocaml.h @@ -25,6 +25,6 @@ CAMLprim value ptr_to_option(void *Ptr); /* Convert a C string into an OCaml string */ -CAMLprim value cstr_to_string(const unsigned char *Str, mlsize_t Len); +CAMLprim value cstr_to_string(const char *Str, mlsize_t Len); #endif // LLVM_LLVM_OCAML_H