From c136c86b75855e49ad7f0a27774683a23e55d7c0 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Sat, 8 Mar 2025 13:12:25 +0100 Subject: [PATCH 1/2] db: add warning when enum cannot be found --- tools/regz/src/atdf.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/regz/src/atdf.zig b/tools/regz/src/atdf.zig index a03296d75..640d91e23 100644 --- a/tools/regz/src/atdf.zig +++ b/tools/regz/src/atdf.zig @@ -676,7 +676,10 @@ fn load_field(ctx: *Context, node: xml.Node, peripheral_struct_id: StructID, par .size_bits = 1, .offset_bits = i, .enum_id = if (node.get_attribute("values")) |values| blk: { - const e = try db.get_enum_by_name(arena, peripheral_struct_id, values); + const e = db.get_enum_by_name(arena, peripheral_struct_id, values) catch |err| { + log.warn("{s} failed to get_enum_by_name: {s}", .{ name, values }); + return err; + }; break :blk e.id; } else null, }); @@ -722,7 +725,10 @@ fn load_field(ctx: *Context, node: xml.Node, peripheral_struct_id: StructID, par .size_bits = width, .offset_bits = offset, .enum_id = if (node.get_attribute("values")) |values| blk: { - const e = try db.get_enum_by_name(arena, peripheral_struct_id, values); + const e = db.get_enum_by_name(arena, peripheral_struct_id, values) catch |err| { + log.warn("{s} failed to get_enum_by_name: {s}", .{ name, values }); + return err; + }; break :blk e.id; } else null, }); From c0101af7e19b6e50b3e821ee12a8b8fe7ac5223f Mon Sep 17 00:00:00 2001 From: oliverpool Date: Sat, 8 Mar 2025 12:33:59 +0100 Subject: [PATCH 2/2] db: get_enum_by_name fallbacks to the parent when missing --- tools/regz/src/Database.zig | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index bd2b3d556..28739dd45 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -1347,7 +1347,42 @@ pub fn get_enum_by_name( return db.one_alloc(Enum, allocator, query, .{ .struct_id = struct_id, .name = name, + }) catch |err| switch (err) { + error.MissingEntity => { + // lookup the enum among the parents + var parent_id = struct_id; + var i: u8 = 0; + const max_depth = 10; + while (i <= max_depth) : (i += 1) { + parent_id = db.get_parent_struct_id(parent_id) catch { + return err; + }; + + log.debug("get_enum_by_name: parent_id={} name='{s}'", .{ parent_id, name }); + return db.one_alloc(Enum, allocator, query, .{ + .struct_id = parent_id, + .name = name, + }) catch { + continue; + }; + } + return err; + }, + else => { + return err; + }, + }; +} + +fn get_parent_struct_id(db: *Database, struct_id: StructID) !StructID { + var stmt = try db.sql.prepare("SELECT parent_id FROM struct_decls WHERE struct_id = ?"); + defer stmt.deinit(); + + const row = try stmt.one(StructID, .{}, .{ + .struct_id = struct_id, }); + + return if (row) |parent_id| parent_id else error.MissingEntity; } fn one_alloc(