Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templates: fix issues with spaces #221

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 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 @@ -122,11 +122,10 @@ def erase_container(self, cls_name, default_container_name='std::deque'):
return
value_type = c_args[0]
dc_no_defaults = self.erase_recursive(c_args[1])
if self.normalize(dc_no_defaults) != self.normalize(
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 +158,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 +183,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 +217,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 +262,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 +278,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
7 changes: 6 additions & 1 deletion src/pygccxml/parser/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ def members(self):
return self.__members

def startElement(self, name, attrs):

try:
if name not in self.__readers:
return
Expand Down Expand Up @@ -656,6 +655,12 @@ def __read_class_impl(self, class_type, attrs):
name = attrs.get(XML_AN_NAME, '')
if '$' in name or '.' in name:
name = ''
if "<" in name and " >" in name:
# Name with template. In some rare cases there
# is a space before > (and only there), so remove
# it to be consistent with the other names that
# have no space there
name = name.replace(" >", ">")
if XML_AN_INCOMPLETE in attrs:
decl = self.__decl_factory.create_class_declaration(name=name)
else:
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
36 changes: 18 additions & 18 deletions tests/test_find_container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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 +50,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 +154,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
Loading