Clang Project

clang_source_code/test/CodeGenCXX/attr-no-destroy-d54344.cpp
1// RUN: %clang_cc1 -std=c++2a -emit-llvm -O0 -triple x86_64-unknown-linux-gnu -DNOATTR  %s -o - | FileCheck %s
2// RUN: %clang_cc1 -std=c++2a -emit-llvm -O0 -triple x86_64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=CHECK-ATTR
3// RUN: %clang_cc1 -std=c++2a -emit-llvm -O0 -triple x86_64-unknown-linux-gnu -DNOATTR -fno-c++-static-destructors %s -o - | FileCheck %s --check-prefix=CHECK-FLAG
4
5// Regression test for D54344. Class with no user-defined destructor
6// that has an inherited member that has a non-trivial destructor
7// and a non-default constructor will attempt to emit a destructor
8// despite being marked as __attribute((no_destroy)) in which case
9// it would trigger an assertion due to an incorrect assumption.
10
11// This test is more reliable with asserts to work as without 
12// the crash may (unlikely) could generate working but semantically
13// incorrect code.
14
15class a {
16public:
17  ~a();
18};
19class logger_base {
20  a d;
21};
22class e : logger_base {};
23#ifndef NOATTR
24__attribute((no_destroy))
25#endif
26e g;
27
28// In the absence of the attribute and flag, both ctor and dtor should
29// be emitted, check for that.
30// CHECK: @__cxx_global_var_init
31// CHECK: @__cxa_atexit
32
33// When attribute is enabled, the constructor should not be balanced
34// by a destructor. Make sure we have the ctor but not the dtor
35// registration.
36// CHECK-ATTR: @__cxx_global_var_init
37// CHECK-ATTR-NOT: @__cxa_atexit
38
39// Same scenario except with global flag (-fno-c++-static-destructors)
40// supressing it instead of the attribute. 
41// CHECK-FLAG: @__cxx_global_var_init
42// CHECK-FLAG-NOT: @__cxa_atexit
43