Skip to content

Commit

Permalink
"is_union" type trait and unit tests
Browse files Browse the repository at this point in the history
Change-Id: I9f92881bce2afbe2db7160a3acefb21a0fbdd925

Cherry-picked from the develop branch
  • Loading branch information
praetorian20 authored and iMichka committed Mar 13, 2016
1 parent 8c6a8e0 commit 718dcb2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
9 changes: 8 additions & 1 deletion pygccxml/declarations/type_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,15 @@ def is_fundamental(type):
(cpptypes.volatile_t, cpptypes.const_t))


class declaration_xxx_traits(object):
def is_union(type_):
"""returns True if type represents a C++ union"""
if not is_class(type_):
return False
decl = class_traits.get_declaration(type_)
return decl.class_type == class_declaration.CLASS_TYPES.UNION


class declaration_xxx_traits(object):
"""this class implements the functionality needed for convenient work with
declaration classes
Expand Down
12 changes: 11 additions & 1 deletion unittests/data/unnamed_classes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct S1{
unsigned int raw;
} flags;

union FileAttribs{
union /*FileAttribs*/{
struct{
unsigned int isReadOnly : 1;
unsigned int isHidden : 1;
Expand All @@ -45,6 +45,16 @@ struct S1{
unsigned int raw;
} fileattribs; // in GetFileAttributes() format
} header;

struct S3{
union
{
char anon_mem_c;
int anon_mem_i;
};
long s3_mem;
S2 s2;
};
};

} // namespace
Expand Down
8 changes: 8 additions & 0 deletions unittests/parser_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ def _test_calldef_exceptions(self, calldef, exceptions):
lambda parser, a1, a2, *args: parser.assertTrue(a1 in a2, args)
parser_test_case_t.assertNotIn = \
lambda parser, a1, a2, *args: parser.assertFalse(a1 in a2, args)
parser_test_case_t.assertIs = \
lambda parser, a1, a2, *args: parser.assertTrue(a1 is a2, args)
parser_test_case_t.assertIsNot = \
lambda parser, a1, a2, *args: parser.assertFalse(a1 is a2, args)
parser_test_case_t.assertIsNone = \
lambda parser, a1, *args: parser.assertTrue(a1 is None, args)
parser_test_case_t.assertIsNotNone = \
lambda parser, a1, *args: parser.assertFalse(a1 is None, args)
4 changes: 3 additions & 1 deletion unittests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import test_va_list_tag_removal
import test_copy_constructor
import test_cpp_standards
import unnamed_classes_tester

testers = [
# , demangled_tester # failing right now
Expand Down Expand Up @@ -130,7 +131,8 @@
test_utils,
test_va_list_tag_removal,
test_copy_constructor,
test_cpp_standards
test_cpp_standards,
unnamed_classes_tester
]

if 'posix' in os.name:
Expand Down
63 changes: 59 additions & 4 deletions unittests/unnamed_classes_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pygccxml import parser
from pygccxml import declarations
from pygccxml.declarations import type_traits


class tester_t(parser_test_case.parser_test_case_t):
Expand All @@ -23,10 +24,64 @@ def setUp(self):
self.global_ns = declarations.get_global_namespace(decls)
self.global_ns.init_optimizer()

def test(self):
# bf_x = self.global_ns.variable( 'x' )
# self.assertTrue( bf_x.bits == 1 )
pass
def validate_bitfields(self, parent, bitfields):
for key in bitfields:
var = parent.variable(key)
self.assertEqual(var.bits, bitfields[key])

def do_union_test(self, union_name, bitfields):
s2 = self.global_ns.class_('S2')
self.assertFalse(type_traits.is_union(s2))
self.assertEqual(s2.parent.name, 'S1')
self.assertFalse(type_traits.is_union(s2.parent))

union = s2.variable(union_name)
self.assertTrue(type_traits.is_union(union.type))

union_type = type_traits.remove_declarated(union.type)
self.validate_bitfields(union_type, bitfields)
self.assertIsNotNone(union_type.variable('raw'))

def test_union_Flags(self):
flags_bitfields = {
'hasItemIdList': 1,
'pointsToFileOrDir': 1,
'hasDescription': 1,
'hasRelativePath': 1,
'hasWorkingDir': 1,
'hasCmdLineArgs': 1,
'hasCustomIcon': 1,
'useWorkingDir': 1,
'unused': 24,
}
self.do_union_test('flags', flags_bitfields)

def test_unnamed_unions(self):
fileattribs_bitfields = {
'isReadOnly': 1,
'isHidden': 1,
'isSystem': 1,
'isVolumeLabel': 1,
'isDir': 1,
'isModified': 1,
'isEncrypted': 1,
'isNormal': 1,
'isTemporary': 1,
'isSparse': 1,
'hasReparsePoint': 1,
'isCompressed': 1,
'isOffline': 1,
'unused': 19,
}
self.do_union_test('fileattribs', fileattribs_bitfields)

def test_anonymous_unions(self):
s3 = self.global_ns.class_('S3')
self.assertEqual(s3.parent.name, 'S1')

s3_vars = ['anon_mem_c', 'anon_mem_i', 's3_mem', 's2']
for var in s3_vars:
self.assertFalse(type_traits.is_union(s3.variable(var).type))


def create_suite():
Expand Down

0 comments on commit 718dcb2

Please sign in to comment.