authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-08-31 10:30:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-01 17:54:07-07:00
log89dd2b7ac23f298b5ac37ae022724b43c2137091
tree8e4ce82a0f3f9305c2cbae6d38501a9bca82ac7a
parentcca57042df374305390b5b0000be75ba6d24fb63

translate-c: emit compileError for undefined identifiers in macros


2 files changed, 32 insertions(+), 0 deletions(-)

src/translate_c.zig+20
......@@ -5248,6 +5248,23 @@ const MacroCtx = struct {
52485248 fn makeSlicer(self: *const MacroCtx) MacroSlicer {
52495249 return MacroSlicer{ .source = self.source, .tokens = self.list };
52505250 }
5251
5252 fn containsUndefinedIdentifier(self: *MacroCtx, scope: *Scope) ?[]const u8 {
5253 const slicer = self.makeSlicer();
5254 var i: usize = 1; // index 0 is the macro name
5255 while (i < self.list.len) : (i += 1) {
5256 const token = self.list[i];
5257 switch (token.id) {
5258 .Period => i += 1, // skip next token since field identifiers can be unknown
5259 .Identifier => {
5260 const identifier = slicer.slice(token);
5261 if (!scope.contains(identifier)) return identifier;
5262 },
5263 else => {},
5264 }
5265 }
5266 return null;
5267 }
52515268};
52525269
52535270fn tokenizeMacro(source: []const u8, tok_list: *std.ArrayList(CToken)) Error!void {
......@@ -5344,6 +5361,9 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void {
53445361fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void {
53455362 const scope = &c.global_scope.base;
53465363
5364 if (m.containsUndefinedIdentifier(scope)) |ident|
5365 return m.fail(c, "unable to translate macro: undefined identifier `{s}`", .{ident});
5366
53475367 const init_node = try parseCExpr(c, m, scope);
53485368 const last = m.next().?;
53495369 if (last != .Eof and last != .Nl)
test/translate_c.zig+12
......@@ -228,6 +228,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
228228 });
229229
230230 cases.add("macro expressions respect C operator precedence",
231 \\int *foo = 0;
231232 \\#define FOO *((foo) + 2)
232233 \\#define VALUE (1 + 2 * 3 + 4 * 5 + 6 << 7 | 8 == 9)
233234 \\#define _AL_READ3BYTES(p) ((*(unsigned char *)(p)) \
......@@ -459,6 +460,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
459460 });
460461
461462 cases.add("macro line continuation",
463 \\int BAR = 0;
462464 \\#define FOO -\
463465 \\BAR
464466 , &[_][]const u8{
......@@ -1833,6 +1835,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18331835 });
18341836
18351837 cases.add("macro pointer cast",
1838 \\#define NRF_GPIO_BASE 0
18361839 \\typedef struct { int dummy; } NRF_GPIO_Type;
18371840 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
18381841 , &[_][]const u8{
......@@ -1873,6 +1876,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18731876 });
18741877
18751878 cases.add("macro add",
1879 \\#define D3_AHB1PERIPH_BASE 0
18761880 \\#define PERIPH_BASE (0x40000000UL) /*!< Base address of : AHB/APB Peripherals */
18771881 \\#define D3_APB1PERIPH_BASE (PERIPH_BASE + 0x18000000UL)
18781882 \\#define RCC_BASE (D3_AHB1PERIPH_BASE + 0x4400UL)
......@@ -3138,6 +3142,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31383142 \\#define FOO(bar) baz((void *)(baz))
31393143 \\#define BAR (void*) a
31403144 \\#define BAZ (uint32_t)(2)
3145 \\#define a 2
31413146 , &[_][]const u8{
31423147 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) {
31433148 \\ _ = bar;
......@@ -3160,6 +3165,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31603165 });
31613166
31623167 cases.add("macro conditional operator",
3168 \\ int a, b, c;
31633169 \\#define FOO a ? b : c
31643170 , &[_][]const u8{
31653171 \\pub const FOO = if (a) b else c;
......@@ -3649,4 +3655,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
36493655 ,
36503656 \\_ = p[@intCast(c_uint, @as(c_int, 1))];
36513657 });
3658
3659 cases.add("Undefined macro identifier",
3660 \\#define FOO BAR
3661 , &[_][]const u8{
3662 \\pub const FOO = @compileError("unable to translate macro: undefined identifier `BAR`");
3663 });
36523664}