summaryrefslogtreecommitdiff
path: root/bindings
diff options
context:
space:
mode:
authorJeroen Ketema <j.ketema@imperial.ac.uk>2016-04-10 13:55:53 +0000
committerJeroen Ketema <j.ketema@imperial.ac.uk>2016-04-10 13:55:53 +0000
commit828014932bfdae9e64703f95c6fc1060f83e0f22 (patch)
tree4dcc0a87907ca1be69cd3b55f31bb7227723efd7 /bindings
parente6319e78de313ebb738bd94c3483b24c4c9b271d (diff)
[OCaml] Expose the LLVM diagnostic handler
Differential Revision: http://reviews.llvm.org/D18891 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265897 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r--bindings/ocaml/llvm/llvm.ml22
-rw-r--r--bindings/ocaml/llvm/llvm.mli28
-rw-r--r--bindings/ocaml/llvm/llvm_ocaml.c44
3 files changed, 93 insertions, 1 deletions
diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml
index 259d57bc068..5e149d44c41 100644
--- a/bindings/ocaml/llvm/llvm.ml
+++ b/bindings/ocaml/llvm/llvm.ml
@@ -283,6 +283,14 @@ module ValueKind = struct
| Instruction of Opcode.t
end
+module DiagnosticSeverity = struct
+ type t =
+ | Error
+ | Warning
+ | Remark
+ | Note
+end
+
exception IoError of string
let () = Callback.register_exception "Llvm.IoError" (IoError "")
@@ -304,6 +312,20 @@ type ('a, 'b) llrev_pos =
| At_start of 'a
| After of 'b
+
+(*===-- Context error handling --------------------------------------------===*)
+module Diagnostic = struct
+ type t
+
+ external description : t -> string = "llvm_get_diagnostic_description"
+ external severity : t -> DiagnosticSeverity.t
+ = "llvm_get_diagnostic_severity"
+end
+
+external set_diagnostic_handler
+ : llcontext -> (Diagnostic.t -> unit) option -> unit
+ = "llvm_set_diagnostic_handler"
+
(*===-- Contexts ----------------------------------------------------------===*)
external create_context : unit -> llcontext = "llvm_create_context"
external dispose_context : llcontext -> unit = "llvm_dispose_context"
diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli
index 541c35a2a22..5d3ce958264 100644
--- a/bindings/ocaml/llvm/llvm.mli
+++ b/bindings/ocaml/llvm/llvm.mli
@@ -15,7 +15,7 @@
(** {6 Abstract types}
- These abstract types correlate directly to the LLVM VMCore classes. *)
+ These abstract types correlate directly to the LLVMCore classes. *)
(** The top-level container for all LLVM global data. See the
[llvm::LLVMContext] class. *)
@@ -352,6 +352,16 @@ module ValueKind : sig
| Instruction of Opcode.t
end
+(** The kind of [Diagnostic], the result of [Diagnostic.severity d].
+ See [llvm::DiagnosticSeverity]. *)
+module DiagnosticSeverity : sig
+ type t =
+ | Error
+ | Warning
+ | Remark
+ | Note
+end
+
(** {6 Iteration} *)
@@ -398,6 +408,22 @@ val reset_fatal_error_handler : unit -> unit
See the function [llvm::cl::ParseCommandLineOptions()]. *)
val parse_command_line_options : ?overview:string -> string array -> unit
+(** {6 Context error handling} *)
+
+module Diagnostic : sig
+ type t
+
+ (** [description d] returns a textual description of [d]. *)
+ val description : t -> string
+
+ (** [severity d] returns the severity of [d]. *)
+ val severity : t -> DiagnosticSeverity.t
+end
+
+(** [set_diagnostic_handler c h] set the diagnostic handler of [c] to [h].
+ See the method [llvm::LLVMContext::setDiagnosticHandler]. *)
+val set_diagnostic_handler : llcontext -> (Diagnostic.t -> unit) option -> unit
+
(** {6 Contexts} *)
(** [create_context ()] creates a context for storing the "global" state in
diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c
index 925f3bdb600..665842d0b6c 100644
--- a/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/bindings/ocaml/llvm/llvm_ocaml.c
@@ -115,6 +115,49 @@ static value alloc_variant(int tag, void *Value) {
return alloc_variant(0, pfun(Kid)); \
}
+/*===-- Context error handling --------------------------------------------===*/
+
+void llvm_diagnostic_handler_trampoline(LLVMDiagnosticInfoRef DI,
+ void *DiagnosticContext) {
+ caml_callback(*((value *)DiagnosticContext), (value)DI);
+}
+
+/* Diagnostic.t -> string */
+CAMLprim value llvm_get_diagnostic_description(value Diagnostic) {
+ return llvm_string_of_message(
+ LLVMGetDiagInfoDescription((LLVMDiagnosticInfoRef)Diagnostic));
+}
+
+/* Diagnostic.t -> DiagnosticSeverity.t */
+CAMLprim value llvm_get_diagnostic_severity(value Diagnostic) {
+ return Val_int(LLVMGetDiagInfoSeverity((LLVMDiagnosticInfoRef)Diagnostic));
+}
+
+static void llvm_remove_diagnostic_handler(LLVMContextRef C) {
+ if (LLVMContextGetDiagnosticHandler(C) ==
+ llvm_diagnostic_handler_trampoline) {
+ value *Handler = (value *)LLVMContextGetDiagnosticContext(C);
+ remove_global_root(Handler);
+ free(Handler);
+ }
+}
+
+/* llcontext -> (Diagnostic.t -> unit) option -> unit */
+CAMLprim value llvm_set_diagnostic_handler(LLVMContextRef C, value Handler) {
+ llvm_remove_diagnostic_handler(C);
+ if (Handler == Val_int(0)) {
+ LLVMContextSetDiagnosticHandler(C, NULL, NULL);
+ } else {
+ value *DiagnosticContext = malloc(sizeof(value));
+ if (DiagnosticContext == NULL)
+ caml_raise_out_of_memory();
+ caml_register_global_root(DiagnosticContext);
+ *DiagnosticContext = Field(Handler, 0);
+ LLVMContextSetDiagnosticHandler(C, llvm_diagnostic_handler_trampoline,
+ DiagnosticContext);
+ }
+ return Val_unit;
+}
/*===-- Contexts ----------------------------------------------------------===*/
@@ -125,6 +168,7 @@ CAMLprim LLVMContextRef llvm_create_context(value Unit) {
/* llcontext -> unit */
CAMLprim value llvm_dispose_context(LLVMContextRef C) {
+ llvm_remove_diagnostic_handler(C);
LLVMContextDispose(C);
return Val_unit;
}