[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
This commit is contained in:
Josh Berdine
2021-03-25 23:24:16 +00:00
parent 511d90318d
commit b6f24fdff2
2 changed files with 9 additions and 9 deletions

View File

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

View File

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