mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
translate-c: Correctly translate enum init values, addressing #1360
This commit is contained in:
parent
dd4b13ac03
commit
7e7e59d881
@ -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<BigInt>(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) {
|
||||
|
||||
@ -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,
|
||||
\\};
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user