From 871d7e11ac8ebbcf2a4deea9be826466b5dcc281 Mon Sep 17 00:00:00 2001 From: Esteban Zimanyi Date: Thu, 19 Nov 2026 19:16:11 +0100 Subject: [PATCH] Name a record declared without a tag by its typedef MEOS declares four structures without a tag, `typedef struct {...} GeomVal;` and `Pixel`, `ways_record` and `SERIALIZED_PATCH` the same way. C names such a record by its typedef alone, while libclang spells it `struct (unnamed at meos_raster.h:130:9)`. _canonical_spelling replaces the `(unnamed at ...)` part by the typedef and keeps the keyword before it, so a slot reads `struct GeomVal *`, a tag no header declares, which a binding spelling the type as C does cannot compile (GoMEOS's cgo `C.struct_GeomVal`). And parse_meos takes as a record's typedef any typedef whose type contains the record, so a callback taking a `ways_record *` (`ways_fetch_fn`) can stand for the layout of `ways_record`. _canonical_spelling replaces the keyword with the `(unnamed ...)` part, in every form libclang writes it, and parse_meos takes as a record's typedef only a typedef whose type is that record. Witness: the catalog of MobilityDB 87c8d33e4d, generated by master's parser, spells those four records by a tag 13 times (`struct GeomVal`, `struct Pixel`, `struct ways_record`, `struct SERIALIZED_PATCH`, with and without a pointer). Measured: the same catalog generated by this branch spells them by a tag 0 times and by their typedef 30 times, the `STBox` control unchanged at 2469. tests/test_anonymous_records.py (4 tests) checks the spellings libclang gives and the catalog's slots and structures. The suite runs 700 passed against master's 696 on that catalog, and the 6 tests that fail on master (test_family, test_struct_layout, test_typerecover) fail the same with this branch and none other. Why: the owner's ruling of 2026-11-19 ("as you recommend") that this fix, kept from a worktree sweep, becomes a pull request. --- parser/extractors.py | 12 +++-- parser/parser.py | 6 ++- tests/test_anonymous_records.py | 94 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tests/test_anonymous_records.py diff --git a/parser/extractors.py b/parser/extractors.py index 3c40a12..cf5838a 100644 --- a/parser/extractors.py +++ b/parser/extractors.py @@ -7,13 +7,19 @@ def _canonical_spelling(ty) -> str: - # Return canonical type spelling, resolving anonymous typedef-struct names. + # Return canonical type spelling, resolving anonymous typedef-struct names. A + # record declared without a tag (`typedef struct {...} GeomVal;`) is named by its + # typedef alone, as #parse_meos names the record itself through its typedef map, + # so the keyword libclang writes before `(unnamed at ...)` goes with it: + # `struct (unnamed at meos_raster.h:130:9) *` is `GeomVal *`, a type C declares, + # where `struct GeomVal *` names a tag no header declares. canonical = ty.get_canonical().spelling - if "(unnamed at" not in canonical: + if "(unnamed" not in canonical: return canonical c = ty.spelling base_name = re.sub(r"\bconst\b", "", c).replace("*", "").strip() - return re.sub(r"\(unnamed at [^)]+\)", base_name, canonical) + return re.sub(r"\b(?:struct|union|enum)\s+\(unnamed(?: struct| union| enum)? at [^)]+\)" + r"|\(unnamed(?: struct| union| enum)? at [^)]+\)", base_name, canonical) # Spellings that always represent a boolean type, regardless of the underlying C representation: diff --git a/parser/parser.py b/parser/parser.py index 6cacfd2..5bd862a 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -173,8 +173,12 @@ def parse_meos(entry: Path, include_dir: Path, if not loc or str(Path(loc.name).resolve()) not in own_files: continue if node.kind == clang.cindex.CursorKind.TYPEDEF_DECL: + # A typedef names an anonymous record when the record IS the type it + # defines (`typedef struct {...} ways_record;`); a typedef whose type only + # contains it, a function pointer taking a `ways_record *` + # (`ways_fetch_fn`), names something else canonical = node.underlying_typedef_type.get_canonical().spelling - m = re.search(r"\(unnamed at ([^)]+)\)", canonical) + m = re.fullmatch(r"(?:struct|union)\s+\(unnamed at ([^)]+)\)", canonical) if m: typedef_map[m.group(1)] = node.spelling diff --git a/tests/test_anonymous_records.py b/tests/test_anonymous_records.py new file mode 100644 index 0000000..c9ff4b8 --- /dev/null +++ b/tests/test_anonymous_records.py @@ -0,0 +1,94 @@ +"""A record declared without a tag is named by its typedef, in its slots and as a structure. + +MEOS declares several structures without a tag (``typedef struct {...} GeomVal;``, +``Pixel``, ``ways_record``, ``SERIALIZED_PATCH``). C names such a record by its typedef +alone: a slot spelling it ``struct GeomVal *`` names a tag no header declares, which a +binding spelling the type as C does (GoMEOS's cgo ``C.struct_GeomVal``) cannot compile. +And the structure the catalog lays out is the one the typedef defines, never a typedef +whose type only contains the record: ``ways_fetch_fn``, a function pointer taking a +``ways_record *``, is a callback, not the layout of ``ways_record``. + +The catalog half mirrors #StructLayoutTests of tests/test_struct_layout.py: it reads the +generated IDL and skips where there is none; run ``python run.py`` first. The spelling +half runs #_canonical_spelling of parser/extractors.py on the spellings libclang gives. +""" +import json +import re +import unittest +from pathlib import Path + +from parser.extractors import _canonical_spelling + +IDL = Path(__file__).resolve().parents[1] / "output" / "meos-idl.json" + + +class _Type: + """The two spellings #_canonical_spelling reads of a libclang type.""" + + def __init__(self, spelling, canonical): + self.spelling = spelling + self._canonical = canonical + + def get_canonical(self): + return _Type(self._canonical, self._canonical) + + +class AnonymousRecordSpellingTests(unittest.TestCase): + def test_an_untagged_record_is_spelled_by_its_typedef(self): + at = "(unnamed at /x/meos/include/meos_raster.h:130:9)" + cases = { + ("GeomVal *", f"struct {at} *"): "GeomVal *", + ("const GeomVal *", f"const struct {at} *"): "const GeomVal *", + ("GeomVal", f"struct {at}"): "GeomVal", + ("GeomVal *", "struct (unnamed struct at /x/meos_raster.h:130:9) *"): "GeomVal *", + ("Span *", "struct Span *"): "struct Span *", + } + for (spelling, canonical), expected in cases.items(): + self.assertEqual(_canonical_spelling(_Type(spelling, canonical)), expected, + canonical) + + +class AnonymousRecordCatalogTests(unittest.TestCase): + def setUp(self): + if not IDL.exists(): + self.skipTest(f"{IDL} not generated; run `python run.py` first") + self.idl = json.loads(IDL.read_text()) + + def test_no_slot_names_an_unnamed_record(self): + found = set() + + def visit(o): + if isinstance(o, dict): + if isinstance(o.get("canonical"), str) and "unnamed" in o["canonical"]: + found.add(o["canonical"]) + for v in o.values(): + visit(v) + elif isinstance(o, list): + for v in o: + visit(v) + + visit(self.idl) + self.assertEqual(sorted(found), []) + + def test_the_raster_records_are_their_typedefs(self): + by_name = {f["name"]: f for f in self.idl["functions"]} + if "geomval_arr_free" not in by_name: + self.skipTest("the catalog was built without the raster family") + self.assertEqual(by_name["geomval_arr_free"]["params"][0]["canonical"], "GeomVal *") + self.assertEqual(by_name["pixel_arr_free"]["params"][0]["canonical"], "Pixel *") + + def test_a_structure_is_never_a_callback(self): + structs = {s["name"] for s in self.idl["structs"]} + callbacks = {c["name"] for c in self.idl.get("callbacks", [])} + self.assertEqual(sorted(structs & callbacks), []) + if any(f["name"] == "ways_cache_lookup" for f in self.idl["functions"]): + self.assertIn("ways_record", structs) + self.assertNotIn("ways_fetch_fn", structs) + self.assertFalse(any(re.search(r"\bstruct\s+ways_record\b", p["canonical"]) + for f in self.idl["functions"] + if f["name"] == "ways_cache_lookup" + for p in f["params"])) + + +if __name__ == "__main__": + unittest.main()