Home | History | Annotate | Line # | Download | only in gdb.mi
mi-var-rtti.cc revision 1.1
      1 /* Copyright 2012-2014 Free Software Foundation, Inc.
      2 
      3    This program is free software; you can redistribute it and/or modify
      4    it under the terms of the GNU General Public License as published by
      5    the Free Software Foundation; either version 3 of the License, or
      6    (at your option) any later version.
      7 
      8    This program is distributed in the hope that it will be useful,
      9    but WITHOUT ANY WARRANTY; without even the implied warranty of
     10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11    GNU General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License
     14    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     15 
     16 struct Base {
     17     Base() : A(1) {}
     18     virtual ~Base() {}  // Enforce type to have vtable
     19     int A;
     20 };
     21 
     22 struct Derived : public Base {
     23     Derived() : B(2), C(3) {}
     24     int B;
     25     int C;
     26 };
     27 
     28 
     29 void use_rtti_for_ptr_test ()
     30 {
     31   /*: BEGIN: use_rtti_for_ptr :*/
     32 	Derived d;
     33 	Base* ptr = &d;
     34 	const Base* constPtr = &d;
     35 	Base* const ptrConst = &d;
     36 	Base const* const constPtrConst = &d;
     37   /*:
     38 	set testname use_rtti_for_ptr
     39 	set_print_object off $testname
     40 	check_new_derived_without_rtti ptr {Base \*} $testname
     41 	check_new_derived_without_rtti constPtr {const Base \*} $testname
     42 	check_new_derived_without_rtti ptrConst {Base \* const} $testname
     43 	check_new_derived_without_rtti constPtrConst {const Base \* const} \
     44 		$testname
     45 
     46 	set_print_object on $testname
     47 	check_new_derived_with_rtti ptr {Derived \*} $testname
     48 	check_new_derived_with_rtti constPtr {const Derived \*} $testname
     49 	check_new_derived_with_rtti ptrConst {Derived \* const} $testname
     50 	check_new_derived_with_rtti constPtrConst {const Derived \* const} \
     51 		$testname
     52   :*/
     53 	return;
     54   /*: END: use_rtti_for_ptr :*/
     55 }
     56 
     57 
     58 void use_rtti_for_ref_test ()
     59 {
     60   /*: BEGIN: use_rtti_for_ref :*/
     61 	Derived d;
     62 	Base& ref = d;
     63 	const Base& constRef = d;
     64   /*:
     65 	set testname use_rtti_for_ref
     66 	set_print_object off $testname
     67 	check_new_derived_without_rtti ref {Base \&} $testname
     68 	check_new_derived_without_rtti constRef {const Base \&} $testname
     69 
     70 	set_print_object on $testname
     71 	check_new_derived_with_rtti ref {Derived \&} $testname
     72 	check_new_derived_with_rtti constRef {const Derived \&} $testname
     73   :*/
     74 	return;
     75   /*: END: use_rtti_for_ref :*/
     76 }
     77 
     78 
     79 void use_rtti_for_ptr_child_test ()
     80 {
     81   /*: BEGIN: use_rtti_for_ptr_child :*/
     82 	Derived d;
     83 	struct S {
     84 		Base* ptr;
     85 		const Base* constPtr;
     86 		Base* const ptrConst;
     87 		Base const* const constPtrConst;
     88 		S ( Base* v ) :
     89 			ptr ( v ),
     90 			constPtr ( v ),
     91 			ptrConst ( v ),
     92 			constPtrConst ( v ) {}
     93 	} s ( &d );
     94   /*:
     95 	set testname use_rtti_for_ptr_child
     96 
     97 	set_print_object off $testname
     98 	mi_create_varobj VAR s "create varobj for s (without RTTI) in $testname"
     99 	mi_list_varobj_children VAR {
    100 	    { VAR.public public 4 }
    101 	} "list children of s (without RTTI) in $testname"
    102 	mi_list_varobj_children VAR.public {
    103 	    { VAR.public.ptr ptr 1 {Base \*} }
    104 	    { VAR.public.constPtr constPtr 1 {const Base \*} }
    105 	    { VAR.public.ptrConst ptrConst 1 {Base \* const} }
    106 	    { VAR.public.constPtrConst constPtrConst 1 {const Base \* const} }
    107 	} "list children of s.public (without RTTI) in $testname"
    108 	check_derived_without_rtti VAR.public.ptr s.ptr $testname
    109 	check_derived_without_rtti VAR.public.constPtr s.constPtr $testname
    110 	check_derived_without_rtti VAR.public.ptrConst s.ptrConst $testname
    111 	check_derived_without_rtti VAR.public.constPtrConst s.constPtrConst \
    112 		$testname
    113 	mi_delete_varobj VAR "delete varobj for s (without RTTI) in $testname"
    114 
    115 	set_print_object on $testname
    116 	mi_create_varobj VAR s "create varobj for s (with RTTI) in $testname"
    117 	mi_list_varobj_children VAR {
    118 	    { VAR.public public 4 }
    119 	} "list children of s (with RTTI) in $testname"
    120 	mi_list_varobj_children VAR.public {
    121 	    { VAR.public.ptr ptr 2 {Derived \*} }
    122 	    { VAR.public.constPtr constPtr 2 {const Derived \*} }
    123 	    { VAR.public.ptrConst ptrConst 2 {Derived \* const} }
    124 	    { VAR.public.constPtrConst constPtrConst 2 {const Derived \* const}}
    125 	} "list children of s.public (with RTTI) in $testname"
    126 	check_derived_with_rtti VAR.public.ptr s.ptr $testname
    127 	check_derived_with_rtti VAR.public.constPtr s.constPtr $testname
    128 	check_derived_with_rtti VAR.public.ptrConst s.ptrConst $testname
    129 	check_derived_with_rtti VAR.public.constPtrConst s.constPtrConst \
    130 		$testname
    131 	mi_delete_varobj VAR "delete varobj for s (with RTTI) in $testname"
    132   :*/
    133 	return;
    134   /*: END: use_rtti_for_ptr_child :*/
    135 }
    136 
    137 
    138 void use_rtti_for_ref_child_test ()
    139 {
    140   /*: BEGIN: use_rtti_for_ref_child :*/
    141 	Derived d;
    142 	struct S {
    143 		Base& ref;
    144 		const Base& constRef;
    145 		S ( Base& v ) :
    146 			ref ( v ),
    147 			constRef ( v ) {}
    148 	} s ( d );
    149   /*:
    150 	set testname use_rtti_for_ref_child
    151 
    152 	set_print_object off $testname
    153 	mi_create_varobj VAR s "create varobj for s (without RTTI) in $testname"
    154 	mi_list_varobj_children VAR {
    155 	    { VAR.public public 2 }
    156 	} "list children of s (without RTTI) in $testname"
    157 	mi_list_varobj_children VAR.public {
    158 	    { VAR.public.ref ref 1 {Base \&} }
    159 	    { VAR.public.constRef constRef 1 {const Base \&} }
    160 	} "list children of s.public (without RTTI) in $testname"
    161 	check_derived_without_rtti VAR.public.ref s.ref $testname
    162 	check_derived_without_rtti VAR.public.constRef s.constRef  $testname
    163 	mi_delete_varobj VAR "delete varobj for s (without RTTI) in $testname"
    164 
    165 	set_print_object on $testname
    166 	mi_create_varobj VAR s "create varobj for s (with RTTI) in $testname"
    167 	mi_list_varobj_children VAR {
    168 	    { VAR.public public 2 }
    169 	} "list children of s (with RTTI) in $testname"
    170 	mi_list_varobj_children VAR.public {
    171 	    { VAR.public.ref ref 2 {Derived \&} }
    172 	    { VAR.public.constRef constRef 2 {const Derived \&} }
    173 	} "list children of s.public (with RTTI) in $testname"
    174 	check_derived_with_rtti VAR.public.ref s.ref $testname
    175 	check_derived_with_rtti VAR.public.constRef s.constRef $testname
    176 	mi_delete_varobj VAR "delete varobj for s (with RTTI) in $testname"
    177   :*/
    178 	return;
    179   /*: END: use_rtti_for_ref_child :*/
    180 }
    181 
    182 
    183 struct First {
    184     First() : F(-1) {}
    185     int F;
    186 };
    187 
    188 
    189 struct MultipleDerived : public First, Base {
    190     MultipleDerived() : B(2), C(3) {}
    191     int B;
    192     int C;
    193 };
    194 
    195 
    196 void use_rtti_with_multiple_inheritence_test ()
    197 {
    198   /*: BEGIN: use_rtti_with_multiple_inheritence :*/
    199 	MultipleDerived d;
    200 	Base* ptr = &d;
    201 	Base& ref = d;
    202   /*:
    203 	set testname use_rtti_with_multiple_inheritence
    204 	set_print_object off $testname
    205 	check_new_derived_without_rtti ptr {Base \*} $testname
    206 	check_new_derived_without_rtti ref {Base \&} $testname
    207 
    208 	set_print_object on $testname
    209 	mi_create_varobj_checked VAR ptr {MultipleDerived \*} \
    210 	    "create varobj for ptr (with RTTI) in $testname"
    211 	mi_list_varobj_children VAR {
    212 	    { VAR.First First 1 First }
    213 	    { VAR.Base Base 1 Base }
    214 	    { VAR.public public 2 }
    215 	} "list children of ptr (with RTTI) in $testname"
    216 	mi_list_varobj_children "VAR.First" {
    217 	    { VAR.First.public public 1 }
    218 	} "list children of ptr.First (with RTTI) in $testname"
    219 	mi_list_varobj_children "VAR.First.public" {
    220 	    { VAR.First.public.F F 0 int }
    221 	} "list children of ptr.Base.public (with RTTI) in $testname"
    222 	mi_list_varobj_children "VAR.Base" {
    223 	    { VAR.Base.public public 1 }
    224 	} "list children of ptr.Base (with RTTI) in $testname"
    225 	mi_list_varobj_children "VAR.Base.public" {
    226 	    { VAR.Base.public.A A 0 int }
    227 	} "list children of ptr.Base.public (with RTTI) in $testname"
    228 	mi_list_varobj_children "VAR.public" {
    229 	    { VAR.public.B B 0 int }
    230 	    { VAR.public.C C 0 int }
    231 	} "list children of ptr.public (with RTTI) in $testname"
    232 
    233 	mi_delete_varobj VAR \
    234 	    "delete varobj for ptr (with RTTI) in $testname"
    235   :*/
    236 	return;
    237   /*: END: use_rtti_with_multiple_inheritence :*/
    238 }
    239 
    240 
    241 void type_update_when_use_rtti_test ()
    242 {
    243   /*: BEGIN: type_update_when_use_rtti :*/
    244 	Derived d;
    245   /*:
    246 	set testname type_update_when_use_rtti
    247 
    248 	set_print_object on $testname
    249 	mi_create_varobj_checked PTR ptr {Base \*} \
    250 		"create varobj for ptr in $testname"
    251 	check_derived_children_without_rtti PTR ptr $testname
    252 
    253 	mi_create_varobj S s "create varobj for S in $testname"
    254 	mi_list_varobj_children S {
    255 	    { S.public public 1 }
    256 	} "list children of s in $testname"
    257 	mi_list_varobj_children S.public {
    258 	    { S.public.ptr ptr 1 {Base \*} }
    259 	} "list children of s.public in $testname"
    260 	check_derived_children_without_rtti S.public.ptr s.ptr $testname
    261   :*/
    262 
    263 	Base* ptr = &d;
    264 	struct S {
    265 		Base* ptr;
    266 		S ( Base* v ) :
    267 			ptr ( v ) {}
    268 	} s ( &d );
    269   /*:
    270 	mi_varobj_update_with_type_change PTR {Derived \*} 2 \
    271 		"update ptr to derived in $testname"
    272 	check_derived_with_rtti PTR ptr $testname
    273 
    274 	mi_varobj_update_with_child_type_change S S.public.ptr {Derived \*} 2 \
    275 		"update s.ptr to derived in $testname"
    276 	check_derived_with_rtti S.public.ptr s.ptr $testname
    277   :*/
    278 
    279 	ptr = 0;
    280 	s.ptr = 0;
    281   /*:
    282 	mi_varobj_update_with_type_change PTR {Base \*} 1 \
    283 		"update ptr back to base type in $testname"
    284 	mi_delete_varobj PTR "delete varobj for ptr in $testname"
    285 
    286 	mi_varobj_update_with_child_type_change S S.public.ptr {Base \*} 1 \
    287 		"update s.ptr back to base type in $testname"
    288 	mi_delete_varobj S "delete varobj for s in $testname"
    289   :*/
    290 	return;
    291   /*: END: type_update_when_use_rtti :*/
    292 }
    293 
    294 
    295 void skip_type_update_when_not_use_rtti_test ()
    296 {
    297   /*: BEGIN: skip_type_update_when_not_use_rtti :*/
    298 	Derived d;
    299   /*:
    300 	set testname skip_type_update_when_not_use_rtti
    301 
    302 	set_print_object off $testname
    303 	mi_create_varobj_checked PTR ptr {Base \*} \
    304 		"create varobj for ptr in $testname"
    305 	check_derived_children_without_rtti PTR ptr $testname
    306 
    307 	mi_create_varobj S s "create varobj for S in $testname"
    308 	mi_list_varobj_children S {
    309 	    { S.public public 1 }
    310 	} "list children of s in $testname"
    311 	mi_list_varobj_children S.public {
    312 	    { S.public.ptr ptr 1 {Base \*} }
    313 	} "list children of s.public in $testname"
    314 	check_derived_children_without_rtti S.public.ptr s.ptr $testname
    315   :*/
    316 
    317 	Base* ptr = &d;
    318 	struct S {
    319 		Base* ptr;
    320 		S ( Base* v ) :
    321 			ptr ( v ) {}
    322 	} s ( &d );
    323   /*:
    324 	mi_varobj_update PTR {PTR PTR.public.A} \
    325 		"update ptr to derived type in $testname"
    326 	check_derived_without_rtti PTR ptr $testname
    327 
    328 	mi_varobj_update S {S.public.ptr S.public.ptr.public.A} \
    329 		"update s to derived type in $testname"
    330 	check_derived_without_rtti S.public.ptr s.ptr $testname
    331   :*/
    332 
    333 	ptr = 0;
    334 	s.ptr = 0;
    335   /*:
    336 	mi_varobj_update PTR {PTR  PTR.public.A} \
    337 		"update ptr back to base type in $testname"
    338 	mi_delete_varobj PTR "delete varobj for ptr in $testname"
    339 
    340 	mi_varobj_update S {S.public.ptr S.public.ptr.public.A} \
    341 		"update s back to base type in $testname"
    342 	mi_delete_varobj S "delete varobj for s in $testname"
    343   :*/
    344 	return;
    345   /*: END: skip_type_update_when_not_use_rtti :*/
    346 }
    347 
    348 
    349 int main ()
    350 {
    351 	use_rtti_for_ptr_test();
    352 	use_rtti_for_ref_test();
    353 	use_rtti_for_ptr_child_test();
    354 	use_rtti_for_ref_child_test();
    355 	use_rtti_with_multiple_inheritence_test();
    356 	type_update_when_use_rtti_test();
    357 	skip_type_update_when_not_use_rtti_test();
    358 	return 0;
    359 }
    360