authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-24 12:19:16-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-24 12:19:16-04:00
log004c383292412faef7987b5fb5da73a2971176ee
tree62f03d78b28a73fd12d7f81a4907455ac3acdea3
parent8a7737eef4f9f5fd3e6d318b6985e1bf6a364438
signaturelock-open Commit is signed but in an unrecognized format.

fix translate-c incorrectly translating negative enum init values

closes #1360

2 files changed, 61 insertions(+), 1 deletions(-)

src/translate_c.cpp+7-1
...@@ -458,7 +458,13 @@ static const char *decl_name(const Decl *decl) {...@@ -458,7 +458,13 @@ static const char *decl_name(const Decl *decl) {
458static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {458static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {
459 AstNode *node = trans_create_node(c, NodeTypeIntLiteral);459 AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
460 node->data.int_literal.bigint = allocate<BigInt>(1);460 node->data.int_literal.bigint = allocate<BigInt>(1);
461 bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative());461 bool is_negative = aps_int.isNegative();
462 if (!is_negative) {
463 bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), false);
464 return node;
465 }
466 llvm::APSInt negated = -aps_int;
467 bigint_init_data(node->data.int_literal.bigint, negated.getRawData(), negated.getNumWords(), true);
462 return node;468 return node;
463469
464}470}
test/translate_c.zig+54
...@@ -1,6 +1,60 @@...@@ -1,6 +1,60 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.TranslateCContext) void {3pub fn addCases(cases: *tests.TranslateCContext) void {
4 cases.add("negative enum init values",
5 \\enum EnumWithInits {
6 \\ VAL01 = 0,
7 \\ VAL02 = 1,
8 \\ VAL03 = 2,
9 \\ VAL04 = 3,
10 \\ VAL05 = -1,
11 \\ VAL06 = -2,
12 \\ VAL07 = -3,
13 \\ VAL08 = -4,
14 \\ VAL09 = VAL02 + VAL08,
15 \\ VAL10 = -1000012000,
16 \\ VAL11 = -1000161000,
17 \\ VAL12 = -1000174001,
18 \\ VAL13 = VAL09,
19 \\ VAL14 = VAL10,
20 \\ VAL15 = VAL11,
21 \\ VAL16 = VAL13,
22 \\ VAL17 = (VAL16 - VAL10 + 1),
23 \\ VAL18 = 0x1000000000000000L,
24 \\ VAL19 = VAL18 + VAL18 + VAL18 - 1,
25 \\ VAL20 = VAL19 + VAL19,
26 \\ VAL21 = VAL20 + 0xFFFFFFFFFFFFFFFF,
27 \\ VAL22 = 0xFFFFFFFFFFFFFFFF + 1,
28 \\ VAL23 = 0xFFFFFFFFFFFFFFFF,
29 \\};
30 ,
31 \\pub const enum_EnumWithInits = extern enum(c_longlong) {
32 \\ VAL01 = 0,
33 \\ VAL02 = 1,
34 \\ VAL03 = 2,
35 \\ VAL04 = 3,
36 \\ VAL05 = -1,
37 \\ VAL06 = -2,
38 \\ VAL07 = -3,
39 \\ VAL08 = -4,
40 \\ VAL09 = -3,
41 \\ VAL10 = -1000012000,
42 \\ VAL11 = -1000161000,
43 \\ VAL12 = -1000174001,
44 \\ VAL13 = -3,
45 \\ VAL14 = -1000012000,
46 \\ VAL15 = -1000161000,
47 \\ VAL16 = -3,
48 \\ VAL17 = 1000011998,
49 \\ VAL18 = 1152921504606846976,
50 \\ VAL19 = 3458764513820540927,
51 \\ VAL20 = 6917529027641081854,
52 \\ VAL21 = 6917529027641081853,
53 \\ VAL22 = 0,
54 \\ VAL23 = -1,
55 \\};
56 );
57
4 cases.add("for loop with var init but empty body",58 cases.add("for loop with var init but empty body",
5 \\void foo(void) {59 \\void foo(void) {
6 \\ for (int x = 0; x < 10; x++);60 \\ for (int x = 0; x < 10; x++);