Skip to content

Commit

Permalink
Merge pull request #265 from emillon/len-not-int
Browse files Browse the repository at this point in the history
len attribute should be an int literal
  • Loading branch information
avsm authored Sep 3, 2019
2 parents 8d7ab7c + 353102f commit 5af8b1b
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 8 deletions.
26 changes: 18 additions & 8 deletions ppx/ppx_cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,25 @@ let constr_enum = function
| {pcd_loc = loc; _} ->
loc_err loc "invalid cenum variant"

let get_len = function
| [ ({txt = "len"; loc},
PStr
[{pstr_desc =
Pstr_eval ({pexp_desc = Pexp_constant (Pconst_integer (sz, None)); _}, _)
; _}])]
->
let n = int_of_string sz in
if n > 0 then
Some n
else
loc_err loc "[@len] argument should be > 0"
| [{txt = "len"; loc}, _ ] ->
loc_err loc "[@len] argument should be an integer"
| _ ->
None

let constr_field {pld_name = fname; pld_type = fty; pld_loc = loc; pld_attributes = att; _} =
let get = function
| [{txt = "len"; _}, PStr
[{pstr_desc = Pstr_eval ({pexp_desc = Pexp_constant (Pconst_integer(sz, _)); _}, _); _}]] ->
Some (int_of_string sz)
| _ ->
None
in
let sz = match get fty.ptyp_attributes, get att with
let sz = match get_len fty.ptyp_attributes, get_len att with
| Some sz, None
| None, Some sz -> Some sz
| Some _, Some _ -> loc_err loc "multiple field length attribute"
Expand Down
6 changes: 6 additions & 0 deletions ppx_test/errors/cstruct_len_int32.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[%%cstruct
type t = {
a: uint8_t [@len 8l]
}
[@@little_endian]
]
2 changes: 2 additions & 0 deletions ppx_test/errors/cstruct_len_int32.ml.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
File "cstruct_len_int32.ml", line 3, characters 17-20:
Error: ppx_cstruct: [@len] argument should be an integer
6 changes: 6 additions & 0 deletions ppx_test/errors/cstruct_len_not_int.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[%%cstruct
type t = {
a: uint8_t [@len ""]
}
[@@little_endian]
]
2 changes: 2 additions & 0 deletions ppx_test/errors/cstruct_len_not_int.ml.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
File "cstruct_len_not_int.ml", line 3, characters 17-20:
Error: ppx_cstruct: [@len] argument should be an integer
6 changes: 6 additions & 0 deletions ppx_test/errors/cstruct_len_zero.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[%%cstruct
type t = {
a: uint8_t [@len 0]
}
[@@little_endian]
]
2 changes: 2 additions & 0 deletions ppx_test/errors/cstruct_len_zero.ml.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
File "cstruct_len_zero.ml", line 3, characters 17-20:
Error: ppx_cstruct: [@len] argument should be > 0
42 changes: 42 additions & 0 deletions ppx_test/errors/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,48 @@
(action
(diff cstruct_duplicate_field.ml.expected cstruct_duplicate_field.ml.errors)))

(rule
(deps pp.exe (:input cstruct_len_int32.ml))
(targets cstruct_len_int32.ml.errors)
(action
(with-stderr-to
%{targets}
(run ./pp.exe --impl %{input}))))

(alias
(name runtest)
(package ppx_cstruct)
(action
(diff cstruct_len_int32.ml.expected cstruct_len_int32.ml.errors)))

(rule
(deps pp.exe (:input cstruct_len_not_int.ml))
(targets cstruct_len_not_int.ml.errors)
(action
(with-stderr-to
%{targets}
(run ./pp.exe --impl %{input}))))

(alias
(name runtest)
(package ppx_cstruct)
(action
(diff cstruct_len_not_int.ml.expected cstruct_len_not_int.ml.errors)))

(rule
(deps pp.exe (:input cstruct_len_zero.ml))
(targets cstruct_len_zero.ml.errors)
(action
(with-stderr-to
%{targets}
(run ./pp.exe --impl %{input}))))

(alias
(name runtest)
(package ppx_cstruct)
(action
(diff cstruct_len_zero.ml.expected cstruct_len_zero.ml.errors)))

(rule
(deps pp.exe (:input cstruct_multiple_len.ml))
(targets cstruct_multiple_len.ml.errors)
Expand Down

0 comments on commit 5af8b1b

Please sign in to comment.