authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-08-31 20:14:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-01 17:54:07-07:00
logc0e84e3ee0f6aea0537d83f141cb17522d5e3a1b
treefb603a13d5057d207766167dff05c7d907611743
parent0932b0d9b309e654951d7c7f94dae823d7cdd154

translate-c: translate valueless macros as empty strings


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

src/translate_c.zig+5-2
...@@ -401,7 +401,7 @@ pub fn translate(...@@ -401,7 +401,7 @@ pub fn translate(
401 .name = decl.name,401 .name = decl.name,
402 .init = try Tag.import_builtin.create(context.arena, decl.name),402 .init = try Tag.import_builtin.create(context.arena, decl.name),
403 });403 });
404 try context.global_scope.nodes.append(builtin);404 try addTopLevelDecl(&context, decl.name, builtin);
405 }405 }
406 }406 }
407407
...@@ -5347,7 +5347,10 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void {...@@ -5347,7 +5347,10 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void {
5347 },5347 },
5348 .Nl, .Eof => {5348 .Nl, .Eof => {
5349 // this means it is a macro without a value5349 // this means it is a macro without a value
5350 // we don't care about such things5350 // We define it as an empty string so that it can still be used with ++
5351 const str_node = try Tag.string_literal.create(c.arena, "\"\"");
5352 const var_decl = try Tag.pub_var_simple.create(c.arena, .{ .name = name, .init = str_node });
5353 try c.global_scope.macro_table.put(name, var_decl);
5351 continue;5354 continue;
5352 },5355 },
5353 .LParen => {5356 .LParen => {
test/translate_c.zig+6
...@@ -3678,4 +3678,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3678,4 +3678,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3678 , &[_][]const u8{3678 , &[_][]const u8{
3679 \\pub const FOO = @compileError("unable to translate macro: undefined identifier `std`");3679 \\pub const FOO = @compileError("unable to translate macro: undefined identifier `std`");
3680 });3680 });
3681
3682 cases.add("Macro without a value",
3683 \\#define FOO
3684 , &[_][]const u8{
3685 \\pub const FOO = "";
3686 });
3681}3687}