translate-c: default initialize non-extern variables to undefined

This commit is contained in:
Vexu 2020-03-06 00:04:24 +02:00 committed by Andrew Kelley
parent 6b069f5c8c
commit eaccfffe56
2 changed files with 4 additions and 2 deletions

View File

@ -609,7 +609,8 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
else
try transCreateNodeUndefinedLiteral(c);
} else if (storage_class != .Extern) {
return failDecl(c, var_decl_loc, checked_name, "non-extern variable has no initializer", .{});
eq_tok = try appendToken(c, .Equal, "=");
init_node = try transCreateNodeTypeIdentifier(c, "undefined");
}
const linksection_expr = blk: {

View File

@ -337,10 +337,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("variables",
\\extern int extern_var;
\\static const int int_var = 13;
\\int foo;
, &[_][]const u8{
\\pub extern var extern_var: c_int;
,
\\pub const int_var: c_int = 13;
\\pub export var foo: c_int = undefined;
});
cases.add("const ptr initializer",