Skip to content

Commit 6fffdd2

Browse files
committed
configurator: allow sizeof in C symbols import
Fixes #1723 Signed-off-by: Christophe Troestler <[email protected]>
1 parent 94f635e commit 6fffdd2

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ unreleased
5959
rules that require files outside the build/source directory. (#1708, fixes
6060
#848, @rgrinberg)
6161

62+
- Let `Configurator` handle `sizeof` (in addition to negative numbers).
63+
(#1726, fixes #1723, @Chris00)
64+
6265
1.6.2 (05/12/2018)
6366
------------------
6467

src/configurator/v1.ml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,18 @@ module C_define = struct
340340
Option.iter prelude ~f:(pr "%s");
341341
if has_type Type.Int then (
342342
pr {|
343-
#define D0(x) ('0'+(x/1 )%%10)
344-
#define D1(x) ('0'+(x/10 )%%10), D0(x)
345-
#define D2(x) ('0'+(x/100 )%%10), D1(x)
346-
#define D3(x) ('0'+(x/1000 )%%10), D2(x)
347-
#define D4(x) ('0'+(x/10000 )%%10), D3(x)
348-
#define D5(x) ('0'+(x/100000 )%%10), D4(x)
349-
#define D6(x) ('0'+(x/1000000 )%%10), D5(x)
350-
#define D7(x) ('0'+(x/10000000 )%%10), D6(x)
351-
#define D8(x) ('0'+(x/100000000 )%%10), D7(x)
352-
#define D9(x) ('0'+(x/1000000000)%%10), D8(x)
343+
#define DUNE_ABS(x) ((x >= 0)? x: -(x))
344+
#define D0(x) ('0'+(DUNE_ABS(x)/1 )%%10)
345+
#define D1(x) ('0'+(DUNE_ABS(x)/10 )%%10), D0(x)
346+
#define D2(x) ('0'+(DUNE_ABS(x)/100 )%%10), D1(x)
347+
#define D3(x) ('0'+(DUNE_ABS(x)/1000 )%%10), D2(x)
348+
#define D4(x) ('0'+(DUNE_ABS(x)/10000 )%%10), D3(x)
349+
#define D5(x) ('0'+(DUNE_ABS(x)/100000 )%%10), D4(x)
350+
#define D6(x) ('0'+(DUNE_ABS(x)/1000000 )%%10), D5(x)
351+
#define D7(x) ('0'+(DUNE_ABS(x)/10000000 )%%10), D6(x)
352+
#define D8(x) ('0'+(DUNE_ABS(x)/100000000 )%%10), D7(x)
353+
#define D9(x) ('0'+(DUNE_ABS(x)/1000000000)%%10), D8(x)
354+
#define DUNE_SIGN(x) ((x >= 0)? '+': '-')
353355
|}
354356
);
355357
List.iteri vars ~f:(fun i (name, t) ->
@@ -366,14 +368,11 @@ module C_define = struct
366368
pr {|
367369
const char s%i[] = {
368370
'B', 'E', 'G', 'I', 'N', '-', %s'-',
369-
#if %s >= 0
371+
DUNE_SIGN((%s)),
370372
D9((%s)),
371-
#else
372-
'-', D9((- %s)),
373-
#endif
374373
'-', 'E', 'N', 'D'
375374
};
376-
|} i c_arr_i name name name
375+
|} i c_arr_i name name
377376
| String ->
378377
pr {|const char *s%i = "BEGIN-%i-" %s "-END";|} i i name;
379378
| Switch ->

test/blackbox-tests/test-cases/configurator/import-define/run.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ let () =
44
let module C_define = Configurator.C_define in
55
Configurator.main ~name:"c_test" (fun t ->
66
C_define.import t
7-
~prelude:{|#define CONFIGURATOR_TESTING "foobar"|}
7+
~prelude:"#define CONFIGURATOR_TESTING \"foobar\"\n\
8+
#define CONFIGURATOR_NEG_INT -127\n"
89
~includes:["caml/config.h"]
910
[ "CAML_CONFIG_H", C_define.Type.Switch
1011
; "Page_log", C_define.Type.Int
1112
; "CONFIGURATOR_TESTING", C_define.Type.String
13+
; "CONFIGURATOR_NEG_INT", C_define.Type.Int
14+
; "sizeof(char)", C_define.Type.Int
1215
]
1316
|> List.iter (fun (n, v) ->
1417
Printf.printf "%s=%s\n"

test/blackbox-tests/test-cases/configurator/run.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Importing #define's from code is successful
1212
CAML_CONFIG_H=true
1313
Page_log=12
1414
CONFIGURATOR_TESTING=foobar
15+
CONFIGURATOR_NEG_INT=-127
16+
sizeof(char)=1

0 commit comments

Comments
 (0)