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 21, 2024
1 parent ea300f6 commit 7b51283
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 110 deletions.
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"]
65 changes: 33 additions & 32 deletions src/pygccxml/declarations/container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ 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,
Expand All @@ -118,15 +118,16 @@ def erase_allocator(self, cls_name, default_allocator='std::allocator'):
def erase_container(self, cls_name, default_container_name='std::deque'):
cls_name = self.replace_basic_string(cls_name)
c_name, c_args = templates.split(cls_name)
print("erase_container c_name, c_args", c_name, c_args)
if len(c_args) != 2:
return
value_type = c_args[0]
dc_no_defaults = self.erase_recursive(c_args[1])
if self.normalize(dc_no_defaults) != self.normalize(
print("erase_container dc_no_defaults", dc_no_defaults)
if self.normalize(dc_no_defaults) == self.normalize(
templates.join(default_container_name, [value_type])):
return
return templates.join(
c_name, [self.erase_recursive(value_type)])
return templates.join(
c_name, [self.erase_recursive(value_type)])

def erase_container_compare(
self,
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
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
2 changes: 1 addition & 1 deletion tests/test_call_invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_split_on_map():


def test_join_on_vector():
assert "vector( int, std::allocator(int) )" == \
assert "vector(int, std::allocator(int))" == \
declarations.call_invocation.join(
"vector", ("int", "std::allocator(int)"))

Expand Down
41 changes: 23 additions & 18 deletions tests/test_find_container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

from pygccxml import parser
from pygccxml import declarations
from pygccxml import utils

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

TEST_FILES = ["remove_template_defaults.hpp", "indexing_suites2.hpp"]

Expand All @@ -17,6 +21,7 @@
def global_ns():
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
config = autoconfig.cxx_parsers_cfg.config.clone()
config.keep_xml = True
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
global_ns = declarations.get_global_namespace(decls)
global_ns.init_optimizer()
Expand All @@ -33,7 +38,6 @@ def __cmp_traits(global_ns, typedef, expected, partial_name, key_type=None):
assert declarations.find_container_traits(cls) == expected
assert cls.partial_name == partial_name
cls = traits.class_declaration(cls)
print("xxxx", traits, typedef)
assert traits.element_type(typedef) is not None
assert cls.cache.container_element_type is not None

Expand All @@ -51,91 +55,92 @@ def test_find_traits(global_ns):
global_ns,
"v_int",
declarations.vector_traits,
"vector< int >"
"vector<int>"
)
__cmp_traits(
global_ns,
"l_int",
declarations.list_traits,
"list< int >"
"list<int>"
)
__cmp_traits(
global_ns, "d_v_int",
global_ns,
"d_v_int",
declarations.deque_traits,
"deque< std::vector< int > >"
"deque<std::vector<int> >"
)
__cmp_traits(
global_ns, "q_int",
declarations.queue_traits,
"queue< int >"
"queue<int>"
)
__cmp_traits(
global_ns, "pq_int",
declarations.priority_queue_traits,
"priority_queue< int >"
"priority_queue<int>"
)
__cmp_traits(
global_ns, "s_v_int",
declarations.set_traits,
"set< std::vector< int > >"
"set<std::vector<int> >"
)
__cmp_traits(
global_ns,
"ms_v_int",
declarations.multiset_traits,
"multiset< std::vector< int > >",
"multiset<std::vector<int> >",
)
__cmp_traits(
global_ns, "m_i2d",
declarations.map_traits,
"map< int, double >",
"map<int, double>",
"int"
)
__cmp_traits(
global_ns,
"mm_i2d",
declarations.multimap_traits,
"multimap< int, double >",
"multimap<int, double>",
"int",
)
__cmp_traits(
global_ns,
"hs_v_int",
declarations.unordered_set_traits,
"unordered_set< std::vector< int > >",
"unordered_set<std::vector<int> >",
)
__cmp_traits(
global_ns,
"mhs_v_int",
declarations.unordered_multiset_traits,
"unordered_multiset< std::vector< int > >",
"unordered_multiset<std::vector<int> >",
)
__cmp_traits(
global_ns,
"hm_i2d",
declarations.unordered_map_traits,
"unordered_map< int, double >",
"unordered_map<int, double>",
"int",
)
__cmp_traits(
global_ns,
"hmm_i2d",
declarations.unordered_multimap_traits,
"unordered_multimap< int, double >",
"unordered_multimap<int, double>",
"int",
)


def test_multimap(global_ns):
m = global_ns.class_(lambda decl: decl.name.startswith("multimap"))
declarations.find_container_traits(m)
assert m.partial_name == "multimap< int, int >"
assert m.partial_name == "multimap<int, int>"


def test_recursive_partial_name(global_ns):
f1 = global_ns.free_function("f1")
t1 = declarations.class_traits.get_declaration(f1.arguments[0].decl_type)
assert "type< std::set< std::vector< int > > >" == t1.partial_name
assert "type<std::set<std::vector<int> >>" == t1.partial_name


def test_remove_defaults_partial_name_namespace(global_ns):
Expand All @@ -154,7 +159,7 @@ def test_from_ogre():
"map<std::string, bool (*)(std::string&, "
+ "Ogre::MaterialScriptContext&), std::less<std::string>, "
+ "std::allocator<std::pair<std::string const, bool (*)"
+ "(std::string&, Ogre::MaterialScriptContext&)> > >"
+ "(std::string&, Ogre::MaterialScriptContext&)>>>"
)
ct = declarations.find_container_traits(x)
ct.remove_defaults(x)
Expand Down
Loading

0 comments on commit 7b51283

Please sign in to comment.