1 # Copyright 2018-2021 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the gdb testsuite
18 # This tests that C++ alignof works in gdb, and that it agrees with
21 if {[skip_cplus_tests]} { continue }
23 # The types we're going to test.
27 short {unsigned short}
30 {long long} {unsigned long long}
42 if {[has_int128_cxx]} {
43 # Note we don't check "unsigned __int128" yet because at least gcc
44 # canonicalizes the name to "__int128 unsigned", and there isn't a
45 # c-exp.y production for this.
46 # https://sourceware.org/bugzilla/show_bug.cgi?id=20991
47 lappend typelist __int128
50 # Create the test file.
52 set filename [standard_output_file align.cc]
53 set outfile [open $filename w]
57 template<typename T, typename U>
64 template<typename T, typename U>
71 enum bigenum { VALUE = 0xffffffffull };
75 struct vstruct { virtual ~vstruct() {} char c; };
77 struct bfstruct { unsigned b : 3; };
79 struct arrstruct { short fld[7]; };
81 unsigned a_int3 = alignof (int[3]);
83 #if !defined (__clang__)
84 unsigned a_void = alignof (void);
87 struct base { char c; };
88 struct derived : public virtual base { int i; };
90 struct b2 : public virtual base { char d; };
91 struct derived2 : public b2, derived { char e; };
94 # First emit single items.
95 foreach type $typelist {
96 set utype [join [split $type] _]
97 puts $outfile "$type item_$utype;"
98 puts $outfile "unsigned a_$utype\n = alignof ($type);"
99 puts $outfile "typedef $type t_$utype;"
100 puts $outfile "t_$utype item_t_$utype;"
103 # Now emit all pairs.
104 foreach type $typelist {
105 set utype [join [split $type] _]
106 foreach inner $typelist {
107 set uinner [join [split $inner] _]
108 puts $outfile "align_pair<$type, $inner> item_${utype}_x_${uinner};"
109 puts $outfile "unsigned a_${utype}_x_${uinner}"
110 puts $outfile " = alignof (align_pair<$type, $inner>);"
112 puts $outfile "align_union<$type, $inner> item_${utype}_u_${uinner};"
113 puts $outfile "unsigned a_${utype}_u_${uinner}"
114 puts $outfile " = alignof (align_union<$type, $inner>);"
127 standard_testfile $filename
129 if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
130 {debug nowarnings c++ additional_flags=-std=c++11}]} {
135 perror "test suppressed"
139 proc maybe_xfail {type} {
140 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69560
141 # The g++ implementation of alignof is changing to match C11.
142 if {[is_x86_like_target]
143 && [test_compiler_info {gcc-[0-8]-*}]
144 && ($type == "double" || $type == "long long"
145 || $type == "unsigned long long")} {
150 foreach type $typelist {
151 set utype [join [split $type] _]
152 set expected [get_integer_valueof a_$utype 0]
155 gdb_test "print alignof($type)" " = $expected"
158 gdb_test "print alignof(t_$utype)" " = $expected"
161 gdb_test "print alignof(typeof(item_$utype))" " = $expected"
163 foreach inner $typelist {
164 set uinner [join [split $inner] _]
165 set expected [get_integer_valueof a_${utype}_x_${uinner} 0]
166 gdb_test "print alignof(align_pair<${type},${inner}>)" " = $expected"
168 set expected [get_integer_valueof a_${utype}_u_${uinner} 0]
169 gdb_test "print alignof(align_union<${type},${inner}>)" " = $expected"
173 set expected [get_integer_valueof a_int3 0]
174 gdb_test "print alignof(int\[3\])" " = $expected"
176 # As an extension, GCC allows void pointer arithmetic, with
177 # sizeof(void) and alignof(void) both 1. This test checks
178 # GDB's support of GCC's extension.
179 if [test_compiler_info clang*] {
180 # Clang doesn't support GCC's extension.
183 set expected [get_integer_valueof a_void 0]
185 gdb_test "print alignof(void)" " = $expected"