63 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <memory>10 11// Test weak_ptr<T> with trivial_abi as return-type.12 13// ADDITIONAL_COMPILE_FLAGS: -Wno-macro-redefined -D_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI14 15// XFAIL: gcc16 17#include <memory>18#include <cassert>19 20__attribute__((noinline)) void call_something() { asm volatile(""); }21 22struct Node {23 explicit Node() {}24 Node(const Node&) = default;25 Node& operator=(const Node&) = default;26 ~Node() {}27};28 29__attribute__((noinline)) std::weak_ptr<Node>30make_val(std::shared_ptr<Node>& sptr, void** local_addr) {31 call_something();32 33 std::weak_ptr<Node> ret;34 ret = sptr;35 36 // Capture the local address of ret.37 *local_addr = &ret;38 39 return ret;40}41 42int main(int, char**) {43 void* local_addr = nullptr;44 auto sptr = std::make_shared<Node>();45 std::weak_ptr<Node> ret = make_val(sptr, &local_addr);46 assert(local_addr != nullptr);47 48 // Without trivial_abi, &ret == local_addr because the return value49 // is allocated here in main's stackframe.50 //51 // With trivial_abi, local_addr is the address of a local variable in52 // make_val, and hence different from &ret.53#if !defined(__i386__) && !defined(__arm__) && !defined(_WIN32) && !defined(_AIX)54 // On X86, structs are never returned in registers.55 // On AIX, structs are never returned in registers.56 // On ARM32, structs larger than 4 bytes cannot be returned in registers.57 // On Windows, structs with a destructor are always returned indirectly.58 // Thus, weak_ptr will be passed indirectly even if it is trivial.59 assert((void*)&ret != local_addr);60#endif61 return 0;62}63