Fix PR4948 (and a leak): by not destroying the DwarfException
[llvm/avr.git] / test / Bindings / Ocaml / bitreader.ml
blob5c23041c80d357ef3bbcf844613fb2f36a17d786
1 (* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_bitreader.cmxa llvm_bitwriter.cmxa %s -o %t
2 * RUN: ./%t %t.bc
3 * RUN: llvm-dis < %t.bc | grep caml_int_ty
4 *)
6 (* Note that this takes a moment to link, so it's best to keep the number of
7 individual tests low. *)
9 let context = Llvm.global_context ()
11 let test x = if not x then exit 1 else ()
13 let _ =
14 let fn = Sys.argv.(1) in
15 let m = Llvm.create_module context "ocaml_test_module" in
17 ignore (Llvm.define_type_name "caml_int_ty" (Llvm.i32_type context) m);
19 test (Llvm_bitwriter.write_bitcode_file m fn);
21 Llvm.dispose_module m;
23 (* parse_bitcode *)
24 begin
25 let mb = Llvm.MemoryBuffer.of_file fn in
26 begin try
27 let m = Llvm_bitreader.parse_bitcode context mb in
28 Llvm.dispose_module m
29 with x ->
30 Llvm.MemoryBuffer.dispose mb;
31 raise x
32 end
33 end;
35 (* MemoryBuffer.of_file *)
36 test begin try
37 let mb = Llvm.MemoryBuffer.of_file (fn ^ ".bogus") in
38 Llvm.MemoryBuffer.dispose mb;
39 false
40 with Llvm.IoError _ ->
41 true
42 end;
44 (* get_module_provider *)
45 begin
46 let mb = Llvm.MemoryBuffer.of_file fn in
47 let mp = begin try
48 Llvm_bitreader.get_module_provider context mb
49 with x ->
50 Llvm.MemoryBuffer.dispose mb;
51 raise x
52 end in
53 Llvm.ModuleProvider.dispose mp
54 end;
56 (* corrupt the bitcode *)
57 let fn = fn ^ ".txt" in
58 begin let oc = open_out fn in
59 output_string oc "not a bitcode file\n";
60 close_out oc
61 end;
63 (* test get_module_provider exceptions *)
64 test begin
65 try
66 let mb = Llvm.MemoryBuffer.of_file fn in
67 let mp = begin try
68 Llvm_bitreader.get_module_provider context mb
69 with x ->
70 Llvm.MemoryBuffer.dispose mb;
71 raise x
72 end in
73 Llvm.ModuleProvider.dispose mp;
74 false
75 with Llvm_bitreader.Error _ ->
76 true
77 end