authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-08-31 12:23:23-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-01 17:54:07-07:00
logdf589eecd61a4a2c93922302f8f486f18d0ff06b
treedffedae386db77c3a7beea33efbd91976679c324
parent8d2acff197a46a9de33ce78713291428aa45ee7c

translate-c: improve handling of undefined identifiers


3 files changed, 46 insertions(+), 32 deletions(-)

src/translate_c.zig+19-9
......@@ -395,7 +395,15 @@ pub fn translate(
395395 context.pattern_list.deinit(gpa);
396396 }
397397
398 try context.global_scope.nodes.append(Tag.usingnamespace_builtins.init());
398 inline for (meta.declarations(std.zig.c_builtins)) |decl| {
399 if (decl.is_pub) {
400 const builtin = try Tag.pub_var_simple.create(context.arena, .{
401 .name = decl.name,
402 .init = try Tag.import_builtin.create(context.arena, decl.name),
403 });
404 try context.global_scope.nodes.append(builtin);
405 }
406 }
399407
400408 try prepopulateGlobalNameTable(ast_unit, &context);
401409
......@@ -5249,16 +5257,19 @@ const MacroCtx = struct {
52495257 return MacroSlicer{ .source = self.source, .tokens = self.list };
52505258 }
52515259
5252 fn containsUndefinedIdentifier(self: *MacroCtx, scope: *Scope) ?[]const u8 {
5260 fn containsUndefinedIdentifier(self: *MacroCtx, scope: *Scope, params: []const ast.Payload.Param) ?[]const u8 {
52535261 const slicer = self.makeSlicer();
52545262 var i: usize = 1; // index 0 is the macro name
52555263 while (i < self.list.len) : (i += 1) {
52565264 const token = self.list[i];
52575265 switch (token.id) {
5258 .Period => i += 1, // skip next token since field identifiers can be unknown
5266 .Period, .Arrow => i += 1, // skip next token since field identifiers can be unknown
52595267 .Identifier => {
52605268 const identifier = slicer.slice(token);
5261 if (!scope.contains(identifier)) return identifier;
5269 const is_param = for (params) |param| {
5270 if (param.name != null and mem.eql(u8, identifier, param.name.?)) break true;
5271 } else false;
5272 if (!scope.contains(identifier) and !isBuiltinDefined(identifier) and !is_param) return identifier;
52625273 },
52635274 else => {},
52645275 }
......@@ -5361,7 +5372,7 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void {
53615372fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void {
53625373 const scope = &c.global_scope.base;
53635374
5364 if (m.containsUndefinedIdentifier(scope)) |ident|
5375 if (m.containsUndefinedIdentifier(scope, &.{})) |ident|
53655376 return m.fail(c, "unable to translate macro: undefined identifier `{s}`", .{ident});
53665377
53675378 const init_node = try parseCExpr(c, m, scope);
......@@ -5414,6 +5425,9 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
54145425 return m.fail(c, "unable to translate C expr: expected ')'", .{});
54155426 }
54165427
5428 if (m.containsUndefinedIdentifier(scope, fn_params.items)) |ident|
5429 return m.fail(c, "unable to translate macro: undefined identifier `{s}`", .{ident});
5430
54175431 const expr = try parseCExpr(c, m, scope);
54185432 const last = m.next().?;
54195433 if (last != .Eof and last != .Nl)
......@@ -5755,10 +5769,6 @@ fn parseCPrimaryExprInner(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!N
57555769 },
57565770 .Identifier => {
57575771 const mangled_name = scope.getAlias(slice);
5758 if (mem.startsWith(u8, mangled_name, "__builtin_") and !isBuiltinDefined(mangled_name)) {
5759 try m.fail(c, "TODO implement function '{s}' in std.zig.c_builtins", .{mangled_name});
5760 return error.ParseError;
5761 }
57625772 if (builtin_typedef_map.get(mangled_name)) |ty| return Tag.type.create(c.arena, ty);
57635773 const identifier = try Tag.identifier.create(c.arena, mangled_name);
57645774 scope.skipVariableDiscard(identifier.castTag(.identifier).?.data);
src/translate_c/ast.zig+14-21
......@@ -31,8 +31,6 @@ pub const Node = extern union {
3131 @"anytype",
3232 @"continue",
3333 @"break",
34 /// pub usingnamespace @import("std").zig.c_builtins
35 usingnamespace_builtins,
3634 // After this, the tag requires a payload.
3735
3836 integer_literal,
......@@ -119,6 +117,8 @@ pub const Node = extern union {
119117 ellipsis3,
120118 assign,
121119
120 /// @import("std").zig.c_builtins.<name>
121 import_builtin,
122122 log2_int_type,
123123 /// @import("std").math.Log2Int(operand)
124124 std_math_Log2Int,
......@@ -224,7 +224,7 @@ pub const Node = extern union {
224224 /// [1]type{val} ** count
225225 array_filler,
226226
227 pub const last_no_payload_tag = Tag.usingnamespace_builtins;
227 pub const last_no_payload_tag = Tag.@"break";
228228 pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;
229229
230230 pub fn Type(comptime t: Tag) type {
......@@ -236,7 +236,6 @@ pub const Node = extern union {
236236 .true_literal,
237237 .false_literal,
238238 .empty_block,
239 .usingnamespace_builtins,
240239 .return_void,
241240 .zero_literal,
242241 .one_literal,
......@@ -344,6 +343,7 @@ pub const Node = extern union {
344343 .warning,
345344 .type,
346345 .helpers_macro,
346 .import_builtin,
347347 => Payload.Value,
348348 .discard => Payload.Discard,
349349 .@"if" => Payload.If,
......@@ -871,22 +871,6 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
871871 try c.buf.append('\n');
872872 return @as(NodeIndex, 0); // error: integer value 0 cannot be coerced to type 'std.mem.Allocator.Error!u32'
873873 },
874 .usingnamespace_builtins => {
875 // pub usingnamespace @import("std").c.builtins;
876 _ = try c.addToken(.keyword_pub, "pub");
877 const usingnamespace_token = try c.addToken(.keyword_usingnamespace, "usingnamespace");
878 const import_node = try renderStdImport(c, &.{ "zig", "c_builtins" });
879 _ = try c.addToken(.semicolon, ";");
880
881 return c.addNode(.{
882 .tag = .@"usingnamespace",
883 .main_token = usingnamespace_token,
884 .data = .{
885 .lhs = import_node,
886 .rhs = undefined,
887 },
888 });
889 },
890874 .std_math_Log2Int => {
891875 const payload = node.castTag(.std_math_Log2Int).?.data;
892876 const import_node = try renderStdImport(c, &.{ "math", "Log2Int" });
......@@ -1143,6 +1127,15 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
11431127 };
11441128 return renderStdImport(c, &chain);
11451129 },
1130 .import_builtin => {
1131 const payload = node.castTag(.import_builtin).?.data;
1132 const chain = [_][]const u8{
1133 "zig",
1134 "c_builtins",
1135 payload,
1136 };
1137 return renderStdImport(c, &chain);
1138 },
11461139 .string_slice => {
11471140 const payload = node.castTag(.string_slice).?.data;
11481141
......@@ -2352,7 +2345,6 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
23522345 .@"comptime",
23532346 .@"defer",
23542347 .asm_simple,
2355 .usingnamespace_builtins,
23562348 .while_true,
23572349 .if_not_break,
23582350 .switch_else,
......@@ -2371,6 +2363,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
23712363 .bit_xor_assign,
23722364 .assign,
23732365 .helpers_macro,
2366 .import_builtin,
23742367 => {
23752368 // these should never appear in places where grouping might be needed.
23762369 unreachable;
test/translate_c.zig+13-2
......@@ -195,6 +195,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
195195
196196 cases.add("use cast param as macro fn return type",
197197 \\#include <stdint.h>
198 \\#define SYS_BASE_CACHED 0
198199 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)
199200 , &[_][]const u8{
200201 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void {
......@@ -364,6 +365,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
364365 });
365366
366367 cases.add("correct semicolon after infixop",
368 \\#define _IO_ERR_SEEN 0
367369 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
368370 , &[_][]const u8{
369371 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
......@@ -430,6 +432,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
430432
431433 cases.add("macro comma operator",
432434 \\#define foo (foo, bar)
435 \\int baz(int x, int y) { return 0; }
433436 \\#define bar(x) (&x, +3, 4 == 4, 5 * 6, baz(1, 2), 2 % 2, baz(1,2))
434437 , &[_][]const u8{
435438 \\pub const foo = blk: {
......@@ -2573,6 +2576,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25732576
25742577 cases.add("macro call",
25752578 \\#define CALL(arg) bar(arg)
2579 \\int bar(int x) { return x; }
25762580 , &[_][]const u8{
25772581 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
25782582 \\ return bar(arg);
......@@ -2581,6 +2585,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25812585
25822586 cases.add("macro call with no args",
25832587 \\#define CALL(arg) bar()
2588 \\int bar(void) { return 0; }
25842589 , &[_][]const u8{
25852590 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {
25862591 \\ _ = arg;
......@@ -3139,6 +3144,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31393144
31403145 cases.add("macro cast",
31413146 \\#include <stdint.h>
3147 \\int baz(void *arg) { return 0; }
31423148 \\#define FOO(bar) baz((void *)(baz))
31433149 \\#define BAR (void*) a
31443150 \\#define BAZ (uint32_t)(2)
......@@ -3475,11 +3481,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34753481 \\pub const MAY_NEED_PROMOTION_OCT = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0o20000000000, .octal);
34763482 });
34773483
3478 // See __builtin_alloca_with_align comment in std.zig.c_builtins
34793484 cases.add("demote un-implemented builtins",
34803485 \\#define FOO(X) __builtin_alloca_with_align((X), 8)
34813486 , &[_][]const u8{
3482 \\pub const FOO = @compileError("TODO implement function '__builtin_alloca_with_align' in std.zig.c_builtins");
3487 \\pub const FOO = @compileError("unable to translate macro: undefined identifier `__builtin_alloca_with_align`");
34833488 });
34843489
34853490 cases.add("null sentinel arrays when initialized from string literal. Issue #8256",
......@@ -3661,4 +3666,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
36613666 , &[_][]const u8{
36623667 \\pub const FOO = @compileError("unable to translate macro: undefined identifier `BAR`");
36633668 });
3669
3670 cases.add("Macro redefines builtin",
3671 \\#define FOO __builtin_popcount
3672 , &[_][]const u8{
3673 \\pub const FOO = __builtin_popcount;
3674 });
36643675}