From: tangxiaofeng <tangxiaofeng@kylinos.cn>
Date: Mon, 21 Sep 2026 04:25:32 +0000
Subject: Fix wrapper generation with libclang's anonymous record spellings

scripts/wrap/parse.py:get_members() detected anonymous records (e.g. the
`union {...} u;` inside `struct fz_stext_block`) by testing for an empty
cursor .spelling. libclang 21/22 instead return a non-empty spelling such
as `(unnamed union at include/mupdf/fitz/structured-text.h:431:2)` when the
translation unit is parsed as C++, so the anonymous record declaration was
returned as if it were a member, the fallback to Type.get_fields() never
happened, and nested member lookups such as `u.t.first_line` failed:

  AssertionError: Cannot find iterator member 'u.t.first_line' in struct
  'fz_stext_block'

Add is_anonymous_spelling()/is_anonymous_cursor() which recognise both the
empty and the `(unnamed ...)` spellings, and use them in get_members().
clang_Cursor_isAnonymous() is deliberately not used: it also reports true
for a named field whose type is an anonymous record, which would drop real
members.

Author: tangxiaofeng <tangxiaofeng@kylinos.cn>
Origin: vendor
Forwarded: no
Last-Update: 2026-09-21

--- mupdf-1.27.0+ds1.orig/scripts/wrap/parse.py
+++ mupdf-1.27.0+ds1/scripts/wrap/parse.py
@@ -91,6 +91,49 @@ def get_children(cursor):
                 jlib.log( '{cursor.spelling=}')
             yield cursor
 
+def is_anonymous_spelling( spelling):
+    '''
+    Returns true if <spelling> is empty or looks like libclang's name for an
+    anonymous struct/union/class/enum, for example:
+
+        ''
+        '(unnamed union at include/mupdf/fitz/structured-text.h:431:2)'
+        'union (unnamed at include/mupdf/fitz/structured-text.h:431:2)'
+        'unnamed struct at include/mupdf/fitz/structured-text.h:431:2'
+
+    Older versions of libclang returned '' as the .spelling of an anonymous
+    record; newer versions (clang-21/22) return a non-empty '(unnamed ...)'
+    string instead, and the exact form depends on whether the translation unit
+    is parsed as C or C++. Code that detects anonymous records by testing for
+    an empty .spelling therefore stops working, so we recognise all forms here.
+    '''
+    if not spelling:
+        return True
+    if 'unnamed' not in spelling:
+        return False
+    return ' at ' in spelling or spelling.startswith( 'unnamed ')
+
+def is_anonymous_cursor( cursor):
+    '''
+    Returns true if <cursor> is for an anonymous struct/union/class/enum, e.g.
+    the `union {...}` in `union {...} u;`.
+
+    We deliberately do not use clang_Cursor_isAnonymous() here: with
+    clang-21/22 it also returns true for a *named* field whose type is an
+    anonymous record (e.g. the `u` in `union {...} u;`), which would make us
+    drop real members.
+    '''
+    if not cursor.spelling:
+        return True
+    if cursor.kind not in (
+            clang.cindex.CursorKind.STRUCT_DECL,
+            clang.cindex.CursorKind.UNION_DECL,
+            clang.cindex.CursorKind.CLASS_DECL,
+            clang.cindex.CursorKind.ENUM_DECL,
+            ):
+        return False
+    return is_anonymous_spelling( cursor.spelling)
+
 def get_members( type_or_cursor, include_empty=False):
     '''
     Yields cursor for each member. Uses whichever of
@@ -155,7 +198,10 @@ def get_members( type_or_cursor, include
 
     ret = list()
     for cursor3 in cursor2.get_children():
-        if include_empty or cursor3.spelling:
+        # Anonymous records (e.g. the `union {...} u;` inside `struct
+        # fz_stext_block`) are not members themselves; skipping them makes us
+        # fall back to Type.get_fields() below, which returns the real members.
+        if include_empty or not is_anonymous_cursor( cursor3):
             ret.append(cursor3)
     if not ret:
         type_ = cursor.type.get_canonical()
