[PATCH] D99392: [NFC][OCaml] Resolve const and unsigned compilation warnings

Josh Berdine via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 25 17:04:33 PDT 2021


jberdine created this revision.
jberdine added a reviewer: vaivaswatha.
Herald added a reviewer: whitequark.
jberdine requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99392

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


Index: llvm/bindings/ocaml/llvm/llvm_ocaml.h
===================================================================
--- llvm/bindings/ocaml/llvm/llvm_ocaml.h
+++ 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
Index: llvm/bindings/ocaml/llvm/llvm_ocaml.c
===================================================================
--- llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -44,12 +44,12 @@
   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_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 @@
   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 @@
   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 @@
 
   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 @@
   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 @@
 /* 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99392.333467.patch
Type: text/x-patch
Size: 2813 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210326/8460c4eb/attachment.bin>


More information about the llvm-commits mailing list