Skip to content

Commit 490ca87

Browse files
committed
Improve find_noncopyable_vars function (#84)
The find_noncopyable_vars function was wrongly returning variables of pointer type (non const) as noncopyable variables, in the case where the pointer was pointing to a noncopyable class (for example an abstract class, as shown in the test). Commit cherry-picked from the develop branch.
1 parent 9c994a1 commit 490ca87

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

pygccxml/declarations/type_traits_classes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ def find_noncopyable_vars(class_type, already_visited_cls_vars=None):
204204
logger.debug((message + " - array"), var_type.decl_string)
205205
noncopyable_vars.append(mvar)
206206

207+
if type_traits.is_pointer(var_type):
208+
continue
209+
207210
if class_traits.is_my_case(var_type):
208211

209212
cls = class_traits.get_declaration(var_type)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014-2017 Insight Software Consortium.
2+
// Copyright 2004-2009 Roman Yakovenko.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// See http://www.boost.org/LICENSE_1_0.txt
5+
6+
struct abstract{
7+
virtual void do_smth() = 0;
8+
};
9+
10+
struct holder{
11+
abstract* ptr1;
12+
abstract* const ptr2;
13+
};

unittests/test_all.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
from . import test_castxml_wrong_epic
8383
from . import test_elaborated_types
8484
from . import test_order
85+
from . import test_find_noncopyable_vars
8586

8687
testers = [
8788
# , demangled_tester # failing right now
@@ -154,7 +155,8 @@
154155
test_non_copyable_recursive,
155156
test_castxml_wrong_epic,
156157
test_elaborated_types,
157-
test_order
158+
test_order,
159+
test_find_noncopyable_vars
158160
]
159161

160162
if platform.system() != 'Windows':
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2014-2017 Insight Software Consortium.
2+
# Copyright 2004-2009 Roman Yakovenko.
3+
# Distributed under the Boost Software License, Version 1.0.
4+
# See http://www.boost.org/LICENSE_1_0.txt
5+
6+
import unittest
7+
8+
from . import parser_test_case
9+
10+
from pygccxml import parser
11+
from pygccxml import declarations
12+
13+
14+
class Test(parser_test_case.parser_test_case_t):
15+
16+
def __init__(self, *args):
17+
parser_test_case.parser_test_case_t.__init__(self, *args)
18+
self.header = "find_noncopyable_vars.hpp"
19+
decls = parser.parse([self.header], self.config)
20+
self.global_ns = declarations.get_global_namespace(decls)
21+
22+
def test(self):
23+
"""
24+
Test the find_noncopyable_vars function
25+
26+
"""
27+
28+
# The ptr1 variable in the holder struct can be copied,
29+
# but not the ptr2 variable
30+
holder = self.global_ns.class_("holder")
31+
nc_vars = declarations.find_noncopyable_vars(holder)
32+
self.assertEqual(len(nc_vars), 1)
33+
self.assertEqual(nc_vars[0].name, "ptr2")
34+
self.assertTrue(declarations.is_pointer(nc_vars[0].decl_type))
35+
self.assertTrue(declarations.is_const(nc_vars[0].decl_type))
36+
37+
38+
def create_suite():
39+
suite = unittest.TestSuite()
40+
suite.addTest(unittest.makeSuite(Test))
41+
return suite
42+
43+
44+
def run_suite():
45+
unittest.TextTestRunner(verbosity=2).run(create_suite())
46+
47+
48+
if __name__ == "__main__":
49+
run_suite()

0 commit comments

Comments
 (0)