@@ -14,6 +14,7 @@ let () = Config.manpage [
14
14
`P " $(b,bap-api)(3), $(b,bap-c)(3), $(b,bap-plugin-api)(1)"
15
15
]
16
16
17
+
17
18
let int size sign : C.Type.basic = match size,sign with
18
19
| (NO_SIZE ,(NO_SIGN |SIGNED )) -> `sint
19
20
| (SHORT ,(NO_SIGN |SIGNED )) -> `sshort
@@ -73,7 +74,7 @@ let name_groups : name_group list -> 'a list =
73
74
List. map ns ~f: (fun (n ,t ,attrs ,_ ) -> n,t,attrs))
74
75
75
76
let single_names : single_name list -> 'a list =
76
- List. map ~f: (fun (_ ,s ,(n ,t ,attrs ,_ )) -> n,t,attrs)
77
+ List. map ~f: (fun (_ ,_ ,(n ,t ,attrs ,_ )) -> n,t,attrs)
77
78
78
79
let rec gnu_attr = function
79
80
| GNU_NONE -> None
@@ -83,7 +84,8 @@ let rec gnu_attr = function
83
84
| GNU_ID s -> Some C.Type.Attr. {name = s; args = [] }
84
85
| GNU_CST _
85
86
| GNU_EXTENSION
86
- | GNU_INLINE -> None
87
+ | GNU_INLINE
88
+ | GNU_TYPE_ARG _ -> None
87
89
and gnu_attrs_args = List. filter_map ~f: (function
88
90
| GNU_ID s
89
91
| GNU_CST
@@ -142,11 +144,16 @@ type tag = {
142
144
143
145
let ctype gamma {lookup} t =
144
146
let rec ctype : base_type -> C.Type.t = function
145
- | NO_TYPE | TYPE_LINE _ | OLD_PROTO _ | BITFIELD _ | VOID -> `Void
147
+ | NO_TYPE | TYPE_LINE _ | OLD_PROTO _ | BITFIELD _
148
+ | BUILTIN_TYPE _ | VOID -> `Void
149
+ | BOOL -> basic `bool
146
150
| CHAR sign -> basic @@ char sign
147
151
| INT (size ,sign ) -> basic @@ int size sign
148
152
| FLOAT _ -> basic @@ `float
149
153
| DOUBLE long -> basic @@ if long then `long_double else `double
154
+ | COMPLEX_FLOAT -> basic `cfloat
155
+ | COMPLEX_DOUBLE -> basic `cdouble
156
+ | COMPLEX_LONG_DOUBLE -> basic `clong_double
150
157
| PTR t -> pointer @@ ctype t
151
158
| RESTRICT_PTR t -> restrict @@ ctype t
152
159
| ARRAY (et ,ice ) -> array (size ice) @@ ctype et
@@ -161,7 +168,7 @@ let ctype gamma {lookup} t =
161
168
| CONST t -> qualify const @@ ctype t
162
169
| VOLATILE t -> qualify volatile @@ ctype t
163
170
| GNU_TYPE (a ,t ) -> with_attrs (gnu_attrs a) @@ ctype t
164
- and enum_items tag =
171
+ and enum_items _ =
165
172
List. map ~f: (fun (name ,exp ) -> match exp with
166
173
| CONSTANT (CONST_INT x ) ->
167
174
name, Option. try_with (fun () -> Int64. of_string x)
@@ -200,7 +207,7 @@ let parse (size : C.Size.base) parse lexbuf =
200
207
let tags = String.Table. create () in
201
208
let gamma name = match Hashtbl. find env name with
202
209
| Some t -> t
203
- | None -> invalid_argf " unbound type %s " name () in
210
+ | None -> `Void in
204
211
let lookup what name = match Hashtbl. find tags name with
205
212
| Some t -> t
206
213
| None -> what name [] in
@@ -231,19 +238,40 @@ let parse (size : C.Size.base) parse lexbuf =
231
238
Hashtbl. map_inplace env ~f: resolve;
232
239
Hashtbl. to_alist env
233
240
234
- let parser size name =
235
- In_channel. with_file name ~f: (fun input ->
236
- let open Clexer in
237
- init {
238
- ! current_handle with
239
- h_in_channel = input;
240
- h_file_name = name;
241
- h_out_channel = stderr;
242
- };
243
- init_lexicon () ;
244
- let lexbuf = Lexing. from_function (get_buffer current_handle) in
245
- let parser = Cparser. file initial in
246
- try Ok (parse size parser lexbuf) with exn ->
247
- Or_error. of_exn exn )
248
-
249
- let () = Config. when_ready @@ fun _ -> C.Parser. provide parser
241
+ exception Cpp_failed
242
+
243
+ let with_file cpp name f =
244
+ match cpp with
245
+ | None -> In_channel. with_file name ~f
246
+ | Some cpp ->
247
+ let cmd = sprintf " %s %S" cpp name in
248
+ let input = Caml_unix. open_process_in cmd in
249
+ protect ~f: (fun () -> f input)
250
+ ~finally: (fun () ->
251
+ match Caml_unix. close_process_in input with
252
+ | WEXITED 0 -> ()
253
+ | _ -> raise Cpp_failed )
254
+
255
+ let parser cpp size name =
256
+ with_file cpp name @@ fun input ->
257
+ let open Clexer in
258
+ init {
259
+ ! current_handle with
260
+ h_in_channel = input;
261
+ h_file_name = name;
262
+ h_out_channel = stderr;
263
+ };
264
+ init_lexicon () ;
265
+ let lexbuf = Lexing. from_function (get_buffer current_handle) in
266
+ let parser = Cparser. file initial in
267
+ try Ok (parse size parser lexbuf) with exn ->
268
+ Or_error. of_exn exn
269
+
270
+ let cpp = Config. param Config. (some string )
271
+ ~as_flag: (Some " cpp" )
272
+ ~doc: " Preprocess headers with the specified preprocessor."
273
+ " preprocess"
274
+ ~synonyms: [" pp" ]
275
+
276
+ let () = Config. when_ready @@ fun {get} ->
277
+ C.Parser. provide (parser (get cpp))
0 commit comments