Skip to content

Commit

Permalink
scanner: debug spaces in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
iMichka committed Dec 20, 2024
1 parent ea300f6 commit d0a11a1
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ jobs:
- name: Run tests
run: |
export PATH=~/castxml/bin:$PATH
pytest tests
pytest tests/test_remove_template_defaults.py
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ docs = [
examples = [
"notebook",
]
[tool.pytest.ini_options]
pythonpath = ["src"]
59 changes: 30 additions & 29 deletions src/pygccxml/declarations/container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ def erase_allocator(self, cls_name, default_allocator='std::allocator'):
return
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $allocator<$value_type> >")
"$container<$value_type, $allocator<$value_type>>")
tmpl = tmpl.substitute(
container=c_name,
value_type=value_type,
allocator=default_allocator)
if self.normalize(cls_name) == \
self.normalize(tmpl):
return templates.join(
x = templates.join(
c_name, [self.erase_recursive(value_type)])
return x

def erase_container(self, cls_name, default_container_name='std::deque'):
cls_name = self.replace_basic_string(cls_name)
Expand Down Expand Up @@ -159,8 +160,8 @@ def erase_compare_allocator(
return
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $compare<$value_type>, " +
"$allocator<$value_type> >")
"$container<$value_type, $compare<$value_type>, " +
"$allocator<$value_type>>")
tmpl = tmpl.substitute(
container=c_name,
value_type=value_type,
Expand All @@ -184,14 +185,14 @@ def erase_map_compare_allocator(
mapped_type = c_args[1]
tmpls = [
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< const $key_type, $mapped_type> > >"),
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<const $key_type, $mapped_type>>>"),
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< $key_type const, $mapped_type> > >"),
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair< $key_type const, $mapped_type>>>"),
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< $key_type, $mapped_type> > >")]
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<$key_type, $mapped_type>>>")]
for tmpl in tmpls:
tmpl = tmpl.substitute(
container=c_name,
Expand All @@ -218,13 +219,13 @@ def erase_hash_allocator(self, cls_name):
if len(c_args) == 3:
default_hash = 'hash_compare'
tmpl = (
"$container< $value_type, $hash<$value_type, " +
"$less<$value_type> >, $allocator<$value_type> >")
"$container<$value_type, $hash<$value_type, " +
"$less<$value_type>>, $allocator<$value_type>>")
elif len(c_args) == 4:
default_hash = 'hash'
tmpl = (
"$container< $value_type, $hash<$value_type >, " +
"$equal_to<$value_type >, $allocator<$value_type> >")
"$container<$value_type, $hash<$value_type>, " +
"$equal_to<$value_type>, $allocator<$value_type>>")
else:
return

Expand Down Expand Up @@ -263,14 +264,14 @@ def erase_hashmap_compare_allocator(self, cls_name):
if len(c_args) == 4:
default_hash = 'hash_compare'
tmpl = string.Template(
"$container< $key_type, $mapped_type, " +
"$hash<$key_type, $less<$key_type> >, " +
"$allocator< std::pair< const $key_type, $mapped_type> > >")
"$container<$key_type, $mapped_type, " +
"$hash<$key_type, $less<$key_type>>, " +
"$allocator<std::pair<const $key_type, $mapped_type>>>")
if key_type.startswith('const ') or key_type.endswith(' const'):
tmpl = string.Template(
"$container< $key_type, $mapped_type, $hash<$key_type, " +
"$less<$key_type> >, $allocator< std::pair< $key_type, " +
"$mapped_type> > >")
"$container<$key_type, $mapped_type, $hash<$key_type, " +
"$less<$key_type>>, $allocator<std::pair<$key_type, " +
"$mapped_type>>>")
elif len(c_args) == 5:
default_hash = 'hash'
if self.unordered_maps_and_sets:
Expand All @@ -279,31 +280,31 @@ def erase_hashmap_compare_allocator(self, cls_name):
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator<std::pair<const$key_type, " +
"$mapped_type> > >")
"$mapped_type>>>")
if key_type.startswith('const ') or \
key_type.endswith(' const'):
tmpl = string.Template(
"$container<$key_type, $mapped_type, " +
"$hash<$key_type >, " +
"$equal_to<$key_type >, " +
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator<std::pair<$key_type, " +
"$mapped_type> > >")
"$mapped_type>>>")
else:
tmpl = string.Template(
"$container< $key_type, $mapped_type, "
"$container<$key_type, $mapped_type, "
"$hash<$key_type >, " +
"$equal_to<$key_type>, "
"$allocator< $mapped_type> >")
"$allocator<$mapped_type>>")
if key_type.startswith('const ') or \
key_type.endswith(' const'):
# TODO: this template is the same than above.
# Make sure why this was needed and if this is
# tested. There may be a const missing somewhere.
tmpl = string.Template(
"$container< $key_type, $mapped_type, " +
"$hash<$key_type >, " +
"$container<$key_type, $mapped_type, " +
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator< $mapped_type > >")
"$allocator<$mapped_type>>")
else:
return

