1*1424dfb3Schristos# Copyright 2018-2020 Free Software Foundation, Inc.
207163879Schristos
307163879Schristos# This program is free software; you can redistribute it and/or modify
407163879Schristos# it under the terms of the GNU General Public License as published by
507163879Schristos# the Free Software Foundation; either version 3 of the License, or
607163879Schristos# (at your option) any later version.
707163879Schristos#
807163879Schristos# This program is distributed in the hope that it will be useful,
907163879Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
1007163879Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1107163879Schristos# GNU General Public License for more details.
1207163879Schristos#
1307163879Schristos# You should have received a copy of the GNU General Public License
1407163879Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1507163879Schristos
1607163879Schristos# This file is part of the gdb testsuite
1707163879Schristos
1807163879Schristos# This tests that C++ alignof works in gdb, and that it agrees with
1907163879Schristos# the compiler.
2007163879Schristos
2107163879Schristosif {[skip_cplus_tests]} { continue }
2207163879Schristos
2307163879Schristos# The types we're going to test.
2407163879Schristos
2507163879Schristosset typelist {
2607163879Schristos    char {unsigned char}
2707163879Schristos    short {unsigned short}
2807163879Schristos    int {unsigned int}
2907163879Schristos    long {unsigned long}
3007163879Schristos    {long long} {unsigned long long}
3107163879Schristos    float
3207163879Schristos    double {long double}
3307163879Schristos    empty
3407163879Schristos    bigenum
3507163879Schristos    vstruct
3607163879Schristos    bfstruct
3707163879Schristos    arrstruct
3807163879Schristos    derived
3907163879Schristos    derived2
4007163879Schristos}
4107163879Schristos
4207163879Schristosif {[has_int128_cxx]} {
4307163879Schristos    # Note we don't check "unsigned __int128" yet because at least gcc
4407163879Schristos    # canonicalizes the name to "__int128 unsigned", and there isn't a
4507163879Schristos    # c-exp.y production for this.
4607163879Schristos    # https://sourceware.org/bugzilla/show_bug.cgi?id=20991
4707163879Schristos    lappend typelist __int128
4807163879Schristos}
4907163879Schristos
5007163879Schristos# Create the test file.
5107163879Schristos
5207163879Schristosset filename [standard_output_file align.cc]
5307163879Schristosset outfile [open $filename w]
5407163879Schristos
5507163879Schristos# Prologue.
5607163879Schristosputs $outfile {
5707163879Schristos    template<typename T, typename U>
5807163879Schristos    struct align_pair
5907163879Schristos    {
6007163879Schristos	T one;
6107163879Schristos	U two;
6207163879Schristos    };
6307163879Schristos
6407163879Schristos    template<typename T, typename U>
6507163879Schristos    struct align_union
6607163879Schristos    {
6707163879Schristos	T one;
6807163879Schristos	U two;
6907163879Schristos    };
7007163879Schristos
7107163879Schristos    enum bigenum { VALUE = 0xffffffffull };
7207163879Schristos
7307163879Schristos    struct empty { };
7407163879Schristos
7507163879Schristos    struct vstruct { virtual ~vstruct() {}  char c; };
7607163879Schristos
7707163879Schristos    struct bfstruct { unsigned b : 3; };
7807163879Schristos
7907163879Schristos    struct arrstruct { short fld[7]; };
8007163879Schristos
8107163879Schristos    unsigned a_int3 = alignof (int[3]);
8207163879Schristos
83*1424dfb3Schristos#if !defined (__clang__)
8407163879Schristos    unsigned a_void = alignof (void);
85*1424dfb3Schristos#endif
8607163879Schristos
8707163879Schristos    struct base { char c; };
8807163879Schristos    struct derived : public virtual base { int i; };
8907163879Schristos
9007163879Schristos    struct b2 : public virtual base { char d; };
9107163879Schristos    struct derived2 : public b2, derived { char e; };
9207163879Schristos}
9307163879Schristos
9407163879Schristos# First emit single items.
9507163879Schristosforeach type $typelist {
9607163879Schristos    set utype [join [split $type] _]
9707163879Schristos    puts $outfile "$type item_$utype;"
9807163879Schristos    puts $outfile "unsigned a_$utype\n  = alignof ($type);"
9907163879Schristos    puts $outfile "typedef $type t_$utype;"
10007163879Schristos    puts $outfile "t_$utype item_t_$utype;"
10107163879Schristos}
10207163879Schristos
10307163879Schristos# Now emit all pairs.
10407163879Schristosforeach type $typelist {
10507163879Schristos    set utype [join [split $type] _]
10607163879Schristos    foreach inner $typelist {
10707163879Schristos	set uinner [join [split $inner] _]
10807163879Schristos	puts $outfile "align_pair<$type, $inner> item_${utype}_x_${uinner};"
10907163879Schristos	puts $outfile "unsigned a_${utype}_x_${uinner}"
11007163879Schristos	puts $outfile "  = alignof (align_pair<$type, $inner>);"
11107163879Schristos
11207163879Schristos	puts $outfile "align_union<$type, $inner> item_${utype}_u_${uinner};"
11307163879Schristos	puts $outfile "unsigned a_${utype}_u_${uinner}"
11407163879Schristos	puts $outfile "  = alignof (align_union<$type, $inner>);"
11507163879Schristos    }
11607163879Schristos}
11707163879Schristos
11807163879Schristos# Epilogue.
11907163879Schristosputs $outfile {
12007163879Schristos    int main() {
12107163879Schristos	return 0;
12207163879Schristos    }
12307163879Schristos}
12407163879Schristos
12507163879Schristosclose $outfile
12607163879Schristos
12707163879Schristosstandard_testfile $filename
12807163879Schristos
12907163879Schristosif {[prepare_for_testing "failed to prepare" $testfile $srcfile \
13007163879Schristos	 {debug nowarnings c++ additional_flags=-std=c++11}]} {
13107163879Schristos    return -1
13207163879Schristos}
13307163879Schristos
13407163879Schristosif {![runto_main]} {
13507163879Schristos    perror "test suppressed"
13607163879Schristos    return
13707163879Schristos}
13807163879Schristos
13907163879Schristosproc maybe_xfail {type} {
14007163879Schristos    # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69560
14107163879Schristos    # The g++ implementation of alignof is changing to match C11.
14207163879Schristos    if {[is_x86_like_target]
14307163879Schristos	&& [test_compiler_info {gcc-[0-8]-*}]
14407163879Schristos	&& ($type == "double" || $type == "long long"
14507163879Schristos	    || $type == "unsigned long long")} {
14607163879Schristos	setup_xfail *-*-*
14707163879Schristos    }
14807163879Schristos}
14907163879Schristos
15007163879Schristosforeach type $typelist {
15107163879Schristos    set utype [join [split $type] _]
15207163879Schristos    set expected [get_integer_valueof a_$utype 0]
15307163879Schristos
15407163879Schristos    maybe_xfail $type
15507163879Schristos    gdb_test "print alignof($type)" " = $expected"
15607163879Schristos
15707163879Schristos    maybe_xfail $type
15807163879Schristos    gdb_test "print alignof(t_$utype)" " = $expected"
15907163879Schristos
16007163879Schristos    maybe_xfail $type
16107163879Schristos    gdb_test "print alignof(typeof(item_$utype))" " = $expected"
16207163879Schristos
16307163879Schristos    foreach inner $typelist {
16407163879Schristos	set uinner [join [split $inner] _]
16507163879Schristos	set expected [get_integer_valueof a_${utype}_x_${uinner} 0]
16607163879Schristos	gdb_test "print alignof(align_pair<${type},${inner}>)" " = $expected"
16707163879Schristos
16807163879Schristos	set expected [get_integer_valueof a_${utype}_u_${uinner} 0]
16907163879Schristos	gdb_test "print alignof(align_union<${type},${inner}>)" " = $expected"
17007163879Schristos    }
17107163879Schristos}
17207163879Schristos
17307163879Schristosset expected [get_integer_valueof a_int3 0]
17407163879Schristosgdb_test "print alignof(int\[3\])" " = $expected"
175*1424dfb3Schristos
176*1424dfb3Schristos# As an extension, GCC allows void pointer arithmetic, with
177*1424dfb3Schristos# sizeof(void) and alignof(void) both 1.  This test checks
178*1424dfb3Schristos# GDB's support of GCC's extension.
179*1424dfb3Schristosif [test_compiler_info clang*] {
180*1424dfb3Schristos    # Clang doesn't support GCC's extension.
181*1424dfb3Schristos    set expected 1
182*1424dfb3Schristos} else {
18307163879Schristos    set expected [get_integer_valueof a_void 0]
184*1424dfb3Schristos}
18507163879Schristosgdb_test "print alignof(void)" " = $expected"
186