[llvm] f860187 - [OCaml] Add (get/set)_module_identifer functions

Vaivaswatha Nagaraj via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 20 08:37:35 PDT 2021


Author: Vaivaswatha Nagaraj
Date: 2021-03-20T20:41:51+05:30
New Revision: f860187ea6e9b30e1ecf74784f0af0e0c9ecc01c

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

LOG: [OCaml] Add (get/set)_module_identifer functions

Also:

- Fix a bug that crept in when fixing a buildbot failure in
https://github.com/llvm/llvm-project/commit/f7be9db6220cb39f0eaa12d2af3abedf0d86c303
- Use mlsize_t for cstr_to_string as that is what
caml_alloc_string specifies.

Differential Revision: https://reviews.llvm.org/D98851

Added: 
    

Modified: 
    llvm/bindings/ocaml/llvm/llvm.ml
    llvm/bindings/ocaml/llvm/llvm.mli
    llvm/bindings/ocaml/llvm/llvm_ocaml.c
    llvm/bindings/ocaml/llvm/llvm_ocaml.h
    llvm/test/Bindings/OCaml/core.ml

Removed: 
    


################################################################################
diff  --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 243f872fe029..9e55ea8c4364 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -442,6 +442,13 @@ external string_of_llmodule : llmodule -> string = "llvm_string_of_llmodule"
 external set_module_inline_asm : llmodule -> string -> unit
                                = "llvm_set_module_inline_asm"
 external module_context : llmodule -> llcontext = "LLVMGetModuleContext"
+
+external get_module_identifier : llmodule -> string
+                               = "llvm_get_module_identifier"
+
+external set_module_identifer : llmodule -> string -> unit
+                              = "llvm_set_module_identifier"
+
 external get_module_flag : llmodule -> string -> llmetadata option
                          = "llvm_get_module_flag"
 external add_module_flag : llmodule -> ModuleFlagBehavior.t ->

diff  --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index d65260dc7d0f..c191382aee22 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -543,6 +543,14 @@ val set_module_inline_asm : llmodule -> string -> unit
     See the method [llvm::Module::getContext] *)
 val module_context : llmodule -> llcontext
 
+(** [get_module_identifier m] returns the module identifier of the
+    specified module. See the method [llvm::Module::getModuleIdentifier] *)
+val get_module_identifier : llmodule -> string
+
+(** [set_module_identifier m id] sets the module identifier of [m]
+    to [id]. See the method [llvm::Module::setModuleIdentifier] *)
+val set_module_identifer : llmodule -> string -> unit
+
 (** [get_module_flag m k] Return the corresponding value if key [k] appears in
     the module flags of [m], otherwise return None
     See the method [llvm::Module::getModuleFlag] *)

diff  --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 104635bb6c3a..04f9796baf0c 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, unsigned Len) {
+CAMLprim value cstr_to_string(const unsigned char *Str, mlsize_t Len) {
   CAMLparam0();
   CAMLlocal1(String);
   if (Str) {
     String = caml_alloc_string(Len);
-    memcpy(String_val(Str), Str, Len);
+    memcpy(String_val(String), Str, Len);
   } else {
     String = caml_alloc_string(0);
   }
@@ -335,6 +335,19 @@ CAMLprim value llvm_string_of_llmodule(LLVMModuleRef M) {
   CAMLreturn(ModuleStr);
 }
 
+/* llmodule -> string */
+CAMLprim value llvm_get_module_identifier(LLVMModuleRef M) {
+  size_t Len;
+  const char *Name = LLVMGetModuleIdentifier(M, &Len);
+  return cstr_to_string(Name, (mlsize_t)Len);
+}
+
+/* llmodule -> string -> unit */
+CAMLprim value llvm_set_module_identifier(LLVMModuleRef M, value Id) {
+  LLVMSetModuleIdentifier(M, String_val(Id), caml_string_length(Id));
+  return Val_unit;
+}
+
 /* llmodule -> string -> unit */
 CAMLprim value llvm_set_module_inline_asm(LLVMModuleRef M, value Asm) {
   LLVMSetModuleInlineAsm(M, String_val(Asm));

diff  --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.h b/llvm/bindings/ocaml/llvm/llvm_ocaml.h
index 0b39b4730360..c52f7ed63650 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, unsigned Len);
+CAMLprim value cstr_to_string(const unsigned char *Str, mlsize_t Len);
 
 #endif // LLVM_LLVM_OCAML_H

diff  --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index e1bb6b056142..532171a1842c 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -596,6 +596,10 @@ let test_global_variables () =
   begin group "iteration";
     let m = create_module context "temp" in
 
+    insist (get_module_identifier m = "temp");
+    set_module_identifer m "temp2";
+    insist (get_module_identifier m = "temp2");
+
     insist (At_end m = global_begin m);
     insist (At_start m = global_end m);
 


        


More information about the llvm-commits mailing list