brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · 65e9069 Raw
60 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 unique_ptr<T> with trivial_abi as return-type.12 13// ADDITIONAL_COMPILE_FLAGS: -Wno-macro-redefined -D_LIBCPP_ABI_ENABLE_UNIQUE_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::unique_ptr<Node> make_val(void** local_addr) {30  call_something();31 32  auto ret = std::unique_ptr<Node>(new Node);33 34  // Capture the local address of ret.35  *local_addr = &ret;36 37  return ret;38}39 40int main(int, char**) {41  void* local_addr = nullptr;42  auto ret = make_val(&local_addr);43  assert(local_addr != nullptr);44 45  // Without trivial_abi, &ret == local_addr because the return value46  // is allocated here in main's stackframe.47  //48  // With trivial_abi, local_addr is the address of a local variable in49  // make_val, and hence different from &ret.50#if !defined(__i386__) && !defined(_WIN32) && !defined(_AIX)51  // On X86, structs are never returned in registers.52  // On AIX, structs are never returned in registers.53  // Thus, unique_ptr will be passed indirectly even if it is trivial.54  // On Windows, structs with a destructor are always returned indirectly.55  assert((void*)&ret != local_addr);56#endif57 58  return 0;59}60