Skip to content

typedef in macro cases failure #140

@jmgurney

Description

@jmgurney

This is related to #84 as if you do fix this, it is possible that this won't work till that bug is fixed as well.

File mindef2.h:

#include "mindef3.h"

#define NGTCP2_SECONDS ((ngtcp2_duration)1000000000ULL)

File mindef3.h:

#include <stdint.h>

typedef uint64_t ngtcp2_duration;

Command:

clang2py --debug -k cdefstum mindef3.h mindef2.h

results in a crash:

DEBUG:cursorhandler:literal has 7 tokens.[ NGTCP2_SECONDS ( ( ngtcp2_duration ) 1000000000ULL ) ]
DEBUG:cursorhandler:cursor.type:INVALID
DEBUG:cursorhandler:token:NGTCP2_SECONDS tk.kd: IDENTIFIER tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:( tk.kd:PUNCTUATION tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:( tk.kd:PUNCTUATION tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:ngtcp2_duration tk.kd: IDENTIFIER tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
Typedef(name=ngtcp2_duration,typ=FundamentalType(...),location=('mindef3.h', 3),comment=None)
Traceback (most recent call last):
  File "/tmp/ctg/bin/clang2py", line 8, in <module>
    sys.exit(main())
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/clang2py.py", line 346, in main
    translate_files(inputs.files, outputs.stream, cfg)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/codegenerator.py", line 1165, in translate_files
    translator.parse_input_files(source_files)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/codegenerator.py", line 1065, in parse_input_files
    self.parser.parse(srcfile)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/clangparser.py", line 130, in parse
    self.start_element(node)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/clangparser.py", line 186, in start_element
    stop_recurse = self.parse_cursor(node)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/clangparser.py", line 324, in parse_cursor
    return self.cursorkind_handler.parse_cursor(cursor)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/cursorhandler.py", line 38, in parse_cursor
    return mth(cursor)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/util.py", line 122, in fn
    return func(*args, **kwargs)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/cursorhandler.py", line 1114, in MACRO_DEFINITION
    tokens = self._literal_handling(cursor)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/util.py", line 122, in fn
    return func(*args, **kwargs)
  File "/private/tmp/ctg/lib/python3.10/site-packages/ctypeslib/codegen/cursorhandler.py", line 551, in _literal_handling
    value = self.get_registered(value).body
AttributeError: 'Typedef' object has no attribute 'body'

This is because it finds the cast, and tries to replace the cast w/ a macro instead.

This requires special construction, because if you don't specify the mindef3.h it doesn't crash, because the macro parsing code processes the macros before parsing the C code, so doesn't find the typedef, and instead treats the cast an an undefined and therefor invalid macro and skips it:

DEBUG:cursorhandler:literal has 7 tokens.[ NGTCP2_SECONDS ( ( ngtcp2_duration ) 1000000000ULL ) ]
DEBUG:cursorhandler:cursor.type:INVALID
DEBUG:cursorhandler:token:NGTCP2_SECONDS tk.kd: IDENTIFIER tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:( tk.kd:PUNCTUATION tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:( tk.kd:PUNCTUATION tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:ngtcp2_duration tk.kd: IDENTIFIER tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:Undefined MACRO_DEFINITION token identifier : ngtcp2_duration
DEBUG:cursorhandler:token:) tk.kd:PUNCTUATION tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:1000000000ULL tk.kd:    LITERAL tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:token:) tk.kd:PUNCTUATION tk.cursor.kd:MACRO_DEFINITION cursor.kd:MACRO_DEFINITION
DEBUG:cursorhandler:_literal_handling final_value: ['NGTCP2_SECONDS', '(', '(', UndefinedIdentifier(name=ngtcp2_duration), ')', '1000000000', ')']
DEBUG:cursorhandler:MACRO: #define NGTCP2_SECONDS((ngtcp2_duration) 1000000000)
DEBUG:clangparser:register: NGTCP2_SECONDS 

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions