Debian Codegraph @

gcc/testsuite/g++.dg/torture/pr111019.C

raw
1 // { dg-do run }
2 // { dg-additional-options "-fstrict-aliasing" }
3 // { dg-skip-if "requires hosted libstdc++ for cassert" { ! hostedlib } }
4
5 #include <cassert>
6 #include <memory>
7 #include <string>
8
9 class Base
10 {
11 public:
12 Base* previous = nullptr;
13 Base* next = nullptr;
14 Base* target = nullptr;
15 };
16
17 class Target : public Base
18 {
19 public:
20 __attribute__((always_inline)) ~Target()
21 {
22 while (this->next)
23 {
24 Base* n = this->next;
25
26 if (n->previous)
27 n->previous->next = n->next;
28 if (n->next)
29 n->next->previous = n->previous;
30 n->previous = nullptr;
31 n->next = nullptr;
32 n->target = nullptr;
33 }
34 }
35 };
36
37 template <typename T>
38 class TargetWithData final : public Target
39 {
40 public:
41 TargetWithData(T data)
42 : data(data)
43 {}
44 T data;
45 };
46
47 void test()
48 {
49 printf("test\n");
50 Base ptr;
51 {
52 auto data = std::make_unique<TargetWithData<std::string>>(std::string("asdf"));
53 ptr.target = &*data;
54 ptr.previous = &*data;
55 data->next = &ptr;
56
57 assert(ptr.target != nullptr);
58 }
59 assert(ptr.target == nullptr);
60 }
61
62 int main(int, char**)
63 {
64 test();
65 return 0;
66 }