From 0698c3cb9365fdf8bf658daf1a7379e014c38d9d Mon Sep 17 00:00:00 2001 From: Michka Popoff Date: Thu, 12 Dec 2024 20:30:20 +0100 Subject: [PATCH] type-traits: fix string equivalences Add missing spaces, which should fix some equivalence detection in the defaults_eraser function --- pyproject.toml | 3 + src/pygccxml/declarations/container_traits.py | 21 +++---- src/pygccxml/declarations/type_traits.py | 60 ++++++++++++++----- 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3dbe90f2..d966316d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,3 +64,6 @@ docs = [ examples = [ "notebook", ] +[tool.pytest.ini_options] + pythonpath = [ + "src"] \ No newline at end of file diff --git a/src/pygccxml/declarations/container_traits.py b/src/pygccxml/declarations/container_traits.py index e5035782..b2c4c104 100644 --- a/src/pygccxml/declarations/container_traits.py +++ b/src/pygccxml/declarations/container_traits.py @@ -29,20 +29,17 @@ def normalize(self, type_str): return type_str.replace(' ', '') def replace_basic_string(self, cls_name): - - # Take the lists of all possible string variations - # and clean them up by replacing ::std by std. - str_eq = [ - v.replace("::std", "std") for v in - type_traits.string_equivalences] - wstr_eq = [ - v.replace("::std", "std") for v in - type_traits.wstring_equivalences] - # Replace all the variations of strings by the smallest one. strings = { - "std::string": [v for v in str_eq if not v == "std::string"], - "std::wstring": [v for v in wstr_eq if not v == "std::wstring"]} + "std::string": + [v for v in + type_traits.normalized_string_equivalences + if not v == "std::string"], + "std::wstring": + [v for v in + type_traits.normalized_wstring_equivalences + if not v == "std::wstring"] + } new_name = cls_name for short_name, long_names in strings.items(): diff --git a/src/pygccxml/declarations/type_traits.py b/src/pygccxml/declarations/type_traits.py index bc9be8de..49aa6950 100644 --- a/src/pygccxml/declarations/type_traits.py +++ b/src/pygccxml/declarations/type_traits.py @@ -480,27 +480,55 @@ def is_fundamental(type_): (cpptypes.volatile_t, cpptypes.const_t)) +def _normalize(string): + return string.replace(' ', '').replace("::std", "std") + + +def _normalize_equivalences(equivalences): + return [_normalize(eq) for eq in equivalences] + + string_equivalences = [ ( - '::std::basic_string,' - 'std::allocator>'), - '::std::basic_string', '::std::string'] + '::std::basic_string, ' + 'std::allocator>' + ), + '::std::basic_string', + '::std::string' + ] wstring_equivalences = [ ( - '::std::basic_string,' + - 'std::allocator>'), - '::std::basic_string', '::std::wstring'] + '::std::basic_string, ' + 'std::allocator>' + ), + '::std::basic_string', + '::std::wstring' + ] ostream_equivalences = [ - '::std::basic_ostream>', + '::std::basic_ostream>', '::std::basic_ostream', '::std::ostream'] wostream_equivalences = [ - '::std::basic_ostream>', + '::std::basic_ostream>', '::std::basic_ostream', '::std::wostream'] +normalized_string_equivalences = _normalize_equivalences( + string_equivalences + ) +normalized_wstring_equivalences = _normalize_equivalences( + wstring_equivalences + ) +normalized_ostream_equivalences = _normalize_equivalences( + ostream_equivalences + ) +normalized_wostream_equivalences = _normalize_equivalences( + wostream_equivalences + ) + + def is_std_string(type_): """ Returns True, if type represents C++ `std::string`, False otherwise. @@ -508,12 +536,12 @@ def is_std_string(type_): """ if isinstance(type_, str): - return type_ in string_equivalences + return _normalize(type_) in normalized_string_equivalences type_ = remove_alias(type_) type_ = remove_reference(type_) type_ = remove_cv(type_) - return type_.decl_string.replace(' ', '') in string_equivalences + return _normalize(type_.decl_string) in normalized_string_equivalences def is_std_wstring(type_): @@ -523,12 +551,12 @@ def is_std_wstring(type_): """ if isinstance(type_, str): - return type_ in wstring_equivalences + return _normalize(type_) in normalized_wstring_equivalences type_ = remove_alias(type_) type_ = remove_reference(type_) type_ = remove_cv(type_) - return type_.decl_string.replace(' ', '') in wstring_equivalences + return _normalize(type_.decl_string) in normalized_wstring_equivalences def is_std_ostream(type_): @@ -538,12 +566,12 @@ def is_std_ostream(type_): """ if isinstance(type_, str): - return type_ in ostream_equivalences + return _normalize(type_) in normalized_ostream_equivalences type_ = remove_alias(type_) type_ = remove_reference(type_) type_ = remove_cv(type_) - return type_.decl_string.replace(' ', '') in ostream_equivalences + return _normalize(type_.decl_string) in normalized_ostream_equivalences def is_std_wostream(type_): @@ -553,9 +581,9 @@ def is_std_wostream(type_): """ if isinstance(type_, str): - return type_ in wostream_equivalences + return _normalize(type_) in normalized_wostream_equivalences type_ = remove_alias(type_) type_ = remove_reference(type_) type_ = remove_cv(type_) - return type_.decl_string.replace(' ', '') in wostream_equivalences + return _normalize(type_.decl_string) in normalized_wostream_equivalences