From dd4b13ac0326aeb6c2c197bfac49f9e931ccee37 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 20 Aug 2018 22:39:39 -0400 Subject: [PATCH 1/4] Revert "translate-c: Correctly translate enum init values, addressing #1360 (#1377)" This reverts commit b8ce8f219c48ae833199130ce575241f848d690b. Squashing the commits from the pull request resulted in kristopher tate from being omitted from the authors. A future commit will merge the code correctly. --- src/translate_c.cpp | 10 ++------ test/translate_c.zig | 54 -------------------------------------------- 2 files changed, 2 insertions(+), 62 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 830ed27cd..735a671bc 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -458,15 +458,9 @@ static const char *decl_name(const Decl *decl) { static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) { AstNode *node = trans_create_node(c, NodeTypeIntLiteral); node->data.int_literal.bigint = allocate(1); - bool is_negative = aps_int.isNegative(); - // ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData; - // Internally to Zig we store an integer's sign outside of getRawData! - // ++(~aps_int) calls .flip() internally on the raw data to match Zig. - bigint_init_data( node->data.int_literal.bigint - , (is_negative ? (++(~aps_int)) : aps_int).getRawData() - , aps_int.getNumWords() - , is_negative ); + bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative()); return node; + } static const Type *qual_type_canon(QualType qt) { diff --git a/test/translate_c.zig b/test/translate_c.zig index 1942b58fe..b31e515aa 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1358,58 +1358,4 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ } \\} ); - - cases.add("correctly translate enum init values", - \\enum EnumWithInits { - \\ VAL01 = 0, - \\ VAL02 = 1, - \\ VAL03 = 2, - \\ VAL04 = 3, - \\ VAL05 = -1, - \\ VAL06 = -2, - \\ VAL07 = -3, - \\ VAL08 = -4, - \\ VAL09 = VAL02 + VAL08, - \\ VAL10 = -1000012000, - \\ VAL11 = -1000161000, - \\ VAL12 = -1000174001, - \\ VAL13 = VAL09, - \\ VAL14 = VAL10, - \\ VAL15 = VAL11, - \\ VAL16 = VAL13, - \\ VAL17 = (VAL16 - VAL10 + 1), - \\ VAL18 = 0x1000000000000000L, - \\ VAL19 = VAL18 + VAL18 + VAL18 - 1, - \\ VAL20 = VAL19 + VAL19, - \\ VAL21 = VAL20 + 0xFFFFFFFFFFFFFFFF, - \\ VAL22 = 0xFFFFFFFFFFFFFFFF + 1, - \\ VAL23 = 0xFFFFFFFFFFFFFFFF, - \\}; - , - \\pub const enum_EnumWithInits = extern enum(c_longlong) { - \\ VAL01 = 0, - \\ VAL02 = 1, - \\ VAL03 = 2, - \\ VAL04 = 3, - \\ VAL05 = -1, - \\ VAL06 = -2, - \\ VAL07 = -3, - \\ VAL08 = -4, - \\ VAL09 = -3, - \\ VAL10 = -1000012000, - \\ VAL11 = -1000161000, - \\ VAL12 = -1000174001, - \\ VAL13 = -3, - \\ VAL14 = -1000012000, - \\ VAL15 = -1000161000, - \\ VAL16 = -3, - \\ VAL17 = 1000011998, - \\ VAL18 = 1152921504606846976, - \\ VAL19 = 3458764513820540927, - \\ VAL20 = 6917529027641081854, - \\ VAL21 = 6917529027641081853, - \\ VAL22 = 0, - \\ VAL23 = -1, - \\}; - ); } From 7e7e59d8811b9fd0ba7dde345f61597a49a3bd13 Mon Sep 17 00:00:00 2001 From: Michael Noronha Date: Sun, 12 Aug 2018 17:41:43 -0700 Subject: [PATCH 2/4] translate-c: Correctly translate enum init values, addressing #1360 --- src/translate_c.cpp | 12 ++++++++++-- test/translate_c.zig | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 735a671bc..da37b907d 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -458,9 +458,17 @@ static const char *decl_name(const Decl *decl) { static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) { AstNode *node = trans_create_node(c, NodeTypeIntLiteral); node->data.int_literal.bigint = allocate(1); - bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative()); - return node; + llvm::APSInt copy = aps_int; + llvm::APSInt positive = (~copy)++; + + if (!aps_int.isNegative()) { + bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative()); + } else { + bigint_init_data(node->data.int_literal.bigint, positive.getRawData(), positive.getNumWords(), aps_int.isNegative()); + } + + return node; } static const Type *qual_type_canon(QualType qt) { diff --git a/test/translate_c.zig b/test/translate_c.zig index b31e515aa..a25d4200c 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1358,4 +1358,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ } \\} ); + + cases.add("correctly translate enum init values", + \\enum EnumWithInits { + \\ VAL1 = 0, + \\ VAL2 = 1, + \\ VAL3 = 2, + \\ VAL4 = 3, + \\ VAL5 = -1, + \\ VAL6 = -2, + \\ VAL7 = -3, + \\ VAL8 = -4, + \\ VAL9 = VAL2 + VAL8, + \\}; + , + \\pub const enum_EnumWithInits = extern enum { + \\ VAL1 = 0, + \\ VAL2 = 1, + \\ VAL3 = 2, + \\ VAL4 = 3, + \\ VAL5 = -1, + \\ VAL6 = -2, + \\ VAL7 = -3, + \\ VAL8 = -4, + \\ VAL9 = -3, + \\}; + ); } From b023db2e822baa5b54e15e0dbc1c5e35c8931cb6 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sun, 19 Aug 2018 00:22:46 +0900 Subject: [PATCH 3/4] src/translate_c.cpp: correctly bridge llvm::APSInt with Zig BigInt; ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData; Internally to Zig we store an integer's sign outside of getRawData! (~aps_int) calls .flip() internally on the raw data to match Zig. test/translate_c.zig: enum: add wider range of values (u64) to try; --- src/translate_c.cpp | 18 ++++++------ test/translate_c.zig | 66 +++++++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index da37b907d..830ed27cd 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -458,16 +458,14 @@ static const char *decl_name(const Decl *decl) { static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) { AstNode *node = trans_create_node(c, NodeTypeIntLiteral); node->data.int_literal.bigint = allocate(1); - - llvm::APSInt copy = aps_int; - llvm::APSInt positive = (~copy)++; - - if (!aps_int.isNegative()) { - bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative()); - } else { - bigint_init_data(node->data.int_literal.bigint, positive.getRawData(), positive.getNumWords(), aps_int.isNegative()); - } - + bool is_negative = aps_int.isNegative(); + // ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData; + // Internally to Zig we store an integer's sign outside of getRawData! + // ++(~aps_int) calls .flip() internally on the raw data to match Zig. + bigint_init_data( node->data.int_literal.bigint + , (is_negative ? (++(~aps_int)) : aps_int).getRawData() + , aps_int.getNumWords() + , is_negative ); return node; } diff --git a/test/translate_c.zig b/test/translate_c.zig index a25d4200c..1942b58fe 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1361,27 +1361,55 @@ pub fn addCases(cases: *tests.TranslateCContext) void { cases.add("correctly translate enum init values", \\enum EnumWithInits { - \\ VAL1 = 0, - \\ VAL2 = 1, - \\ VAL3 = 2, - \\ VAL4 = 3, - \\ VAL5 = -1, - \\ VAL6 = -2, - \\ VAL7 = -3, - \\ VAL8 = -4, - \\ VAL9 = VAL2 + VAL8, + \\ VAL01 = 0, + \\ VAL02 = 1, + \\ VAL03 = 2, + \\ VAL04 = 3, + \\ VAL05 = -1, + \\ VAL06 = -2, + \\ VAL07 = -3, + \\ VAL08 = -4, + \\ VAL09 = VAL02 + VAL08, + \\ VAL10 = -1000012000, + \\ VAL11 = -1000161000, + \\ VAL12 = -1000174001, + \\ VAL13 = VAL09, + \\ VAL14 = VAL10, + \\ VAL15 = VAL11, + \\ VAL16 = VAL13, + \\ VAL17 = (VAL16 - VAL10 + 1), + \\ VAL18 = 0x1000000000000000L, + \\ VAL19 = VAL18 + VAL18 + VAL18 - 1, + \\ VAL20 = VAL19 + VAL19, + \\ VAL21 = VAL20 + 0xFFFFFFFFFFFFFFFF, + \\ VAL22 = 0xFFFFFFFFFFFFFFFF + 1, + \\ VAL23 = 0xFFFFFFFFFFFFFFFF, \\}; , - \\pub const enum_EnumWithInits = extern enum { - \\ VAL1 = 0, - \\ VAL2 = 1, - \\ VAL3 = 2, - \\ VAL4 = 3, - \\ VAL5 = -1, - \\ VAL6 = -2, - \\ VAL7 = -3, - \\ VAL8 = -4, - \\ VAL9 = -3, + \\pub const enum_EnumWithInits = extern enum(c_longlong) { + \\ VAL01 = 0, + \\ VAL02 = 1, + \\ VAL03 = 2, + \\ VAL04 = 3, + \\ VAL05 = -1, + \\ VAL06 = -2, + \\ VAL07 = -3, + \\ VAL08 = -4, + \\ VAL09 = -3, + \\ VAL10 = -1000012000, + \\ VAL11 = -1000161000, + \\ VAL12 = -1000174001, + \\ VAL13 = -3, + \\ VAL14 = -1000012000, + \\ VAL15 = -1000161000, + \\ VAL16 = -3, + \\ VAL17 = 1000011998, + \\ VAL18 = 1152921504606846976, + \\ VAL19 = 3458764513820540927, + \\ VAL20 = 6917529027641081854, + \\ VAL21 = 6917529027641081853, + \\ VAL22 = 0, + \\ VAL23 = -1, \\}; ); } From b2917e6be09138adcf7cfdab51a1909a30eec320 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 21 Aug 2018 20:50:03 -0400 Subject: [PATCH 4/4] Revert "Merge branch 'mtn-translate-c-enum-vals'" This reverts commit 937b822fa90181bf59f1c2ac45faab09342a183f, reversing changes made to dd4b13ac0326aeb6c2c197bfac49f9e931ccee37. Tests failing on Windows. Re-opens #1360 --- src/translate_c.cpp | 10 ++------ test/translate_c.zig | 54 -------------------------------------------- 2 files changed, 2 insertions(+), 62 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 830ed27cd..735a671bc 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -458,15 +458,9 @@ static const char *decl_name(const Decl *decl) { static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) { AstNode *node = trans_create_node(c, NodeTypeIntLiteral); node->data.int_literal.bigint = allocate(1); - bool is_negative = aps_int.isNegative(); - // ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData; - // Internally to Zig we store an integer's sign outside of getRawData! - // ++(~aps_int) calls .flip() internally on the raw data to match Zig. - bigint_init_data( node->data.int_literal.bigint - , (is_negative ? (++(~aps_int)) : aps_int).getRawData() - , aps_int.getNumWords() - , is_negative ); + bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative()); return node; + } static const Type *qual_type_canon(QualType qt) { diff --git a/test/translate_c.zig b/test/translate_c.zig index 1942b58fe..b31e515aa 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1358,58 +1358,4 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ } \\} ); - - cases.add("correctly translate enum init values", - \\enum EnumWithInits { - \\ VAL01 = 0, - \\ VAL02 = 1, - \\ VAL03 = 2, - \\ VAL04 = 3, - \\ VAL05 = -1, - \\ VAL06 = -2, - \\ VAL07 = -3, - \\ VAL08 = -4, - \\ VAL09 = VAL02 + VAL08, - \\ VAL10 = -1000012000, - \\ VAL11 = -1000161000, - \\ VAL12 = -1000174001, - \\ VAL13 = VAL09, - \\ VAL14 = VAL10, - \\ VAL15 = VAL11, - \\ VAL16 = VAL13, - \\ VAL17 = (VAL16 - VAL10 + 1), - \\ VAL18 = 0x1000000000000000L, - \\ VAL19 = VAL18 + VAL18 + VAL18 - 1, - \\ VAL20 = VAL19 + VAL19, - \\ VAL21 = VAL20 + 0xFFFFFFFFFFFFFFFF, - \\ VAL22 = 0xFFFFFFFFFFFFFFFF + 1, - \\ VAL23 = 0xFFFFFFFFFFFFFFFF, - \\}; - , - \\pub const enum_EnumWithInits = extern enum(c_longlong) { - \\ VAL01 = 0, - \\ VAL02 = 1, - \\ VAL03 = 2, - \\ VAL04 = 3, - \\ VAL05 = -1, - \\ VAL06 = -2, - \\ VAL07 = -3, - \\ VAL08 = -4, - \\ VAL09 = -3, - \\ VAL10 = -1000012000, - \\ VAL11 = -1000161000, - \\ VAL12 = -1000174001, - \\ VAL13 = -3, - \\ VAL14 = -1000012000, - \\ VAL15 = -1000161000, - \\ VAL16 = -3, - \\ VAL17 = 1000011998, - \\ VAL18 = 1152921504606846976, - \\ VAL19 = 3458764513820540927, - \\ VAL20 = 6917529027641081854, - \\ VAL21 = 6917529027641081853, - \\ VAL22 = 0, - \\ VAL23 = -1, - \\}; - ); }