Skip to content

Commit

Permalink
parser: always convert _Bool to bool
Browse files Browse the repository at this point in the history
We see _Bool in documentation, because that's the C keyword, and
stdbool.h defines bool to _Bool rather than typedefs it.

Stop overcomplicating the matter, and just unconditionally convert _Bool
to bool. 'bool' is what most people want to see in code and
documentation. It should really be that simple.

As far as special casing goes, bool is easily special enough. C++ and
the new C23 have bool keywords.

Previously I thought we should at least check whether the code
originally had '_Bool' and preserve that, but now that we have C++
support, GCC and Clang stdbool.h actually also define the reverse
(#define _Bool bool) for C++. It gets overly complicated.

With the current type fixups in place, the fix is just a few lines of
code, and I find it hard to justify adding any more code for this.

Fixes #92.
  • Loading branch information
jnikula committed Oct 5, 2023
1 parent 0726141 commit ca39c67
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/hawkmoth/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ def _get_scopedenum_type(cursor):
return f': {cursor.enum_type.spelling}'
return None

def _convert_bool(type_string):
return 'bool' if type_string == '_Bool' else type_string

def _var_type_fixup(cursor, domain):
"""Fix non trivial variable and argument types.
Expand Down Expand Up @@ -455,7 +458,7 @@ def _var_type_fixup(cursor, domain):
if len(args) == 0:
args.append('void')

ret_type = cursor_type.get_result().spelling
ret_type = _convert_bool(cursor_type.get_result().spelling)

name = f'''{pad(ret_type)}({pad(stars_and_quals)}{cursor.spelling}{dims})({', '.join(args)})''' # noqa: E501
else:
Expand All @@ -471,6 +474,9 @@ def _var_type_fixup(cursor, domain):

name = cursor.spelling + dims

# Convert _Bool to bool
type_elem = [_convert_bool(t) for t in type_elem]

ttype = ' '.join(type_elem)
return ttype, name

Expand Down Expand Up @@ -529,7 +535,7 @@ def _function_fixup(cursor, domain):
if template_line:
full_type.append(template_line)

full_type.append(cursor.result_type.spelling)
full_type.append(_convert_bool(cursor.result_type.spelling))

ttype = ' '.join(full_type)

Expand Down

0 comments on commit ca39c67

Please sign in to comment.