Skip to content

Commit 69d0bab

Browse files
authored
Add sanitizing identifier for cpp when cpp keyword is used as the project name. (o3de#17667)
* When cpp keyword is used for the project name an underscore is appended to identifier for cpp. * Add UTs. Signed-off-by: Wiktor Bajor <[email protected]>
1 parent 0692266 commit 69d0bab

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

scripts/o3de/o3de/utils.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,31 @@ def sanitize_identifier_for_cpp(identifier: str) -> str:
287287
:param identifier: the name which needs to be sanitized
288288
:return: str: sanitized identifier
289289
"""
290+
cpp_keywords = [
291+
'alignas', 'constinit', 'false', 'public', 'true', 'alignof', 'const_cast', 'float', 'register',
292+
'try', 'asm', 'continue', 'for', 'reinterpret_cast', 'typedef', 'auto', 'co_await', 'friend',
293+
'requires', 'typeid', 'bool', 'co_return', 'goto', 'return', 'typename', 'break', 'co_yield',
294+
'if', 'short', 'union', 'case', 'decltype', 'inline', 'signed', 'unsigned', 'catch', 'default',
295+
'int', 'sizeof', 'using', 'char', 'delete', 'long', 'static', 'virtual', 'char8_t', 'do', 'mutable',
296+
'static_assert', 'void', 'char16_t', 'double', 'namespace', 'static_cast', 'volatile', 'char32_t',
297+
'dynamic_cast', 'new', 'struct', 'wchar_t', 'class', 'else', 'noexcept', 'switch', 'while', 'concept',
298+
'enum', 'nullptr', 'template', 'const', 'explicit', 'operator', 'this', 'consteval', 'export', 'private',
299+
'thread_local', 'constexpr', 'extern', 'protected', 'throw', 'and', 'and_eq', 'bitand', 'bitor', 'compl',
300+
'not', 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq']
301+
290302
if not identifier:
291303
return ''
292304

293-
sanitized_identifier = list(identifier)
294-
for index, character in enumerate(sanitized_identifier):
305+
identifier_chars = list(identifier)
306+
for index, character in enumerate(identifier_chars):
295307
if not (character.isalnum() or character == '_'):
296-
sanitized_identifier[index] = '_'
308+
identifier_chars[index] = '_'
309+
310+
sanitized_identifier = "".join(identifier_chars)
311+
if sanitized_identifier in cpp_keywords:
312+
sanitized_identifier += '_'
297313

298-
return "".join(sanitized_identifier)
314+
return sanitized_identifier
299315

300316

301317
def validate_uuid4(uuid_string: str) -> bool:

scripts/o3de/tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ def test_validate_identifier(value, expected_result):
2929
result = utils.validate_identifier(value)
3030
assert result == expected_result
3131

32+
@pytest.mark.parametrize(
33+
"value, expected_result", [
34+
pytest.param('new', 'new_'),
35+
pytest.param('default', 'default_'),
36+
pytest.param('Default', 'Default'),
37+
pytest.param('test-', 'test_'),
38+
39+
]
40+
)
41+
def test_sanitize_identifier_for_cpp(value, expected_result):
42+
assert utils.sanitize_identifier_for_cpp(value) == expected_result
3243

3344
@pytest.mark.parametrize(
3445
"value, expected_result", [

0 commit comments

Comments
 (0)