Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions parser/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
94 changes: 94 additions & 0 deletions tests/test_anonymous_records.py
Original file line number Diff line number Diff line change
@@ -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()
Loading