Skip to content

Commit

Permalink
Some slight corrections for subtle inaccuracies
Browse files Browse the repository at this point in the history
  • Loading branch information
earowley committed Aug 4, 2022
1 parent f3ca1d2 commit f8cce80
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion scripts/_pxdgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def run(self):
if len(fwd_decls):
for line in block(fwd_decls, "toplevel", "cdef extern from *:", False):
fwd.writeline(line)
elif self.opts.output or not self.opts.recursive:
elif self.opts.output or not self.opts.recursive or self.opts.recursive and self.opts.whitelist:
for i in pxspace.import_strings(FLAG_IMPORT_ALL in self.flags or self.opts.recursive):
imports.add(i)

Expand Down
12 changes: 10 additions & 2 deletions src/pxdgen/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
TAB = ' ' * TAB_LENGTH
RE_DECLTYPE = re.compile("decltype\(.+\)")
RE_CPP_INCLUDE = re.compile(" *#include *[<\"].+\.h(pp)?[>\"] *")
RE_CPP_INT = re.compile("\d{1,20}")
RE_CPP_FLOAT = re.compile("\d+(\.\d+)?[fF]?")
RE_CPP_INT = re.compile("-?\d{1,20}[uU]?")
RE_CPP_HEX = re.compile("-?0[xX][0-9A-Fa-f]+[uU]?")
RE_CPP_FLOAT = re.compile("-?\d+(\.\d+)?[fF]?")

SPACE_KINDS = (
kinds.STRUCT_DECL,
Expand Down Expand Up @@ -63,6 +64,12 @@
kinds.ENUM_DECL,
kinds.UNION_DECL
)
STRUCT_ATTR_KINDS = (
kinds.ALIGNED_ATTR,
kinds.PACKED_ATTR,
kinds.WARN_UNUSED_ATTR,
kinds.VISIBILITY_ATTR
)
TYPE_REFS = (
kinds.TYPE_REF,
kinds.TEMPLATE_REF
Expand All @@ -85,6 +92,7 @@
"sigjmp_buf": "libc.setjmp",
"sig_handler_t": "libc.signal",
"sig_atomic_t": "libc.signal",
"wchar_t": "libc.stddef",
"int8_t": "libc.stdint",
"int16_t": "libc.stdint",
"int32_t": "libc.stdint",
Expand Down
14 changes: 13 additions & 1 deletion src/pxdgen/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,30 @@ def __init__(self, cursor):
super().__init__(cursor)

def lines(self, **kwargs) -> Generator[str, None, None]:
"""
The lines for this macro definition.
@param kwargs: Optional keyword arguments.
@return: Generator[str]
"""
macro_type = "const int"
tokens = [t.spelling for t in self.cursor.get_tokens()]
func = self.cursor.is_macro_function()
i = 1 if not func else (tokens.index(')') + 1)
macro_def = ''.join(tokens[i:])
eq = True

if re.fullmatch(RE_CPP_INT, macro_def):
macro_type = "const long"
elif re.fullmatch(RE_CPP_HEX, macro_def):
macro_type = "const unsigned long"
elif re.fullmatch(RE_CPP_FLOAT, macro_def):
macro_type = "const double"
macro_def = macro_def.strip("fF")
else:
eq = False

yield f"{macro_type} {tokens[0]}{'(...)' if func else ''}"
yield f"{macro_type} {tokens[0]}{'(...)' if func else f' = {macro_def}' if eq else ''}"


class Function(CCursor):
Expand Down
5 changes: 5 additions & 0 deletions src/pxdgen/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,16 @@ def is_cppclass(cursor: clang.cindex.Cursor) -> bool:

# C++ struct decl that is not C-compliant
for child in cursor.get_children():
# C allows fields
if child.kind == clang.cindex.CursorKind.FIELD_DECL:
continue
# There can be anonymous structs and enumerations as fields
if child.kind in ANON_KINDS and child.is_anonymous():
continue
# Make sure common attributes do not convert to C++ class
if child.kind in STRUCT_ATTR_KINDS:
continue

return True

return False
Expand Down

0 comments on commit f8cce80

Please sign in to comment.