Skip to content

Commit 7dfd205

Browse files
committed
Ignore fields that start with _
Closes #230
1 parent 13bfb4b commit 7dfd205

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

ppx/ppx_cstruct.ml

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ type field = {
4545
off: int;
4646
}
4747

48+
let field_is_ignored f =
49+
String.get f.field 0 = '_'
50+
4851
type t = {
4952
name: string;
5053
fields: field list;
@@ -220,14 +223,17 @@ let hexdump_to_buffer_expr s =
220223
| UInt64 -> [%expr "0x%Lx\n"]
221224
in
222225
let hexdump_field f =
223-
let get_f = op_evar s (Op_get f) in
224-
let expr =
225-
match f.ty with
226-
|Prim p ->
227-
[%expr Printf.bprintf buf [%e prim_format_string p] ([%e get_f] v)]
228-
|Buffer (_,_) ->
229-
[%expr Printf.bprintf buf "<buffer %s>" [%e Ast.str (field_to_string f)];
230-
Cstruct.hexdump_to_buffer buf ([%e get_f] v)]
226+
if field_is_ignored f then
227+
[%expr ()]
228+
else
229+
let get_f = op_evar s (Op_get f) in
230+
let expr =
231+
match f.ty with
232+
|Prim p ->
233+
[%expr Printf.bprintf buf [%e prim_format_string p] ([%e get_f] v)]
234+
|Buffer (_,_) ->
235+
[%expr Printf.bprintf buf "<buffer %s>" [%e Ast.str (field_to_string f)];
236+
Cstruct.hexdump_to_buffer buf ([%e get_f] v)]
231237
in
232238
[%expr
233239
Printf.bprintf buf " %s = " [%e Ast.str f.field];
@@ -249,26 +255,25 @@ let op_expr loc s = function
249255
[%expr fun src srcoff dst ->
250256
Cstruct.blit src srcoff dst [%e Ast.int f.off] [%e Ast.int len]]
251257

258+
let field_ops_for f =
259+
if field_is_ignored f then
260+
[]
261+
else
262+
let if_buffer x =
263+
match f.ty with
264+
|Buffer (_,_) -> [x]
265+
|Prim _ -> []
266+
in
267+
List.concat
268+
[ [Op_get f]
269+
; if_buffer (Op_copy f)
270+
; [Op_set f]
271+
; if_buffer (Op_blit f)
272+
]
273+
252274
let ops_for s =
253-
let field_ops =
254-
List.concat (
255-
List.map (fun f ->
256-
let if_buffer x =
257-
match f.ty with
258-
|Buffer (_,_) -> [x]
259-
|Prim _ -> []
260-
in
261-
List.concat
262-
[ [Op_get f]
263-
; if_buffer (Op_copy f)
264-
; [Op_set f]
265-
; if_buffer (Op_blit f)
266-
]
267-
) s.fields
268-
)
269-
in
270275
( [Op_sizeof]
271-
@ field_ops
276+
@ List.concat (List.map field_ops_for s.fields)
272277
@ [Op_hexdump_to_buffer;
273278
Op_hexdump;
274279
])

ppx_test/basic.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ foo = {
1010
"\007\000,\000\000\190\239abcdefgh"
1111
with_ignored_field = {
1212
a = 0x1
13-
_b = 0x2
1413
c = 0x3
1514

1615
}

ppx_test/basic.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type with_ignored_field = {
8484
} [@@little_endian]
8585
]
8686

87-
let _ : Cstruct.t -> int -> unit = set_with_ignored_field__b
87+
let _ : bool = set_with_ignored_field__b
8888

8989
let tests () =
9090
(* Test basic set/get functions *)

0 commit comments

Comments
 (0)