Skip to content

Commit e697d33

Browse files
committed
Cleanup static - extern type_qualifiers logic for CastXML
CastXML is more robust than GCC-XML at defining static and extern qualifiers. Static and extern are now no more treated as equivalents in the type_qualifiers class. A bunch of tests have been added to test the new behaviour. The old behaviour is kept for GCC-XML (static == extern).
1 parent 91456e4 commit e697d33

File tree

4 files changed

+87
-18
lines changed

4 files changed

+87
-18
lines changed

pygccxml/declarations/cpptypes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,14 +924,16 @@ class type_qualifiers_t(object):
924924

925925
"""contains additional information about type: mutable, static, extern"""
926926

927-
def __init__(self, has_static=False, has_mutable=False):
927+
def __init__(self, has_static=False, has_mutable=False, has_extern=False):
928928
self._has_static = has_static
929+
self._has_extern = has_extern
929930
self._has_mutable = has_mutable
930931

931932
def __eq__(self, other):
932933
if not isinstance(other, type_qualifiers_t):
933934
return False
934935
return self.has_static == other.has_static \
936+
and self.has_extern == other.has_extern \
935937
and self.has_mutable == other.has_mutable
936938

937939
def __hash__(self):
@@ -944,6 +946,7 @@ def __lt__(self, other):
944946
if not isinstance(other, type_qualifiers_t):
945947
return object.__lt__(self, other)
946948
return self.has_static < other.has_static \
949+
and self.has_extern < other.has_extern \
947950
and self.has_mutable < other.has_mutable
948951

949952
@property
@@ -956,12 +959,11 @@ def has_static(self, has_static):
956959

957960
@property
958961
def has_extern(self):
959-
"""synonym to static"""
960-
return self.has_static
962+
return self._has_extern
961963

962964
@has_extern.setter
963965
def has_extern(self, has_extern):
964-
self.has_static = has_extern
966+
self._has_extern = has_extern
965967

966968
@property
967969
def has_mutable(self):

pygccxml/parser/scanner.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,16 @@ def __read_typedef(self, attrs):
556556
def __read_variable(self, attrs):
557557
type_qualifiers = declarations.type_qualifiers_t()
558558
type_qualifiers.has_mutable = attrs.get(XML_AN_MUTABLE, False)
559-
type_qualifiers.has_static = attrs.get(XML_AN_EXTERN, False)
559+
if "GCC-XML" in utils.xml_generator:
560+
# Old behaviour with gccxml. Will be dropped when gccxml
561+
# is removed.
562+
type_qualifiers.has_static = attrs.get(XML_AN_EXTERN, False)
563+
# With gccxml both were equivalent (at least this was the old
564+
# behaviour in pygccxml)
565+
type_qualifiers.has_extern = type_qualifiers.has_static
566+
else:
567+
type_qualifiers.has_static = attrs.get(XML_AN_STATIC, False)
568+
type_qualifiers.has_extern = attrs.get(XML_AN_EXTERN, False)
560569
bits = attrs.get(XML_AN_BITS)
561570
if bits:
562571
bits = int(bits)

unittests/data/declarations_variables.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace declarations{ namespace variables{
1111
const long unsigned int initialized = 10122004;
1212
int array[255];
1313

14-
//TODO: explain why such variables is not peeked
15-
extern int static_var;
14+
static int static_var;
15+
extern int extern_var;
1616

1717
struct struct_variables_t{
1818
mutable int m_mutable;
@@ -22,6 +22,11 @@ struct struct_variables_holder_t{
2222
struct_variables_t m_struct_variables;
2323
};
2424

25+
struct struct_static_variables_t{
26+
static const int ssv_static_var;
27+
static const int ssv_static_var_value = 1;
28+
};
29+
2530
} }
2631

2732
#endif//__declarations_variables_hpp__

unittests/declarations_tester.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,78 @@ def test_variables(self):
5656
declarations.const_t,
5757
declarations.long_unsigned_int_t)
5858

