Skip to content

Commit df4a4cc

Browse files
committed
Expand comment reading
Expand the comment attribute reading to Namespace and Enumeration objects. Add tests for each of those objects.
1 parent b7c9e1f commit df4a4cc

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

pygccxml/parser/scanner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,19 @@ def __read_namespace(self, attrs):
416416
# that is almost true: gcc mangale name using top file name.
417417
# almost all files has '.' in name
418418
ns_name = ''
419-
return self.__decl_factory.create_namespace(name=ns_name)
419+
decl = self.__decl_factory.create_namespace(name=ns_name)
420+
if attrs.get(XML_AN_COMMENT):
421+
decl.comment = attrs.get(XML_AN_COMMENT)
422+
return decl
420423

421424
def __read_enumeration(self, attrs):
422425
enum_name = attrs.get(XML_AN_NAME, '')
423426
if '$_' in enum_name or '._' in enum_name:
424427
# it means that this is unnamed enum. in c++ enum{ x };
425428
enum_name = ''
426429
decl = self.__decl_factory.create_enumeration(name=enum_name)
430+
if attrs.get(XML_AN_COMMENT):
431+
decl.comment = attrs.get(XML_AN_COMMENT)
427432
self.__read_byte_size(decl, attrs)
428433
self.__read_byte_align(decl, attrs)
429434
self.__enums.append(decl)

unittests/data/test_comments.hpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,38 @@
33
// Distributed under the Boost Software License, Version 1.0.
44
// See http://www.boost.org/LICENSE_1_0.txt
55

6-
/** class comment */
7-
class test
8-
{
9-
public:
10-
// Non-doc comment before.
11-
/** doc comment */
12-
// Non-doc comment after.
13-
test();
6+
//! Namespace Comment
7+
//! Across multiple lines
8+
namespace comment {
149

15-
/// cxx comment
16-
/// with multiple lines
17-
int hello();
10+
/** class comment */
11+
class test
12+
{
13+
public:
14+
// Non-doc comment before.
15+
/** doc comment */
16+
// Non-doc comment after.
17+
test();
1818

19-
//! mutable field comment
20-
int val1 = 0;
21-
/// bit field comment
22-
double val2=2;
23-
};
19+
/// cxx comment
20+
/// with multiple lines
21+
int hello();
22+
23+
//! mutable field comment
24+
int val1 = 0;
25+
/// bit field comment
26+
double val2=2;
27+
28+
/// inside class enum comment
29+
enum test_enum {
30+
One = 1,
31+
Two = 2
32+
};
33+
};
34+
35+
/// Outside Class enum comment
36+
enum com_enum {
37+
One = 1,
38+
Two = 2
39+
};
40+
}

unittests/test_comments.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,27 @@ def test(self):
3737
if self.config.castxml_epic_version != 1:
3838
# Run this test only with castxml epic version == 1
3939
return
40-
tclass = self.global_ns.class_("test")
40+
41+
tnamespace = self.global_ns.namespace("comment")
42+
43+
self.assertIn("comment", dir(tnamespace))
44+
self.assertEqual(["//! Namespace Comment", "//! Across multiple lines"],
45+
tnamespace.comment.text)
46+
47+
tenumeration = tnamespace.enumeration("com_enum")
48+
self.assertIn("comment", dir(tenumeration))
49+
self.assertEqual(['/// Outside Class enum comment'],
50+
tenumeration.comment.text)
51+
52+
tclass = tnamespace.class_("test")
4153
self.assertIn("comment", dir(tclass))
4254
self.assertEqual(["/** class comment */"], tclass.comment.text)
4355

56+
tcls_enumeration = tclass.enumeration("test_enum")
57+
self.assertIn("comment", dir(tcls_enumeration))
58+
self.assertEqual(['/// inside class enum comment'],
59+
tcls_enumeration.comment.text)
60+
4461
tmethod = tclass.member_functions()[0]
4562

4663
self.assertIn("comment", dir(tmethod))

0 commit comments

Comments
 (0)