Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright 2018-2020 Free Software Foundation, Inc. |
007e1530 TT |
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 | # This file is part of the gdb testsuite | |
17 | ||
007e1530 TT |
18 | # The types we're going to test. |
19 | ||
20 | set typelist { | |
21 | char {unsigned char} | |
22 | short {unsigned short} | |
23 | int {unsigned int} | |
24 | long {unsigned long} | |
25 | {long long} {unsigned long long} | |
26 | float | |
27 | double {long double} | |
28 | } | |
29 | ||
30 | if {[has_int128_c]} { | |
31 | # Note we don't check "unsigned __int128" yet because at least gcc | |
32 | # canonicalizes the name to "__int128 unsigned", and there isn't a | |
33 | # c-exp.y production for this. | |
34 | # https://sourceware.org/bugzilla/show_bug.cgi?id=20991 | |
35 | lappend typelist __int128 | |
36 | } | |
37 | ||
bf9a735e AB |
38 | # Build source file for testing alignment handling of language LANG. |
39 | # Returns the name of the newly created source file. | |
40 | proc prepare_test_source_file { lang } { | |
41 | global typelist | |
42 | ||
43 | # Create the test file. | |
007e1530 | 44 | |
bf9a735e AB |
45 | if { $lang == "c++" } { |
46 | set suffix "cpp" | |
47 | set align_func "alignof" | |
48 | } else { | |
49 | set suffix "c" | |
50 | set align_func "_Alignof" | |
51 | } | |
007e1530 | 52 | |
971a3747 | 53 | set filename [standard_output_file "align.$suffix"] |
bf9a735e | 54 | set outfile [open $filename w] |
007e1530 | 55 | |
bf9a735e AB |
56 | # Prologue. |
57 | puts -nonewline $outfile "#define DEF(T,U) struct align_pair_ ## T ## _x_ ## U " | |
58 | puts $outfile "{ T one; U two; }" | |
59 | if { $lang == "c++" } { | |
41077b66 | 60 | puts -nonewline $outfile "#define DEF_WITH_1_STATIC(T,U) struct align_pair_static_ ## T ## _x_ ## U " |
bf9a735e | 61 | puts $outfile "{ static T one; U two; }" |
41077b66 AB |
62 | puts -nonewline $outfile "#define DEF_WITH_2_STATIC(T,U) struct align_pair_static_ ## T ## _x_static_ ## U " |
63 | puts $outfile "{ static T one; static U two; }" | |
bf9a735e AB |
64 | } |
65 | if { $lang == "c" } { | |
66 | puts $outfile "unsigned a_void = ${align_func} (void);" | |
007e1530 | 67 | } |
007e1530 | 68 | |
bf9a735e AB |
69 | # First emit single items. |
70 | foreach type $typelist { | |
71 | set utype [join [split $type] _] | |
72 | if {$type != $utype} { | |
73 | puts $outfile "typedef $type $utype;" | |
74 | } | |
75 | puts $outfile "$type item_$utype;" | |
76 | if { $lang == "c" } { | |
77 | puts $outfile "unsigned a_$utype\n = ${align_func} ($type);" | |
78 | } | |
79 | set utype [join [split $type] _] | |
007e1530 | 80 | } |
007e1530 | 81 | |
bf9a735e AB |
82 | # Now emit all pairs. |
83 | foreach type $typelist { | |
84 | set utype [join [split $type] _] | |
85 | foreach inner $typelist { | |
86 | set uinner [join [split $inner] _] | |
87 | puts $outfile "DEF ($utype, $uinner);" | |
88 | set joined "${utype}_x_${uinner}" | |
89 | puts $outfile "struct align_pair_$joined item_${joined};" | |
90 | puts $outfile "unsigned a_${joined}" | |
91 | puts $outfile " = ${align_func} (struct align_pair_${joined});" | |
92 | ||
93 | if { $lang == "c++" } { | |
41077b66 AB |
94 | puts $outfile "DEF_WITH_1_STATIC ($utype, $uinner);" |
95 | set joined "static_${utype}_x_${uinner}" | |
96 | puts $outfile "struct align_pair_$joined item_${joined};" | |
97 | puts $outfile "unsigned a_${joined}" | |
98 | puts $outfile " = ${align_func} (struct align_pair_${joined});" | |
99 | ||
100 | puts $outfile "DEF_WITH_2_STATIC ($utype, $uinner);" | |
101 | set joined "static_${utype}_x_static_${uinner}" | |
102 | puts $outfile "struct align_pair_$joined item_${joined};" | |
103 | puts $outfile "unsigned a_${joined}" | |
104 | puts $outfile " = ${align_func} (struct align_pair_${joined});" | |
bf9a735e AB |
105 | } |
106 | } | |
007e1530 | 107 | } |
007e1530 | 108 | |
bf9a735e AB |
109 | # Epilogue. |
110 | puts $outfile { | |
111 | int main() { | |
112 | return 0; | |
113 | } | |
114 | } | |
007e1530 | 115 | |
bf9a735e | 116 | close $outfile |
007e1530 | 117 | |
bf9a735e | 118 | return $filename |
007e1530 TT |
119 | } |
120 | ||
bf9a735e AB |
121 | # Run the alignment test for the language LANG. |
122 | proc run_alignment_test { lang } { | |
123 | global testfile srcfile typelist | |
124 | global subdir | |
125 | ||
126 | set filename [prepare_test_source_file $lang] | |
007e1530 | 127 | |
af1a8d03 TV |
128 | set flags {debug} |
129 | if { "$lang" == "c++" } { | |
130 | lappend flags "additional_flags=-std=c++11" | |
131 | } | |
bf9a735e | 132 | standard_testfile $filename |
971a3747 | 133 | if {[prepare_for_testing "failed to prepare" "$testfile" $srcfile $flags]} { |
bf9a735e AB |
134 | return -1 |
135 | } | |
136 | ||
137 | if {![runto_main]} { | |
138 | perror "test suppressed" | |
139 | return | |
140 | } | |
141 | ||
142 | if { $lang == "c++" } { | |
143 | set align_func "alignof" | |
144 | } else { | |
145 | set align_func "_Alignof" | |
146 | } | |
007e1530 | 147 | |
bf9a735e AB |
148 | foreach type $typelist { |
149 | set utype [join [split $type] _] | |
150 | if { $lang == "c" } { | |
151 | set expected [get_integer_valueof a_$utype 0] | |
152 | gdb_test "print ${align_func}($type)" " = $expected" | |
153 | } | |
154 | ||
155 | foreach inner $typelist { | |
156 | set uinner [join [split $inner] _] | |
157 | set expected [get_integer_valueof a_${utype}_x_${uinner} 0] | |
158 | gdb_test "print ${align_func}(struct align_pair_${utype}_x_${uinner})" \ | |
159 | " = $expected" | |
160 | ||
161 | if { $lang == "c++" } { | |
162 | set expected [get_integer_valueof a_static_${utype}_x_${uinner} 0] | |
163 | gdb_test "print ${align_func}(struct align_pair_static_${utype}_x_${uinner})" \ | |
164 | " = $expected" | |
41077b66 AB |
165 | |
166 | set expected [get_integer_valueof a_static_${utype}_x_static_${uinner} 0] | |
167 | gdb_test "print ${align_func}(struct align_pair_static_${utype}_x_static_${uinner})" \ | |
168 | " = $expected" | |
bf9a735e AB |
169 | } |
170 | } | |
171 | } | |
172 | ||
173 | if { $lang == "c" } { | |
174 | set expected [get_integer_valueof a_void 0] | |
175 | gdb_test "print ${align_func}(void)" " = $expected" | |
007e1530 TT |
176 | } |
177 | } | |
178 | ||
971a3747 | 179 | run_alignment_test $lang |