From: tangxiaofeng <tangxiaofeng@kylinos.cn>
Date: Mon, 21 Sep 2026 03:31:56 +0000
Subject: [PATCH] Fix iterator member lookup in C++ wrapper generation

scripts/wrap/cpp.py:class_add_iterator() looked up the iterator member
with parse.find_name() using the typedef cursor. With clang-22 the
typedef cursor resolves to a forward declaration without members, so
find_name() returned None and wrapper generation aborted with:

  AttributeError: 'NoneType' object has no attribute 'type'

Resolve the struct definition cursor first, mirroring the workaround in
parse.has_refs(), so that the iterator member is found.

Author: tangxiaofeng <tangxiaofeng@kylinos.cn>
Origin: vendor
Forwarded: no
Last-Update: 2026-09-21
--- mupdf-1.27.0+ds1.orig/scripts/wrap/cpp.py
+++ mupdf-1.27.0+ds1/scripts/wrap/cpp.py
@@ -2361,7 +2361,15 @@ def class_add_iterator( tu, struct_curso
     # Figure out type of what the iterator returns by looking at type of
     # <it_begin>.
     if it_begin:
+        # clang-22 may resolve the typedef cursor to a forward declaration
+        # (which has no members), making find_name() return None below.
+        # Resolve the struct definition cursor first, mirroring the workaround
+        # in parse.has_refs() for the same libclang behaviour.
+        struct_cursor_definition = parse.find_struct_definition( tu, struct_name)
+        if struct_cursor_definition:
+            struct_cursor = struct_cursor_definition
         c = parse.find_name( struct_cursor, it_begin)
+        assert c is not None, f'Cannot find iterator member {it_begin!r} in struct {struct_name!r}'
         assert c.type.kind == state.clang.cindex.TypeKind.POINTER
         it_internal_type = state.get_name_canonical( c.type.get_pointee()).spelling
         it_internal_type = util.clip( it_internal_type, 'struct ')
