generator2: Skip anonymous structs/unions

The Python code has been relying on `spelling` to return `None` for skipping
anonymous structs/unions.

libclang has been returning a "spelling" for those for a while now (LLVM 16
introduced this in its branch in 2022), though, so this check no longer works.
Use the correct method `clang.CIndex.is_anonymous()` instead.

PiperOrigin-RevId: 557099905
Change-Id: I13707509dbae03481c5edce7fa92554cefdd57e7
This commit is contained in:
Christian Blichmann 2023-08-15 05:20:14 -07:00 committed by Copybara-Service
parent 352d1f8fb2
commit ae3d334cc2
2 changed files with 13 additions and 22 deletions

View File

@ -327,8 +327,9 @@ class Type(object):
"""Returns all types related to the structure."""
# skip unnamed structures eg. typedef struct {...} x;
# struct {...} will be rendered as part of typedef rendering
if self._get_declaration().spelling and not skip_self:
self._tu.search_for_macro_name(self._get_declaration())
decl = self._get_declaration()
if not decl.is_anonymous() and not skip_self:
self._tu.search_for_macro_name(decl)
result.add(self)
for f in self._clang_type.get_fields():

View File

@ -327,23 +327,16 @@ class CodeAnalysisTest(parameterized.TestCase):
types = args[0].get_related_types()
names = [t._clang_type.spelling for t in types]
self.assertLen(types, 4)
self.assertSameElements(names, [
'struct_2', 'struct_1::(unnamed struct at tmp.cc:6:9)', 'uint',
'struct_1'
])
self.assertLen(types, 3)
self.assertSameElements(names, ['struct_2', 'uint', 'struct_1'])
types = args[1].get_related_types()
names = [t._clang_type.spelling for t in types]
self.assertLen(types, 3)
self.assertSameElements(
names, ['struct_1', 'struct_1::(unnamed struct at tmp.cc:6:9)', 'uint'])
self.assertLen(types, 2)
self.assertSameElements(names, ['struct_1', 'uint'])
names = [t._clang_type.spelling for t in generator._get_related_types()]
self.assertEqual(names, [
'uint', 'struct_1', 'struct_1::(unnamed struct at tmp.cc:6:9)',
'struct_2', 'struct_a'
])
self.assertEqual(names, ['uint', 'struct_1', 'struct_2', 'struct_a'])
types = args[2].get_related_types()
self.assertLen(types, 1)
@ -380,16 +373,13 @@ class CodeAnalysisTest(parameterized.TestCase):
types = args[0].get_related_types()
names = [t._clang_type.spelling for t in types]
self.assertLen(types, 4)
self.assertSameElements(names, [
'union_2', 'union_1::(unnamed union at tmp.cc:6:9)', 'uint', 'union_1'
])
self.assertLen(types, 3)
self.assertSameElements(names, ['union_2', 'uint', 'union_1'])
types = args[1].get_related_types()
names = [t._clang_type.spelling for t in types]
self.assertLen(types, 3)
self.assertSameElements(
names, ['union_1', 'union_1::(unnamed union at tmp.cc:6:9)', 'uint'])
self.assertLen(types, 2)
self.assertSameElements(names, ['union_1', 'uint'])
# Extra check for generation, in case rendering throws error for this test.
generator.generate('Test', [], 'sapi::Tests', None, None)
@ -648,7 +638,7 @@ class CodeAnalysisTest(parameterized.TestCase):
self.assertLen(functions, 1)
types = generator._get_related_types()
self.assertLen(types, 3)
self.assertLen(types, 2)
self.assertEqual('typedef unsigned int uint', types[0].stringify())
self.assertMultiLineEqual(expected, types[1].stringify())