Skip to content

Commit 5abf782

Browse files
committed
Fix for differences in nesting of cv-qual types between GCCXML/CastXML
There is a difference in behavior between GCCXML and CastXML for cv-qual arrays. GCCXML produces the following nesting of types: -> volatile_t(const_t(array_t)) while CastXML produces the following nesting: -> array_t(volatile_t(const_t)) For both cases, we must unwrap the types, remove const_t, and add back the outer layers. Change-Id: I17c1e1b034c38cee213a527e0eaae9c334f20440
1 parent e694d7f commit 5abf782

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

pygccxml/declarations/type_traits.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,26 +293,37 @@ def is_const(type):
293293
return False
294294

295295

296-
def remove_const(type):
296+
def remove_const(type_):
297297
"""removes const from the type definition
298298
299299
If type is not const type, it will be returned as is
300300
"""
301301

302-
nake_type = remove_alias(type)
302+
nake_type = remove_alias(type_)
303303
if not is_const(nake_type):
304-
return type
304+
return type_
305305
else:
306+
# Handling for const and volatile qualified types. There is a
307+
# difference in behavior between GCCXML and CastXML for cv-qual arrays.
308+
# GCCXML produces the following nesting of types:
309+
# -> volatile_t(const_t(array_t))
310+
# while CastXML produces the following nesting:
311+
# -> array_t(volatile_t(const_t))
312+
# For both cases, we must unwrap the types, remove const_t, and add
313+
# back the outer layers
306314
if isinstance(nake_type, cpptypes.array_t):
307315
is_v = is_volatile(nake_type)
308316
if is_v:
309-
base_type = nake_type.base.base.base
317+
result_type = nake_type.base.base.base
310318
else:
311-
base_type = nake_type.base.base
312-
result_type = base_type
319+
result_type = nake_type.base.base
313320
if is_v:
314321
result_type = cpptypes.volatile_t(result_type)
315322
return cpptypes.array_t(result_type, nake_type.size)
323+
324+
elif isinstance(nake_type, cpptypes.volatile_t):
325+
return cpptypes.volatile_t(nake_type.base.base)
326+
316327
return nake_type.base
317328

318329

unittests/data/type_traits.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,20 +420,19 @@ namespace no{
420420

421421
namespace remove_const{
422422
namespace before{
423-
424423
typedef const void x1;
425424
typedef const incomplete_type x2;
426425
typedef int* const x3;
427426
typedef int* volatile x4;
428427
typedef void const * x5;
428+
typedef int volatile const x6;
429+
typedef int const volatile x7;
429430

430431
typedef char arr_42[42];
431432
typedef char const arr_c_42[42];
432433
typedef char volatile arr_v_42[42];
433-
#ifdef __castxml__
434434
typedef char const volatile arr_cv_42[42];
435435
typedef char volatile const arr_vc_42[42];
436-
#endif
437436
}
438437

439438
namespace after{
@@ -442,14 +441,14 @@ namespace after{
442441
typedef int* x3;
443442
typedef int* volatile x4;
444443
typedef void const * x5;
444+
typedef int volatile x6;
445+
typedef int volatile x7;
445446

446447
typedef char arr_42[42];
447448
typedef char arr_c_42[42];
448449
typedef char volatile arr_v_42[42];
449-
#ifdef __castxml__
450450
typedef char volatile arr_cv_42[42];
451451
typedef char volatile arr_vc_42[42];
452-
#endif
453452
} }
454453

455454
namespace is_volatile{
@@ -480,6 +479,8 @@ namespace before{
480479
typedef volatile int x2;
481480
typedef int* x3;
482481
typedef void volatile * x4;
482+
typedef int volatile const x5;
483+
typedef int const volatile x6;
483484

484485
typedef char arr_42[42];
485486
typedef char const arr_c_42[42];
@@ -493,6 +494,8 @@ namespace after{
493494
typedef int x2;
494495
typedef int* x3;
495496
typedef void volatile * x4;
497+
typedef int const x5;
498+
typedef int const x6;
496499

497500
typedef char arr_42[42];
498501
typedef char const arr_c_42[42];

unittests/type_traits_tester.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ def __test_type_transformation(self, ns_name, transformer):
9494
declarations.is_same(
9595
transformed,
9696
tafter),
97-
("there is a difference between expected type and result. " +
98-
"typedef name: %s") % tbefore.decl_string)
97+
("there is a difference between expected type({0}) " +
98+
"and result({1}). typedef name: {2}").format(
99+
declarations.remove_declarated(tafter).decl_string,
100+
declarations.remove_declarated(transformed).decl_string,
101+
tbefore.decl_string))
99102

100103
def test_is_enum(self):
101104
self.__test_type_category('is_enum', declarations.is_enum)

0 commit comments

Comments
 (0)