Skip to content

Commit

Permalink
parser: change naming of typedef of anonymous entities to fix clang 16+
Browse files Browse the repository at this point in the history
Starting from libclang 16, typedefs of anonymous entities such as

	typedef struct {
		/* ... */
	} foo;

will no longer have an empty name for cursor.spelling. Instead, the
returned name will be cursor.type.spelling, matching the name of the
typedef, i.e. "foo" in the above case.

cursor.is_anonymous() will also be false for such entities, regardless
of libclang version.

Rather than try to coerce libclang 16 to the @anonymous_<sha1> naming
convention we have, manually use the same naming for libclang 15 and
earlier. It's arguably a more useful name anyway.

We first have to distinguish regular anonymous entities using
cursor.is_anonymous(). For libclang 15 and earlier, to distinguish from
typedef of anonyous entities. For libclang 16 and later, the name will
be of the form "unnamed ... at /path/to/file".
  • Loading branch information
jnikula committed Oct 21, 2023
1 parent 09f4799 commit a1544cf
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/hawkmoth/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,15 @@ def _type_definition_fixup(cursor):
type_elem = []

# Short cut for anonymous symbols.
if cursor.spelling == '':
if cursor.is_anonymous():
return None

# libclang 16 and later have cursor.spelling == cursor.type.spelling for
# typedefs of anonymous entities, while libclang 15 and earlier have an
# empty string. Match the behaviour across libclang versions.
if cursor.spelling == '':
return cursor.type.spelling

type_elem.extend(_specifiers_fixup(cursor, cursor.type))

colon_suffix = ''
Expand Down
2 changes: 1 addition & 1 deletion test/c/typedef-enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
named enumeration


.. c:enum:: @anonymous_90372874a3c8c25dccf983612f39e93f
.. c:enum:: unnamed_t
unnamed typedeffed enum

Expand Down
2 changes: 1 addition & 1 deletion test/c/typedef-struct.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
named member


.. c:struct:: @anonymous_7f9a1d628cd33f3227f3fcdc3a405aa6
.. c:struct:: typedef_struct
unnamed typedeffed struct

Expand Down
2 changes: 1 addition & 1 deletion test/cpp/typedef-enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
named enumeration


.. cpp:enum:: @anonymous_90372874a3c8c25dccf983612f39e93f
.. cpp:enum:: unnamed_t

unnamed typedeffed enum

Expand Down
2 changes: 1 addition & 1 deletion test/cpp/typedef-struct.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
named member


.. cpp:struct:: @anonymous_7f9a1d628cd33f3227f3fcdc3a405aa6
.. cpp:struct:: typedef_struct

unnamed typedeffed struct

Expand Down

0 comments on commit a1544cf

Please sign in to comment.