# This file is part of the gdb testsuite
-# This tests that C11 _Alignof works in gdb, and that it agrees with
-# the compiler.
+# This tests that C11 _Alignof and C++11 alignof works in gdb, and
+# that it agrees with the compiler.
+
+# Only test C++ if we are able. Always use C.
+if { [skip_cplus_tests] || [get_compiler_info "c++"] } {
+ set lang {c}
+} else {
+ set lang {c c++}
+}
# The types we're going to test.
lappend typelist __int128
}
-# Create the test file.
+# Build source file for testing alignment handling of language LANG.
+# Returns the name of the newly created source file.
+proc prepare_test_source_file { lang } {
+ global typelist
+
+ # Create the test file.
-set filename [standard_output_file align.c]
-set outfile [open $filename w]
+ if { $lang == "c++" } {
+ set suffix "cpp"
+ set align_func "alignof"
+ } else {
+ set suffix "c"
+ set align_func "_Alignof"
+ }
-# Prologue.
-puts -nonewline $outfile "#define DEF(T,U) struct align_pair_ ## T ## _x_ ## U "
-puts $outfile "{ T one; U two; }"
-puts $outfile "unsigned a_void = _Alignof(void);"
+ set filename [standard_output_file "$lang/align.$suffix"]
+ set outfile [open $filename w]
-# First emit single items.
-foreach type $typelist {
- set utype [join [split $type] _]
- if {$type != $utype} {
- puts $outfile "typedef $type $utype;"
+ # Prologue.
+ puts -nonewline $outfile "#define DEF(T,U) struct align_pair_ ## T ## _x_ ## U "
+ puts $outfile "{ T one; U two; }"
+ if { $lang == "c++" } {
+ puts -nonewline $outfile "#define DEF_WITH_STATIC(T,U) struct align_pair_static_ ## T ## _x_ ## U "
+ puts $outfile "{ static T one; U two; }"
+ }
+ if { $lang == "c" } {
+ puts $outfile "unsigned a_void = ${align_func} (void);"
}
- puts $outfile "$type item_$utype;"
- puts $outfile "unsigned a_$utype\n = _Alignof ($type);"
- set utype [join [split $type] _]
-}
-# Now emit all pairs.
-foreach type $typelist {
- set utype [join [split $type] _]
- foreach inner $typelist {
- set uinner [join [split $inner] _]
- puts $outfile "DEF ($utype, $uinner);"
- set joined "${utype}_x_${uinner}"
- puts $outfile "struct align_pair_$joined item_${joined};"
- puts $outfile "unsigned a_${joined}"
- puts $outfile " = _Alignof (struct align_pair_${joined});"
+ # First emit single items.
+ foreach type $typelist {
+ set utype [join [split $type] _]
+ if {$type != $utype} {
+ puts $outfile "typedef $type $utype;"
+ }
+ puts $outfile "$type item_$utype;"
+ if { $lang == "c" } {
+ puts $outfile "unsigned a_$utype\n = ${align_func} ($type);"
+ }
+ set utype [join [split $type] _]
}
-}
-# Epilogue.
-puts $outfile {
- int main() {
- return 0;
+ # Now emit all pairs.
+ foreach type $typelist {
+ set utype [join [split $type] _]
+ foreach inner $typelist {
+ set uinner [join [split $inner] _]
+ puts $outfile "DEF ($utype, $uinner);"
+ set joined "${utype}_x_${uinner}"
+ puts $outfile "struct align_pair_$joined item_${joined};"
+ puts $outfile "unsigned a_${joined}"
+ puts $outfile " = ${align_func} (struct align_pair_${joined});"
+
+ if { $lang == "c++" } {
+ puts $outfile "DEF_WITH_STATIC ($utype, $uinner);"
+ set joined "${utype}_x_${uinner}"
+ puts $outfile "struct align_pair_static_$joined item_static_${joined};"
+ puts $outfile "unsigned a_static_${joined}"
+ puts $outfile " = ${align_func} (struct align_pair_static_${joined});"
+ }
+ }
}
-}
-close $outfile
+ # Epilogue.
+ puts $outfile {
+ int main() {
+ return 0;
+ }
+ }
-standard_testfile $filename
+ close $outfile
-if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug}]} {
- return -1
+ return $filename
}
-if {![runto_main]} {
- perror "test suppressed"
- return
-}
+# Run the alignment test for the language LANG.
+proc run_alignment_test { lang } {
+ global testfile srcfile typelist
+ global subdir
+
+ set filename [prepare_test_source_file $lang]
-foreach type $typelist {
- set utype [join [split $type] _]
- set expected [get_integer_valueof a_$utype 0]
- gdb_test "print _Alignof($type)" " = $expected"
+ standard_testfile $filename
+ if {[prepare_for_testing "failed to prepare" "$lang/$testfile" $srcfile {debug}]} {
+ return -1
+ }
+
+ if {![runto_main]} {
+ perror "test suppressed"
+ return
+ }
+
+ if { $lang == "c++" } {
+ set align_func "alignof"
+ } else {
+ set align_func "_Alignof"
+ }
- foreach inner $typelist {
- set uinner [join [split $inner] _]
- set expected [get_integer_valueof a_${utype}_x_${uinner} 0]
- gdb_test "print _Alignof(struct align_pair_${utype}_x_${uinner})" \
- " = $expected"
+ foreach type $typelist {
+ set utype [join [split $type] _]
+ if { $lang == "c" } {
+ set expected [get_integer_valueof a_$utype 0]
+ gdb_test "print ${align_func}($type)" " = $expected"
+ }
+
+ foreach inner $typelist {
+ set uinner [join [split $inner] _]
+ set expected [get_integer_valueof a_${utype}_x_${uinner} 0]
+ gdb_test "print ${align_func}(struct align_pair_${utype}_x_${uinner})" \
+ " = $expected"
+
+ if { $lang == "c++" } {
+ set expected [get_integer_valueof a_static_${utype}_x_${uinner} 0]
+ gdb_test "print ${align_func}(struct align_pair_static_${utype}_x_${uinner})" \
+ " = $expected"
+ }
+ }
+ }
+
+ if { $lang == "c" } {
+ set expected [get_integer_valueof a_void 0]
+ gdb_test "print ${align_func}(void)" " = $expected"
}
}
-set expected [get_integer_valueof a_void 0]
-gdb_test "print _Alignof(void)" " = $expected"
+# Create nested 'c' and 'c++' directories within this tests directory.
+foreach l $lang {
+ set dir "$l"
+ remote_exec host "rm -rf [standard_output_file ${dir}]"
+ remote_exec host "mkdir -p [standard_output_file ${dir}]"
+}
+
+# Now run the test for each language.
+foreach_with_prefix l $lang {
+ run_alignment_test $l
+}