Skip to content

Commit

Permalink
Move the set of reserved C keywords to Naming.py and add the known re…
Browse files Browse the repository at this point in the history
…served C++ keywords. Update both sets to the C(++)23 standards.
  • Loading branch information
scoder committed Sep 20, 2024
1 parent 8525602 commit b8b5a24
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 10 deletions.
166 changes: 166 additions & 0 deletions Cython/Compiler/Naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,169 @@ def py_version_hex(major, minor=0, micro=0, release_level=0, release_serial=0):
('__pyx_CoroutineAwaitType', '__Pyx_Coroutine_USED'),
('__pyx_CoroutineType', '__Pyx_Coroutine_USED'),
]


iso_c23_keywords = {
'alignas', # (C23)
'alignof', # (C23)
'auto',
'bool', # (C23)
'break',
'case',
'char',
'const',
'constexpr', # (C23)
'continue',
'default',
'do',
'double',
'else',
'enum',
'extern',
'false', # (C23)
'float',
'for',
'goto',
'if',
'inline', # (C99)
'int',
'long',
'nullptr', # (C23)
'register',
'restrict', # (C99)
'return',
'short',
'signed',
'sizeof',
'static',
'static_assert', # (C23)
'struct',
'switch',
'thread_local', # (C23)
'true', # (C23)
'typedef',
'typeof', # (C23)
'typeof_unqual', # (C23)
'union',
'unsigned',
'void',
'volatile',
'while',
'_Alignas', # (C11)
'_Alignof', # (C11)
'_Atomic', # (C11)
'_BitInt', # (C23)
'_Bool', # (C99)
'_Complex', # (C99)
'_Decimal128', # (C23)
'_Decimal32', # (C23)
'_Decimal64', # (C23)
'_Generic', # (C11)
'_Imaginary', # (C99)
'_Noreturn', # (C11)
'_Static_assert', # (C11)
'_Thread_local', # (C11)
}


iso_cpp23_keywords = {
'alignas', # (C++11)
'alignof', # (C++11)
'and',
'and_eq',
'asm',
'atomic_cancel', # (TM TS)
'atomic_commit', # (TM TS)
'atomic_noexcept', # (TM TS)
'auto',
'bitand',
'bitor',
'bool',
'break',
'case',
'catch',
'char',
'char8_t', # (C++20)
'char16_t', # (C++11)
'char32_t', # (C++11)
'class',
'compl',
'concept', # (C++20)
'const',
'consteval', # (C++20) (5)
'constexpr', # (C++11) (3)
'constinit', # (C++20)
'const_cast',
'continue',
'co_await', # (C++20)
'co_return', # (C++20)
'co_yield', # (C++20)
'decltype', # (C++11) (2)
'default',
'delete',
'do',
'double',
'dynamic_cast',
'else',
'enum',
'explicit',
'export',
'extern',
'false',
'float',
'for',
'friend',
'goto',
'if',
'inline',
'int',
'long',
'mutable',
'namespace',
'new',
'noexcept', # (C++11)
'not',
'not_eq',
'nullptr', # (C++11)
'operator',
'or',
'or_eq',
'private',
'protected',
'public',
'reflexpr', # (reflection TS)
'register',
'reinterpret_cast',
'requires', # (C++20)
'return',
'short',
'signed',
'sizeof',
'static',
'static_assert', # (C++11)
'static_cast',
'struct',
'switch',
'synchronized', # (TM TS)
'template',
'this',
'thread_local', # (C++11)
'throw',
'true',
'try',
'typedef',
'typeid',
'typename',
'union',
'unsigned',
'using',
'virtual',
'void',
'volatile',
'wchar_t',
'while',
'xor',
'xor_eq',
}

reserved_cnames = iso_c23_keywords | iso_cpp23_keywords
12 changes: 2 additions & 10 deletions Cython/Compiler/Symtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,16 @@

from . import Code

iso_c99_keywords = {
'auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do',
'double', 'else', 'enum', 'extern', 'float', 'for', 'goto', 'if',
'int', 'long', 'register', 'return', 'short', 'signed', 'sizeof',
'static', 'struct', 'switch', 'typedef', 'union', 'unsigned', 'void',
'volatile', 'while',
'_Bool', '_Complex'', _Imaginary', 'inline', 'restrict',
}


def c_safe_identifier(cname):
# There are some C limitations on struct entry names.
if ((cname[:2] == '__' and not (cname.startswith(Naming.pyrex_prefix)
or cname in ('__weakref__', '__dict__')))
or cname in iso_c99_keywords):
or cname in Naming.reserved_cnames):
cname = Naming.pyrex_prefix + cname
return cname


def punycodify_name(cname, mangle_with=None):
# if passed the mangle_with should be a byte string
# modified from PEP489
Expand Down

0 comments on commit b8b5a24

Please sign in to comment.