summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/opt/pr6713.C
blob: d89fef81b2c97342943217f88c9a95a7139ef04b (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
// PR optimization/6713
// This testcase segfaulted on x86 because a dangling REG_EQUAL note
// resulted in incorrect substitutions later.
// { dg-do run }
// { dg-options "-O2" }

template<typename _CharT> class basic_iterator
{
  public:
    basic_iterator(_CharT* _p) : _M_current(_p) {}
    basic_iterator& operator++() { ++_M_current; return *this; }
    _CharT& operator*() const { return *_M_current; }
    bool operator!=(basic_iterator &_rhs) { return _M_current != _rhs._M_current; }

  private:
    _CharT* _M_current;
};

template<typename _CharT> class basic_string
{
  public:
    typedef unsigned int size_type;
    typedef basic_iterator<_CharT> iterator;

  private:
    struct _Rep
    {
      size_type _M_length;
      size_type _M_capacity;
      int _M_references;

      bool _M_is_leaked() const { return _M_references < 0; }
      bool _M_is_shared() const { return _M_references > 0; }
      void _M_set_leaked() { _M_references = -1; }
      void _M_set_sharable() { _M_references = 0; }
    };

    struct _Rep _M_rep;

    struct _Alloc_hider
    {
      _CharT _raw[16];
      _CharT* _M_p;
    };

    mutable _Alloc_hider _M_dataplus;

    _CharT* _M_data() const { return _M_dataplus._M_p; }

    void _M_leak() { if (!_M_rep._M_is_leaked()) _M_leak_hard(); }

    static int count;

    static void _M_leak_hard();

  public:
    explicit basic_string(const _CharT* __s);

    iterator begin() { _M_leak(); return iterator(_M_data()); }

    iterator end() { _M_leak(); return iterator(_M_data() + this->size()); }

    size_type size() const { return _M_rep._M_length; }
};

template<typename _CharT> basic_string<_CharT>::
basic_string(const _CharT* __s)
{
  int i;

  for (i=0; i<15; i++) {
    if (!__s[i])
      break;

    _M_dataplus._raw[i] = __s[i];
  }

  _M_dataplus._raw[i] = 0;
  _M_dataplus._M_p = _M_dataplus._raw;

  _M_rep._M_length = i;
  _M_rep._M_capacity = i;
  _M_rep._M_references = 1;
}     

template<typename _CharT> int basic_string<_CharT>::count = 0;

template<typename _CharT> void basic_string<_CharT>::
_M_leak_hard()
{
  count++;
}

typedef basic_string<char> string;

template int basic_string<char>::count;

int isspa(int ch)
{
  return 0;
}

void foo(string& str)
{
  string::iterator it = str.begin();
  string::iterator stop = str.end();

  for (; it != stop; ++it)
    if (isspa(*it))
      break;
}

int main()
{
  string str("test");
  foo(str);
}