authorgravatar for michaeltnoronha@gmail.comMichael Noronha <michaeltnoronha@gmail.com> 2018-08-12 17:41:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-20 22:45:19-04:00
log7e7e59d8811b9fd0ba7dde345f61597a49a3bd13
tree6e66638f672ef95a125d80378d13a69fdcc14620
parentdd4b13ac0326aeb6c2c197bfac49f9e931ccee37

translate-c: Correctly translate enum init values, addressing #1360


2 files changed, 36 insertions(+), 2 deletions(-)

src/translate_c.cpp+10-2
......@@ -458,9 +458,17 @@ static const char *decl_name(const Decl *decl) {
458458static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {
459459 AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
460460 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());
462 return node;
463461
462 llvm::APSInt copy = aps_int;
463 llvm::APSInt positive = (~copy)++;
464
465 if (!aps_int.isNegative()) {
466 bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative());
467 } else {
468 bigint_init_data(node->data.int_literal.bigint, positive.getRawData(), positive.getNumWords(), aps_int.isNegative());
469 }
470
471 return node;
464472}
465473
466474static const Type *qual_type_canon(QualType qt) {
test/translate_c.zig+26
......@@ -1358,4 +1358,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13581358 \\ }
13591359 \\}
13601360 );
1361
1362 cases.add("correctly translate enum init values",
1363 \\enum EnumWithInits {
1364 \\ VAL1 = 0,
1365 \\ VAL2 = 1,
1366 \\ VAL3 = 2,
1367 \\ VAL4 = 3,
1368 \\ VAL5 = -1,
1369 \\ VAL6 = -2,
1370 \\ VAL7 = -3,
1371 \\ VAL8 = -4,
1372 \\ VAL9 = VAL2 + VAL8,
1373 \\};
1374 ,
1375 \\pub const enum_EnumWithInits = extern enum {
1376 \\ VAL1 = 0,
1377 \\ VAL2 = 1,
1378 \\ VAL3 = 2,
1379 \\ VAL4 = 3,
1380 \\ VAL5 = -1,
1381 \\ VAL6 = -2,
1382 \\ VAL7 = -3,
1383 \\ VAL8 = -4,
1384 \\ VAL9 = -3,
1385 \\};
1386 );
13611387}