[llvm] 0b1dc49 - [NFC][OCaml] Resolve const and unsigned compilation warnings

Josh Berdine via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 26 05:01:41 PDT 2021


Author: Josh Berdine
Date: 2021-03-26T11:49:13Z
New Revision: 0b1dc49ca38a8569b0aee40ea8d3054bc960e2ed

URL: https://github.com/llvm/llvm-project/commit/0b1dc49ca38a8569b0aee40ea8d3054bc960e2ed
DIFF: https://github.com/llvm/llvm-project/commit/0b1dc49ca38a8569b0aee40ea8d3054bc960e2ed.diff

LOG: [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

Added: 
    

Modified: 
    llvm/bindings/ocaml/llvm/llvm_ocaml.c
    llvm/bindings/ocaml/llvm/llvm_ocaml.h

Removed: 
    


################################################################################
diff  --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index af655f94eb28..9a05345ba648 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/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/llvm/bindings/ocaml/llvm/llvm_ocaml.h b/llvm/bindings/ocaml/llvm/llvm_ocaml.h
index c52f7ed63650..1202cc79f2c2 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.h
+++ b/llvm/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


        


More information about the llvm-commits mailing list