Skip to content

Commit 471dc87

Browse files
authored
frontend-c: Fix enum extraction for c project (#2165)
* frontend-c: Fix enum extraction for c project Signed-off-by: Arthur Chan <[email protected]> * Fix formatting Signed-off-by: Arthur Chan <[email protected]> --------- Signed-off-by: Arthur Chan <[email protected]>
1 parent 47e247f commit 471dc87

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/fuzz_introspector/frontends/frontend_c.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -613,21 +613,39 @@ def extract_types(self):
613613
for _, enums in enum_query_res.items():
614614
for enum in enums:
615615
enum_name_field = enum.child_by_field_name('name')
616-
enum_body_field = enum.child_by_field_name('body')
616+
enum_body = enum.child_by_field_name('body')
617617
if not enum_name_field:
618618
# Skip anonymous enum
619619
continue
620-
if not enum_body_field:
620+
if not enum_body:
621621
# Skip forward declaration
622622
continue
623623

624+
enum_item_query = self.tree_sitter_lang.query(
625+
'( enumerator ) @en')
626+
enumerator_list = []
627+
for _, enumerators in enum_item_query.captures(
628+
enum_body).items():
629+
for enumerator in enumerators:
630+
item_dict = {}
631+
enum_item_name = enumerator.child_by_field_name('name')
632+
enum_item_value = enumerator.child_by_field_name(
633+
'value')
634+
635+
if not enum_item_name:
636+
# Skip anonymous enum items
637+
continue
638+
item_dict['name'] = enum_item_name.text.decode()
639+
640+
if enum_item_value:
641+
item_dict['value'] = enum_item_value.text.decode()
642+
643+
enumerator_list.append(item_dict)
644+
624645
self.enum_defs.append({
625-
'name':
626-
enum_name_field.text.decode(),
627-
'definition':
628-
enum_body_field.text.decode(),
629-
'item_type':
630-
'enum',
646+
'name': enum_name_field.text.decode(),
647+
'enumerators': enumerator_list,
648+
'item_type': 'enum',
631649
'pos': {
632650
'source_file': self.source_file,
633651
'line_start': enum.start_point.row,

0 commit comments

Comments
 (0)