aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/torture/pr71002.C
blob: 8a726809217037a2ce503c3a4d23bcaeceb3e4d2 (plain)
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
// { dg-do run }

using size_t = __SIZE_TYPE__;

inline void* operator new(size_t, void* p) noexcept
{ return p; }

inline void operator delete(void*, void*)
{ }

struct long_t
{
  size_t is_short : 1;
  size_t length   : (__SIZEOF_SIZE_T__ * __CHAR_BIT__ - 1);
  size_t capacity;
  char* pointer;
};

union long_raw_t {
  unsigned char data[sizeof(long_t)];
  struct __attribute__((aligned(alignof(long_t)))) { } align;
};

struct short_header
{
  unsigned char is_short : 1;
  unsigned char length   : (__CHAR_BIT__ - 1);
};

struct short_t
{
  short_header h;
  char data[23];
};

union repr_t
{
  long_raw_t  r;
  short_t     s;

  const short_t& short_repr() const
  { return s; }

  const long_t& long_repr() const
  { return *static_cast<const long_t*>(static_cast<const void*>(&r)); }

  short_t& short_repr()
  { return s;  }

  long_t& long_repr()
  { return *static_cast<long_t*>(static_cast<void*>(&r)); }
};

class string
{
public:
  string()
  {
    short_t& s = m_repr.short_repr();
    s.h.is_short = 1;
    s.h.length = 0;
    s.data[0] = '\0';
  }

  string(const char* str)
  {
    size_t length = __builtin_strlen(str);
    if (length + 1 > 23) {
      long_t& l = m_repr.long_repr();
      l.is_short = 0;
      l.length = length;
      l.capacity = length + 1;
      l.pointer = new char[l.capacity];
      __builtin_memcpy(l.pointer, str, length + 1);
    } else {
      short_t& s = m_repr.short_repr();
      s.h.is_short = 1;
      s.h.length = length;
      __builtin_memcpy(s.data, str, length + 1);
    }
  }

  string(string&& other)
    : string{}
  {
    swap_data(other);
  }

  ~string()
  {
    if (!is_short()) {
      delete[] m_repr.long_repr().pointer;
    }
  }

  size_t length() const
  { return is_short() ? short_length() : long_length(); }

private:
  bool is_short() const
  { return m_repr.s.h.is_short != 0; }

  size_t short_length() const
  { return m_repr.short_repr().h.length; }

  size_t long_length() const
  { return m_repr.long_repr().length; }

  void swap_data(string& other)
  {
    if (is_short()) {
      if (other.is_short()) {
        repr_t tmp(m_repr);
        m_repr = other.m_repr;
        other.m_repr = tmp;
      } else {
        short_t short_backup(m_repr.short_repr());
        m_repr.short_repr().~short_t();
        ::new(&m_repr.long_repr()) long_t(other.m_repr.long_repr());
        other.m_repr.long_repr().~long_t();
        ::new(&other.m_repr.short_repr()) short_t(short_backup);
      }
    } else {
      if (other.is_short()) {
        short_t short_backup(other.m_repr.short_repr());
        other.m_repr.short_repr().~short_t();
        ::new(&other.m_repr.long_repr()) long_t(m_repr.long_repr());
        m_repr.long_repr().~long_t();
        ::new(&m_repr.short_repr()) short_t(short_backup);
      } else {
        long_t tmp(m_repr.long_repr());
        m_repr.long_repr() = other.m_repr.long_repr();
        other.m_repr.long_repr() = tmp;
      }
    }
  }

  repr_t m_repr;
};

struct foo
{
  __attribute__((noinline))
  foo(string str)
    : m_str{static_cast<string&&>(str)},
      m_len{m_str.length()}
  { }

  string m_str;
  size_t m_len;
};

int main()
{
  foo f{"the quick brown fox jumps over the lazy dog"};
  if (f.m_len == 0) {
    __builtin_abort();
  }
  return 0;
}