59-
static_var = initialized = self.global_ns.variable(name='static_var')
59+
m_mutable = self.global_ns.variable(name="m_mutable")
60+
self.assertFalse(
61+
m_mutable.type_qualifiers.has_static,
62+
"m_mutable must not have static type qualifier")
63+
64+
if "GCC-XML" in utils.xml_generator:
65+
# Old GCC-XML behaviour. Can be dropped once GCC-XML is removed.
66+
static_var = self.global_ns.variable(name="extern_var")
67+
self.assertTrue(
68+
static_var.type_qualifiers.has_static,
69+
"static_var must have static type qualifier")
70+
self.assertFalse(
71+
static_var.type_qualifiers.has_mutable,
72+
"static_var must not have mutable type qualifier")
73+
return
74+
75+
# CastXML only tests --------------
76+
77+
self.assertTrue(
78+
m_mutable.type_qualifiers.has_mutable,
79+
"m_mutable must have mutable type qualifier")
80+
81+
# External static variable
82+
extern_var = self.global_ns.variable(name="extern_var")
83+
self.assertTrue(
84+
extern_var.type_qualifiers.has_extern,
85+
"extern_var must have extern type qualifier")
86+
self.assertFalse(
87+
extern_var.type_qualifiers.has_static,
88+
"extern_var must not have a static type qualifier")
89+
self.assertFalse(
90+
extern_var.type_qualifiers.has_mutable,
91+
"static_var must not have mutable type qualifier")
92+
93+
# Static variable
94+
static_var = self.global_ns.variable(name="static_var")
6095
self.assertTrue(
6196
static_var.type_qualifiers.has_static,
6297
"static_var must have static type qualifier")
63-
self.assertTrue(
64-
not static_var.type_qualifiers.has_mutable,
98+
self.assertFalse(
99+
static_var.type_qualifiers.has_extern,
100+
"static_var must not have an extern type qualifier")
101+
self.assertFalse(
102+
static_var.type_qualifiers.has_mutable,
65103
"static_var must not have mutable type qualifier")
66104

105+
ssv_static_var = self.global_ns.variable(name="ssv_static_var")
106+
self.assertTrue(
107+
ssv_static_var.type_qualifiers.has_static,
108+
"ssv_static_var must have static type qualifier")
109+
self.assertFalse(
110+
ssv_static_var.type_qualifiers.has_extern,
111+
"ssv_static_var must not have an extern type qualifier")
112+
self.assertFalse(
113+
ssv_static_var.type_qualifiers.has_mutable,
114+
"ssv_static_var must not have mutable type qualifier")
115+
116+
ssv_static_var_value = self.global_ns.variable(
117+
name="ssv_static_var_value")
118+
self.assertTrue(
119+
ssv_static_var_value.type_qualifiers.has_static,
120+
"ssv_static_var_value must have static type qualifier")
121+
self.assertFalse(
122+
ssv_static_var_value.type_qualifiers.has_extern,
123+
"ssv_static_var_value must not have an extern type qualifier")
124+
self.assertFalse(
125+
ssv_static_var_value.type_qualifiers.has_mutable,
126+
"ssv_static_var_value must not have mutable type qualifier")
127+
67128
if 'PDB' in utils.xml_generator:
68129
return # TODO find out work around
69130

70-
m_mutable = initialized = self.global_ns.variable(name='m_mutable')
71-
self.assertTrue(
72-
not m_mutable.type_qualifiers.has_static,
73-
"m_mutable must not have static type qualifier")
74-
# TODO: "There is bug in GCCXML: doesn't write mutable qualifier."
75-
# self.assertTrue( m_mutable.type_qualifiers.has_mutable
76-
# , "static_var must have mutable type qualifier" )
77-
78131
def test_calldef_free_functions(self):
79132
ns = self.global_ns.namespace('calldef')
80133

0 commit comments

Comments
 (0)