diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a3b5f842..731ccef5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -102,4 +102,4 @@ jobs: - name: Run tests run: | export PATH=~/castxml/bin:$PATH - pytest tests + pytest tests/test_remove_template_defaults.py diff --git a/pyproject.toml b/pyproject.toml index 8abdfac0..d0200c70 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,3 +64,5 @@ 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 0f8d1d61..24f69239 100644 --- a/src/pygccxml/declarations/container_traits.py +++ b/src/pygccxml/declarations/container_traits.py @@ -101,6 +101,7 @@ def erase_recursive(self, cls_name): def erase_allocator(self, cls_name, default_allocator='std::allocator'): cls_name = self.replace_basic_string(cls_name) c_name, c_args = templates.split(cls_name) + print("erase_allocator c_name, c_args", c_name, c_args) if len(c_args) != 2: return value_type = c_args[0] @@ -112,8 +113,10 @@ def erase_allocator(self, cls_name, default_allocator='std::allocator'): allocator=default_allocator) if self.normalize(cls_name) == \ self.normalize(tmpl): - return templates.join( + x = templates.join( c_name, [self.erase_recursive(value_type)]) + print("erase_allocator x", x) + return x def erase_container(self, cls_name, default_container_name='std::deque'): cls_name = self.replace_basic_string(cls_name) @@ -378,7 +381,9 @@ def get_container_or_none(self, type_): """ + print("get_container_or_none", type_) type_ = type_traits.remove_alias(type_) + print("get_container_or_none", type_) type_ = type_traits.remove_cv(type_) utils.loggers.queries_engine.debug( @@ -520,13 +525,14 @@ def remove_defaults(self, type_or_string): std::vector< int > """ - + print("xxxx", type_or_string) name = type_or_string if not isinstance(type_or_string, str): name = self.class_declaration(type_or_string).name if not self.remove_defaults_impl: return name no_defaults = self.remove_defaults_impl(name) + print("xxxx name, no_defaults", name, no_defaults) if not no_defaults: return name return no_defaults diff --git a/src/pygccxml/declarations/type_traits.py b/src/pygccxml/declarations/type_traits.py index ba129259..d1eff970 100644 --- a/src/pygccxml/declarations/type_traits.py +++ b/src/pygccxml/declarations/type_traits.py @@ -52,6 +52,7 @@ def remove_alias(type_): Returns: type_t: the type associated to the inputted declaration """ + print("remove_alias", type_) if isinstance(type_, cpptypes.type_t): type_ref = type_ elif isinstance(type_, typedef.typedef_t): @@ -59,6 +60,7 @@ def remove_alias(type_): else: # Not a valid input, just return it return type_ + print("remove_alias type_ref", type_ref) if type_ref.cache.remove_alias: return type_ref.cache.remove_alias no_alias = __remove_alias(type_ref.clone()) diff --git a/src/pygccxml/parser/scanner.py b/src/pygccxml/parser/scanner.py index f39dd05d..fb44e8f6 100644 --- a/src/pygccxml/parser/scanner.py +++ b/src/pygccxml/parser/scanner.py @@ -286,6 +286,8 @@ def members(self): def startElement(self, name, attrs): + # print(attrs) + try: if name not in self.__readers: return @@ -654,6 +656,7 @@ def __read_variable(self, attrs): def __read_class_impl(self, class_type, attrs): name = attrs.get(XML_AN_NAME, '') + # name = name.replace(">", " >").replace("<", "< ") if '$' in name or '.' in name: name = '' if XML_AN_INCOMPLETE in attrs: diff --git a/tests/test_remove_template_defaults.py b/tests/test_remove_template_defaults.py index 5f5024a4..8013531b 100644 --- a/tests/test_remove_template_defaults.py +++ b/tests/test_remove_template_defaults.py @@ -17,6 +17,9 @@ 'remove_template_defaults.hpp' ] +# import logging +# utils.loggers.set_level(logging.DEBUG) + @pytest.fixture def global_ns(): @@ -28,6 +31,7 @@ def global_ns(): # https://github.com/jetbrains/kotlin/commit/d50f585911dedec5723213da8835707ac95e1c01 else: config.cflags = "-std=c++11" + # config.keep_xml = True decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) global_ns = declarations.get_global_namespace(decls) global_ns.init_optimizer() @@ -38,148 +42,149 @@ def test_vector(global_ns): v_int = global_ns.typedef('v_int') v_traits = declarations.vector_traits assert 'vector< int >' == v_traits.remove_defaults(v_int) - v_string = global_ns.typedef('v_string') - assert 'vector< std::string >' == \ - v_traits.remove_defaults(v_string) - v_v_int = global_ns.typedef('v_v_int') - assert 'vector< std::vector< int > >' == \ - v_traits.remove_defaults(v_v_int) - - -def test_list(global_ns): - l_int = global_ns.typedef('l_int') - l_traits = declarations.list_traits - assert 'list< int >' == l_traits.remove_defaults(l_int) - l_wstring = global_ns.typedef('l_wstring') - assert 'list< std::wstring >' == l_traits.remove_defaults(l_wstring) - - -def test_deque(global_ns): - d_v_int = global_ns.typedef('d_v_int') - d_v_traits = declarations.deque_traits - assert 'deque< std::vector< int > >' == \ - d_v_traits.remove_defaults(d_v_int) - d_l_string = global_ns.typedef('d_l_string') - assert 'deque< std::list< std::string > >' == \ - d_v_traits.remove_defaults(d_l_string) - - -def test_queue(global_ns): - q_int = global_ns.typedef('q_int') - q_traits = declarations.queue_traits - assert 'queue< int >' == q_traits.remove_defaults(q_int) - q_string = global_ns.typedef('q_string') - assert 'queue< std::string >' == q_traits.remove_defaults(q_string) - - -def test_priority_queue(global_ns): - pq_int = global_ns.typedef('pq_int') - pq_traits = declarations.priority_queue_traits - assert 'priority_queue< int >' == pq_traits.remove_defaults(pq_int) - pq_string = global_ns.typedef('pq_string') - assert 'priority_queue< std::string >' == \ - pq_traits.remove_defaults(pq_string) - - -def test_set(global_ns): - s_v_int = global_ns.typedef('s_v_int') - assert 'set< std::vector< int > >' == \ - declarations.set_traits.remove_defaults(s_v_int) - s_string = global_ns.typedef('s_string') - assert 'set< std::string >' == \ - declarations.set_traits.remove_defaults(s_string) - - -def test_multiset(global_ns): - ms_v_int = global_ns.typedef('ms_v_int') - ms_v_traits = declarations.multiset_traits - assert 'multiset< std::vector< int > >' == \ - ms_v_traits.remove_defaults(ms_v_int) - ms_string = global_ns.typedef('ms_string') - assert 'multiset< std::string >' == \ - ms_v_traits.remove_defaults(ms_string) - - -def test_map(global_ns): - m_i2d = global_ns.typedef('m_i2d') - assert 'map< int, double >' == \ - declarations.map_traits.remove_defaults(m_i2d) - m_wstr2d = global_ns.typedef('m_wstr2d') - assert 'map< std::wstring, double >' == \ - declarations.map_traits.remove_defaults(m_wstr2d) - m_v_i2m_wstr2d = global_ns.typedef('m_v_i2m_wstr2d') - m = 'map< const std::vector< int >, std::map< std::wstring, double > >' - assert m == declarations.map_traits.remove_defaults(m_v_i2m_wstr2d) - - -def test_multimap(global_ns): - mm_i2d = global_ns.typedef('mm_i2d') - mm_traits = declarations.multimap_traits - assert 'multimap< int, double >' == mm_traits.remove_defaults(mm_i2d) - mm_wstr2d = global_ns.typedef('mm_wstr2d') - assert 'multimap< const std::wstring, double >' == \ - mm_traits.remove_defaults(mm_wstr2d) - mm_v_i2mm_wstr2d = global_ns.typedef('mm_v_i2mm_wstr2d') - assert ('multimap< const std::vector< int >, ' + - 'const std::multimap< const std::wstring, double > >') == \ - mm_traits.remove_defaults(mm_v_i2mm_wstr2d) - - -def test_hash_set(global_ns): - hs_v_int = global_ns.typedef('hs_v_int') - hs_traits = declarations.unordered_set_traits - name = 'unordered_set' - assert (name + '< std::vector< int > >') == \ - hs_traits.remove_defaults(hs_v_int), \ - hs_traits.remove_defaults(hs_v_int) - hs_string = global_ns.typedef('hs_string') - assert (name + '< std::string >') == \ - hs_traits.remove_defaults(hs_string) - - -def test_hash_multiset(global_ns): - mhs_v_int = global_ns.typedef('mhs_v_int') - mhs_traits = declarations.unordered_multiset_traits - name = 'unordered_multiset' - assert (name + '< std::vector< int > >') == \ - mhs_traits.remove_defaults(mhs_v_int) - mhs_string = global_ns.typedef('mhs_string') - assert (name + '< std::string >') == \ - mhs_traits.remove_defaults(mhs_string) - - -def test_hash_map(global_ns): - hm_i2d = global_ns.typedef('hm_i2d') - hm_traits = declarations.unordered_map_traits - name = 'unordered_map' - assert (name + '< int, double >') == \ - hm_traits.remove_defaults(hm_i2d) - hm_wstr2d = global_ns.typedef('hm_wstr2d') - assert (name + '< std::wstring, double >') == \ - hm_traits.remove_defaults(hm_wstr2d) - - -def test_hash_multimap(global_ns): - hmm_i2d = global_ns.typedef('hmm_i2d') - hmm_traits = declarations.unordered_multimap_traits - name = 'unordered_multimap' - assert (name + '< int, double >') == \ - hmm_traits.remove_defaults(hmm_i2d) - hmm_wstr2d = global_ns.typedef('hmm_wstr2d') - assert (name + '< const std::wstring, double >') == \ - hmm_traits.remove_defaults(hmm_wstr2d) - - hmm_v_i2mm_wstr2d = global_ns.typedef('hmm_v_i2mm_wstr2d') - - hmm_traits_value = hmm_traits.remove_defaults(hmm_v_i2mm_wstr2d) - - possible_values = ( - name + '< const std::vector< int >, ' + - 'const __gnu_cxx::' + name + '< const std::wstring, double > >', - name + '< const std::vector< int >, ' + - 'const std::' + utils.get_tr1(hmm_traits_value) + name + - '< const std::wstring, double > >', - name + '< const std::vector< int >, ' + - 'const stdext::' + name + '< const std::wstring, double > >') - - assert hmm_traits_value in possible_values, hmm_traits_value + # v_string = global_ns.typedef('v_string') + # assert 'vector< std::string >' == \ + # v_traits.remove_defaults(v_string) + # v_v_int = global_ns.typedef('v_v_int') + # assert 'vector< std::vector< int > >' == \ + # v_traits.remove_defaults(v_v_int) + raise + + +# def test_list(global_ns): +# l_int = global_ns.typedef('l_int') +# l_traits = declarations.list_traits +# assert 'list< int >' == l_traits.remove_defaults(l_int) +# l_wstring = global_ns.typedef('l_wstring') +# assert 'list< std::wstring >' == l_traits.remove_defaults(l_wstring) + + +# def test_deque(global_ns): +# d_v_int = global_ns.typedef('d_v_int') +# d_v_traits = declarations.deque_traits +# assert 'deque< std::vector< int > >' == \ +# d_v_traits.remove_defaults(d_v_int) +# d_l_string = global_ns.typedef('d_l_string') +# assert 'deque< std::list< std::string > >' == \ +# d_v_traits.remove_defaults(d_l_string) + + +# def test_queue(global_ns): +# q_int = global_ns.typedef('q_int') +# q_traits = declarations.queue_traits +# assert 'queue< int >' == q_traits.remove_defaults(q_int) +# q_string = global_ns.typedef('q_string') +# assert 'queue< std::string >' == q_traits.remove_defaults(q_string) + + +# def test_priority_queue(global_ns): +# pq_int = global_ns.typedef('pq_int') +# pq_traits = declarations.priority_queue_traits +# assert 'priority_queue< int >' == pq_traits.remove_defaults(pq_int) +# pq_string = global_ns.typedef('pq_string') +# assert 'priority_queue< std::string >' == \ +# pq_traits.remove_defaults(pq_string) + + +# def test_set(global_ns): +# s_v_int = global_ns.typedef('s_v_int') +# assert 'set< std::vector< int > >' == \ +# declarations.set_traits.remove_defaults(s_v_int) +# s_string = global_ns.typedef('s_string') +# assert 'set< std::string >' == \ +# declarations.set_traits.remove_defaults(s_string) + + +# def test_multiset(global_ns): +# ms_v_int = global_ns.typedef('ms_v_int') +# ms_v_traits = declarations.multiset_traits +# assert 'multiset< std::vector< int > >' == \ +# ms_v_traits.remove_defaults(ms_v_int) +# ms_string = global_ns.typedef('ms_string') +# assert 'multiset< std::string >' == \ +# ms_v_traits.remove_defaults(ms_string) + + +# def test_map(global_ns): +# m_i2d = global_ns.typedef('m_i2d') +# assert 'map< int, double >' == \ +# declarations.map_traits.remove_defaults(m_i2d) +# m_wstr2d = global_ns.typedef('m_wstr2d') +# assert 'map< std::wstring, double >' == \ +# declarations.map_traits.remove_defaults(m_wstr2d) +# m_v_i2m_wstr2d = global_ns.typedef('m_v_i2m_wstr2d') +# m = 'map< const std::vector< int >, std::map< std::wstring, double > >' +# assert m == declarations.map_traits.remove_defaults(m_v_i2m_wstr2d) + + +# def test_multimap(global_ns): +# mm_i2d = global_ns.typedef('mm_i2d') +# mm_traits = declarations.multimap_traits +# assert 'multimap< int, double >' == mm_traits.remove_defaults(mm_i2d) +# mm_wstr2d = global_ns.typedef('mm_wstr2d') +# assert 'multimap< const std::wstring, double >' == \ +# mm_traits.remove_defaults(mm_wstr2d) +# mm_v_i2mm_wstr2d = global_ns.typedef('mm_v_i2mm_wstr2d') +# assert ('multimap< const std::vector< int >, ' + +# 'const std::multimap< const std::wstring, double > >') == \ +# mm_traits.remove_defaults(mm_v_i2mm_wstr2d) + + +# def test_hash_set(global_ns): +# hs_v_int = global_ns.typedef('hs_v_int') +# hs_traits = declarations.unordered_set_traits +# name = 'unordered_set' +# assert (name + '< std::vector< int > >') == \ +# hs_traits.remove_defaults(hs_v_int), \ +# hs_traits.remove_defaults(hs_v_int) +# hs_string = global_ns.typedef('hs_string') +# assert (name + '< std::string >') == \ +# hs_traits.remove_defaults(hs_string) + + +# def test_hash_multiset(global_ns): +# mhs_v_int = global_ns.typedef('mhs_v_int') +# mhs_traits = declarations.unordered_multiset_traits +# name = 'unordered_multiset' +# assert (name + '< std::vector< int > >') == \ +# mhs_traits.remove_defaults(mhs_v_int) +# mhs_string = global_ns.typedef('mhs_string') +# assert (name + '< std::string >') == \ +# mhs_traits.remove_defaults(mhs_string) + + +# def test_hash_map(global_ns): +# hm_i2d = global_ns.typedef('hm_i2d') +# hm_traits = declarations.unordered_map_traits +# name = 'unordered_map' +# assert (name + '< int, double >') == \ +# hm_traits.remove_defaults(hm_i2d) +# hm_wstr2d = global_ns.typedef('hm_wstr2d') +# assert (name + '< std::wstring, double >') == \ +# hm_traits.remove_defaults(hm_wstr2d) + + +# def test_hash_multimap(global_ns): +# hmm_i2d = global_ns.typedef('hmm_i2d') +# hmm_traits = declarations.unordered_multimap_traits +# name = 'unordered_multimap' +# assert (name + '< int, double >') == \ +# hmm_traits.remove_defaults(hmm_i2d) +# hmm_wstr2d = global_ns.typedef('hmm_wstr2d') +# assert (name + '< const std::wstring, double >') == \ +# hmm_traits.remove_defaults(hmm_wstr2d) + +# hmm_v_i2mm_wstr2d = global_ns.typedef('hmm_v_i2mm_wstr2d') + +# hmm_traits_value = hmm_traits.remove_defaults(hmm_v_i2mm_wstr2d) + +# possible_values = ( +# name + '< const std::vector< int >, ' + +# 'const __gnu_cxx::' + name + '< const std::wstring, double > >', +# name + '< const std::vector< int >, ' + +# 'const std::' + utils.get_tr1(hmm_traits_value) + name + +# '< const std::wstring, double > >', +# name + '< const std::vector< int >, ' + +# 'const stdext::' + name + '< const std::wstring, double > >') + +# assert hmm_traits_value in possible_values, hmm_traits_value