Skip to content

Commit 82209fc

Browse files
authored
Merge pull request #135 from josephsnyder/add_overrides_decl
Use "overrides" information from CastXML
2 parents 660c8d1 + df33866 commit 82209fc

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

pygccxml/declarations/calldef.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def __init__(
165165
self._calling_convention = None
166166
self._has_inline = None
167167
self._mangled = mangled
168+
self._overrides = None
168169

169170
def _get__cmp__call_items(self):
170171
"""
@@ -242,6 +243,16 @@ def optional_args(self):
242243
value"""
243244
return self.arguments[len(self.required_args):]
244245

246+
@property
247+
def overrides(self):
248+
"""If a function is marked as an overrides, contains
249+
the declaration which this function overrides."""
250+
return self._overrides
251+
252+
@overrides.setter
253+
def overrides(self, overrides):
254+
self._overrides = overrides
255+
245256
@property
246257
def does_throw(self):
247258
"""If False, than function does not throw any exception.

pygccxml/parser/scanner.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
XML_AN_MUTABLE = "mutable"
4848
XML_AN_NAME = "name"
4949
XML_AN_OFFSET = "offset"
50+
XML_AN_OVERRIDES = "overrides"
5051
XML_AN_PURE_VIRTUAL = "pure_virtual"
5152
XML_AN_RESTRICT = "restrict"
5253
XML_AN_RETURNS = "returns"
@@ -247,6 +248,9 @@ def endDocument(self):
247248
for gccxml_id, decl in self.__declarations.items():
248249
if not isinstance(decl.comment, declarations.comment.comment_t):
249250
decl.comment = self._handle_comment(decl)
251+
if isinstance(decl, declarations.calldef_t) and decl.overrides:
252+
overrides_decl = self.__declarations.get(decl.overrides)
253+
decl.overrides = overrides_decl
250254

251255
def declarations(self):
252256
return self.__declarations
@@ -581,6 +585,8 @@ def __read_member_function(self, calldef, attrs, is_declaration):
581585
calldef.virtuality = declarations.VIRTUALITY_TYPES.VIRTUAL
582586
else:
583587
calldef.virtuality = declarations.VIRTUALITY_TYPES.NOT_VIRTUAL
588+
if XML_AN_OVERRIDES in attrs:
589+
calldef.overrides = attrs.get(XML_AN_OVERRIDES)
584590
else:
585591
calldef.class_inst = attrs[XML_AN_BASE_TYPE]
586592

unittests/data/test_overrides.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2020 Insight Software Consortium.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// See http://www.boost.org/LICENSE_1_0.txt
4+
#ifndef simple_h
5+
#define simple_h
6+
7+
#include <iostream>
8+
class base
9+
{
10+
public:
11+
base() {};
12+
~base() {};
13+
virtual int goodbye(int num_ref, int num_times) = 0;
14+
};
15+
16+
class simple : public base
17+
{
18+
public:
19+
simple() {};
20+
~simple() {};
21+
int hello() { return 0; };
22+
int goodbye(int , int) override
23+
{
24+
std::cout << "goodbye\n";
25+
return 10;
26+
} ;
27+
};
28+
29+
#endif

unittests/test_all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
from . import test_hash
8585
from . import test_null_comparison
8686
from . import test_comments
87+
from . import test_overrides
8788

8889
testers = [
8990
pep8_tester,
@@ -159,6 +160,7 @@
159160
test_hash,
160161
test_null_comparison,
161162
test_comments,
163+
test_overrides,
162164
]
163165

164166
if platform.system() != 'Windows':

unittests/test_overrides.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2014-2020 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+
global_ns = None
16+
17+
def __init__(self, *args):
18+
parser_test_case.parser_test_case_t.__init__(self, *args)
19+
self.header = "test_overrides.hpp"
20+
self.global_ns = None
21+
self.config.castxml_epic_version = 1
22+
23+
def setUp(self):
24+
25+
if not self.global_ns:
26+
decls = parser.parse([self.header], self.config)
27+
Test.global_ns = declarations.get_global_namespace(decls)
28+
Test.xml_generator_from_xml_file = \
29+
self.config.xml_generator_from_xml_file
30+
self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file
31+
32+
self.global_ns = Test.global_ns
33+
34+
def test(self):
35+
"""
36+
Check that the override information is populated for the
37+
simple::goodbye function. It should contain the decl for the
38+
base::goodbye function. Base::goodbye has no override so it
39+
will be none
40+
"""
41+
base = self.global_ns.class_("base").member_function("goodbye")
42+
override_decl = self.global_ns.class_("simple")\
43+
.member_function("goodbye")
44+
45+
self.assertTrue(base.overrides is None)
46+
self.assertFalse(override_decl.overrides is None)
47+
self.assertEqual(override_decl.overrides, base)
48+
49+
50+
def create_suite():
51+
suite = unittest.TestSuite()
52+
suite.addTest(unittest.makeSuite(Test))
53+
return suite
54+
55+
56+
def run_suite():
57+
unittest.TextTestRunner(verbosity=2).run(create_suite())
58+
59+
60+
if __name__ == "__main__":
61+
run_suite()

0 commit comments

Comments
 (0)