1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
# Copyright 2007-2020 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Check that GDB can call C++ functions whose parameters have
# object type, and are either passed by value or implicitly by reference.
#
# Suppose F is a function that has a call-by-value parameter whose
# type is class C. When calling F with an argument A, a copy of A should
# be created and passed to F. If C is a trivially-copyable type, A can
# be copied by a straightforward memory copy. However, roughly speaking,
# if C has a user-defined copy constructor and/or a user-defined
# destructor, the copy ctor should be used to initialize the copy of A
# before calling F, and a reference to that copy is passed to F. After
# the function returns, the destructor should be called to destruct the
# copy. In this case, C is said to be a 'pass-by-reference' type.
# Determining whether C is pass-by-ref depends on
# how the copy ctor, destructor, and the move ctor of C are defined.
# First of all, C is not copy constructible if its copy constructor is
# explicitly or implicitly deleted. In this case, it would be illegal
# to pass values of type C to a function. C is pass-by-value, if all of
# its copy ctor, dtor, and move ctor are trivially defined.
# Otherwise, it is pass-by-ref.
#
# To cover the many possible combinations, this test generates classes
# that contain three special functions:
# (1) a copy constructor,
# (2) a destructor, and
# (3) a move constructor.
# A special function is in one of the following states:
# * explicit: The function is explicitly defined by the user.
# * defaultedIn: The function is defaulted inside the class decl,
# using the 'default' keyword.
# * defaultedOut: The function is declared inside the class decl,
# and defaulted outside using the 'default' keyword.
# * deleted: The function is explicitly deleted by the user,
# using the 'delete' keyword.
# * absent: The function is not declared by the user (i.e. it does not
# exist in the source. The compiler generates (or deletes) the
# definition in this case.
#
# The C++ ABI decides if a class is pass-by-value or pass-by-ref
# (i.e. trivially copyable or not) first at the language level, based
# on the state of the special functions. Then, at the target level, a
# class may be determined to be pass-by-ref because of its size
# (e.g. if it is too large to fit on registers). For this reason, this
# test generates both a small and a large version for the same
# combination of special function states.
#
# A class is not trivially-copyable if a base class or a field is not
# trivially-copyable, even though the class definition itself seems
# trivial. To test these cases, we also generate derived classes and
# container classes.
#
# The generated code is placed in the test output directory.
#
# The companion test file pass-by-ref-2.exp also contains
# manually-written cases.
if {[skip_cplus_tests]} {
untested "c++ test skipped"
continue
}
# The program source is generated in the output directory.
# We use standard_testfile here to set convenience variables.
standard_testfile .cc
# Some constant values used when generating the source
set SMALL 2
set LARGE 150
set ORIGINAL 2
set CUSTOM 3
set ADDED 4
set TRACE 5
# Return 1 if the class whose special function states are STATES
# is copyable. Otherwise return 0.
proc is_copy_constructible { states } {
set cctor [lindex $states 0]
set dtor [lindex $states 1]
set mctor [lindex $states 2]
if {$cctor == "deleted" || ($cctor == "absent" && $mctor != "absent")} {
return 0
}
return 1
}
# Generate a declaration and an out-of-class definition for a function
# with the provided signature. The STATE should be one of the following:
# - explicit, defaultedIn, defaultedOut, deleted, absent
proc generate_member_function { classname signature length state } {
set declaration ""
set definition ""
global CUSTOM
global TRACE
switch $state {
explicit {
set declaration "$signature;\n"
set definition "$classname\:\:$signature
{
data\[0\] = $CUSTOM;
data\[[expr $length - 1]\] = $CUSTOM;
tracer = $TRACE;
}\n"
}
defaultedIn {
set declaration "$signature = default;\n"
}
defaultedOut {
set declaration "$signature;\n"
set definition "$classname\:\:$signature = default;\n"
}
deleted {
set declaration "$signature = delete;\n"
}
default {
# function is not user-defined in this case
}
}
return [list $declaration $definition]
}
# Generate a C++ class with the given CLASSNAME and LENGTH-many
# integer elements. The STATES is an array of 3 items
# containing the desired state of the special functions
# in this order:
# copy constructor, destructor, move constructor
proc generate_class { classname length states } {
set declarations ""
set definitions ""
set classname "${classname}_[join $states _]"
for {set i 0} {$i < [llength $states]} {incr i} {
set sig ""
switch $i {
0 {set sig "$classname (const $classname \&rhs)"}
1 {set sig "\~$classname (void)"}
2 {set sig "$classname ($classname \&\&rhs)"}
}
set state [lindex $states $i]
set code [generate_member_function $classname $sig $length $state]
append declarations [lindex $code 0]
append definitions [lindex $code 1]
}
global ORIGINAL
return "
/*** C++ class $classname ***/
class ${classname} {
public:
$classname (void);
$declarations
int data\[$length\];
};
$classname\:\:$classname (void)
{
data\[0\] = $ORIGINAL;
data\[[expr $length - 1]\] = $ORIGINAL;
}
$definitions
$classname ${classname}_var; /* global var */
template int cbv<$classname> ($classname arg);"
}
# Generate a small C++ class
proc generate_small_class { states } {
global SMALL
return [generate_class Small $SMALL $states];
}
# Generate a large C++ class
proc generate_large_class { states } {
global LARGE
return [generate_class Large $LARGE $states];
}
# Generate a class that derives from a small class
proc generate_derived_class { states } {
set base "Small_[join $states _]"
set classname "Derived_[join $states _]"
return "
/*** Class derived from $base ***/
class $classname : public $base {
public:
};
$classname ${classname}_var; /* global var */
template int cbv<$classname> ($classname arg);"
}
# Generate a class that contains a small class item
proc generate_container_class { states } {
set contained "Small_[join $states _]"
set classname "Container_[join $states _]"
return "
/*** Class that contains $contained ***/
class $classname {
public:
$contained item;
};
$classname ${classname}_var; /* global var */
template int cbv_container<$classname> ($classname arg);"
}
# Generate useful statements that use a class in the debugee program
proc generate_stmts { classprefix states {cbvfun "cbv"}} {
set classname "${classprefix}_[join $states _]"
# Having an explicit call to the cbv function in the debugee program
# ensures that the compiler will emit necessary function in the binary.
if {[is_copy_constructible $states]} {
set cbvcall "$cbvfun<$classname> (${classname}_var);\n"
} else {
set cbvcall ""
}
return "$cbvcall"
}
# Generate the complete debugee program
proc generate_program { classes stmts } {
global ADDED
return "
/*** THIS FILE IS GENERATED BY THE TEST. ***/
static int tracer = 0;
/* The call-by-value function. */
template <class T>
int
cbv (T arg)
{
arg.data\[0\] += $ADDED; // intentionally modify the arg
return arg.data\[0\];
}
template <class T>
int
cbv_container (T arg)
{
arg.item.data\[0\] += $ADDED; // intentionally modify
return arg.item.data\[0\];
}
$classes
int
main (void)
{
$stmts
/* stop here */
return 0;
}"
}
# Compute all the combinations of special function states.
# We do not contain the 'deleted' state for the destructor,
# because it is illegal to have stack-allocated objects
# whose destructor have been deleted. This case is covered
# in pass-by-ref-2 via heap-allocated objects.
set options_nodelete [list absent explicit defaultedIn defaultedOut]
set options [concat $options_nodelete {deleted}]
set all_combinations {}
foreach cctor $options {
foreach dtor $options_nodelete {
foreach mctor $options {
lappend all_combinations [list $cctor $dtor $mctor]
}
}
}
# Generate the classes.
set classes ""
set stmts ""
foreach state $all_combinations {
append classes [generate_small_class $state]
append stmts [generate_stmts "Small" $state]
append classes [generate_large_class $state]
append stmts [generate_stmts "Large" $state]
append classes [generate_derived_class $state]
append stmts [generate_stmts "Derived" $state]
append classes [generate_container_class $state]
append stmts [generate_stmts "Container" $state "cbv_container"]
}
# Generate the program code and compile
set program [generate_program $classes $stmts]
set srcfile [standard_output_file ${srcfile}]
gdb_produce_source $srcfile $program
set options {debug c++ additional_flags=-std=c++11}
if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options]} {
return -1
}
if {![runto_main]} {
untested "failed to run to main"
return -1
}
set bp_location [gdb_get_line_number "stop here"]
gdb_breakpoint $bp_location
gdb_continue_to_breakpoint "end of main" ".*return .*;"
# Do the checks for a given class whose name is prefixed with PREFIX,
# and whose special functions have the states given in STATES.
# The name of the call-by-value function and the expression to access
# the data field can be specified explicitly if the default values
# do not work.
proc test_for_class { prefix states cbvfun data_field length} {
set name "${prefix}_[join $states _]"
set cctor [lindex $states 0]
set dtor [lindex $states 1]
set mctor [lindex $states 2]
global ORIGINAL
global CUSTOM
global ADDED
global TRACE
# GCC version <= 6 and Clang do not emit DW_AT_defaulted and DW_AT_deleted.
set is_gcc_6_or_older [test_compiler_info {gcc-[0-6]-*}]
set is_clang [test_compiler_info {clang-*}]
# But Clang version >= 7 emits DW_AT_calling_convention for types.
set is_clang_6_or_older [test_compiler_info {clang-[0-6]-*}]
with_test_prefix $name {
if {[is_copy_constructible $states]} {
set expected [expr {$ORIGINAL + $ADDED}]
if {$cctor == "explicit"} {
set expected [expr {$CUSTOM + $ADDED}]
}
if {$dtor == "explicit"} {
gdb_test "print tracer = 0" " = 0" "reset the tracer"
}
if {$cctor == "defaultedIn" || $dtor == "defaultedIn"} {
if {$is_gcc_6_or_older || $is_clang_6_or_older} {
setup_xfail "*-*-*"
} elseif {$is_clang} {
# If this is a pass-by-value case, Clang >= 7's
# DW_AT_calling_convention leads to the right decision.
# Otherwise, it is expected to fail.
if {"defaultedOut" in $states || "explicit" in $states} {
setup_xfail "*-*-*"
}
}
}
gdb_test "print ${cbvfun}<$name> (${name}_var)" " = $expected" \
"call '$cbvfun'"
gdb_test "print ${name}_var.${data_field}\[0\]" " = $ORIGINAL" \
"cbv argument should not change (item 0)"
if {$length > 1} {
set last_index [expr $length - 1]
gdb_test "print ${name}_var.${data_field}\[$last_index\]" \
" = $ORIGINAL" \
"cbv argument should not change (item $last_index)"
}
if {$dtor == "explicit"} {
if {$cctor == "defaultedIn"
&& ($is_gcc_6_or_older || $is_clang)} {
setup_xfail "*-*-*"
}
gdb_test "print tracer" " = $TRACE" \
"destructor should be called"
}
} else {
if {$cctor == "deleted" && ($is_gcc_6_or_older || $is_clang)} {
setup_xfail "*-*-*"
}
gdb_test "print ${cbvfun}<$name> (${name}_var)" \
".* cannot be evaluated .* '${name}' is not copy constructible" \
"calling '$cbvfun' should be refused"
}
}
}
foreach state $all_combinations {
test_for_class "Small" $state "cbv" "data" $SMALL
test_for_class "Large" $state "cbv" "data" $LARGE
test_for_class "Derived" $state "cbv" "data" 1
test_for_class "Container" $state "cbv_container" "item.data" 1
}
|