Expand Down
6 changes: 3 additions & 3 deletions src/pygccxml/declarations/pattern_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ def join(self, name, args, arg_separator=None):
args = [_f for _f in args if _f]

if not args:
args_str = ' '
args_str = ''
elif len(args) == 1:
args_str = ' ' + args[0] + ' '
args_str = args[0]
else:
args_str = ' ' + arg_separator.join(args) + ' '
args_str = arg_separator.join(args)

return ''.join([name, self.__begin, args_str, self.__end])

Expand Down
2 changes: 2 additions & 0 deletions src/pygccxml/declarations/type_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ 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):
type_ref = type_.decl_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())
Expand Down
3 changes: 3 additions & 0 deletions src/pygccxml/parser/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def members(self):

def startElement(self, name, attrs):

# print(attrs)

try:
if name not in self.__readers:
return
Expand Down Expand Up @@ -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:
Expand Down
75 changes: 39 additions & 36 deletions tests/test_remove_template_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
'remove_template_defaults.hpp'
]

# import logging
# utils.loggers.set_level(logging.DEBUG)


@pytest.fixture
def global_ns():
Expand All @@ -37,149 +40,149 @@ def global_ns():
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)
assert 'vector<int>' == v_traits.remove_defaults(v_int)
v_string = global_ns.typedef('v_string')
assert 'vector< std::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 > >' == \
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)
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)
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 > >' == \
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 > >' == \
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)
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)
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)
assert 'priority_queue<int>' == pq_traits.remove_defaults(pq_int)
pq_string = global_ns.typedef('pq_string')
assert 'priority_queue< std::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 > >' == \
assert 'set<std::vector<int>>' == \
declarations.set_traits.remove_defaults(s_v_int)
s_string = global_ns.typedef('s_string')
assert 'set< std::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 > >' == \
assert 'multiset<std::vector<int>>' == \
ms_v_traits.remove_defaults(ms_v_int)
ms_string = global_ns.typedef('ms_string')
assert 'multiset< std::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 >' == \
assert 'map<int, double>' == \
declarations.map_traits.remove_defaults(m_i2d)
m_wstr2d = global_ns.typedef('m_wstr2d')
assert 'map< std::wstring, double >' == \
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 > >'
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)
assert 'multimap<int, double>' == mm_traits.remove_defaults(mm_i2d)
mm_wstr2d = global_ns.typedef('mm_wstr2d')
assert 'multimap< const std::wstring, double >' == \
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 > >') == \
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 > >') == \
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 >') == \
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 > >') == \
assert (name + '<std::vector<int>>') == \
mhs_traits.remove_defaults(mhs_v_int)
mhs_string = global_ns.typedef('mhs_string')
assert (name + '< std::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 >') == \
assert (name + '<int, double>') == \
hm_traits.remove_defaults(hm_i2d)
hm_wstr2d = global_ns.typedef('hm_wstr2d')
assert (name + '< std::wstring, double >') == \
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 >') == \
assert (name + '<int, double>') == \
hmm_traits.remove_defaults(hmm_i2d)
hmm_wstr2d = global_ns.typedef('hmm_wstr2d')
assert (name + '< const std::wstring, double >') == \
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 >, ' +
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 > >')
'<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

0 comments on commit d0a11a1

Please sign in to comment.