Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/strstream.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------------------------ strstream.cpp -------------------------------===//
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-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "strstream"
10
#include "algorithm"
11
#include "climits"
12
#include "cstring"
13
#include "cstdlib"
14
#include "__debug"
15
#include "__undef_macros"
16
17
_LIBCPP_BEGIN_NAMESPACE_STD
18
19
strstreambuf::strstreambuf(streamsize __alsize)
20
    : __strmode_(__dynamic),
21
      __alsize_(__alsize),
22
      __palloc_(nullptr),
23
      __pfree_(nullptr)
24
0
{
25
0
}
26
27
strstreambuf::strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*))
28
    : __strmode_(__dynamic),
29
      __alsize_(__default_alsize),
30
      __palloc_(__palloc),
31
      __pfree_(__pfree)
32
0
{
33
0
}
34
35
void
36
strstreambuf::__init(char* __gnext, streamsize __n, char* __pbeg)
37
0
{
38
0
    if (__n == 0)
39
0
        __n = static_cast<streamsize>(strlen(__gnext));
40
0
    else if (__n < 0)
41
0
        __n = INT_MAX;
42
0
    if (__pbeg == nullptr)
43
0
        setg(__gnext, __gnext, __gnext + __n);
44
0
    else
45
0
    {
46
0
        setg(__gnext, __gnext, __pbeg);
47
0
        setp(__pbeg, __pbeg + __n);
48
0
    }
49
0
}
50
51
strstreambuf::strstreambuf(char* __gnext, streamsize __n, char* __pbeg)
52
    : __strmode_(),
53
      __alsize_(__default_alsize),
54
      __palloc_(nullptr),
55
      __pfree_(nullptr)
56
0
{
57
0
    __init(__gnext, __n, __pbeg);
58
0
}
59
60
strstreambuf::strstreambuf(const char* __gnext, streamsize __n)
61
    : __strmode_(__constant),
62
      __alsize_(__default_alsize),
63
      __palloc_(nullptr),
64
      __pfree_(nullptr)
65
0
{
66
0
    __init(const_cast<char *>(__gnext), __n, nullptr);
67
0
}
68
69
strstreambuf::strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg)
70
    : __strmode_(),
71
      __alsize_(__default_alsize),
72
      __palloc_(nullptr),
73
      __pfree_(nullptr)
74
0
{
75
0
    __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, reinterpret_cast<char*>(__pbeg));
76
0
}
77
78
strstreambuf::strstreambuf(const signed char* __gnext, streamsize __n)
79
    : __strmode_(__constant),
80
      __alsize_(__default_alsize),
81
      __palloc_(nullptr),
82
      __pfree_(nullptr)
83
0
{
84
0
    __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, nullptr);
85
0
}
86
87
strstreambuf::strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg)
88
    : __strmode_(),
89
      __alsize_(__default_alsize),
90
      __palloc_(nullptr),
91
      __pfree_(nullptr)
92
0
{
93
0
    __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, reinterpret_cast<char*>(__pbeg));
94
0
}
95
96
strstreambuf::strstreambuf(const unsigned char* __gnext, streamsize __n)
97
    : __strmode_(__constant),
98
      __alsize_(__default_alsize),
99
      __palloc_(nullptr),
100
      __pfree_(nullptr)
101
0
{
102
0
    __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, nullptr);
103
0
}
104
105
strstreambuf::~strstreambuf()
106
0
{
107
0
    if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0)
108
0
    {
109
0
        if (__pfree_)
110
0
            __pfree_(eback());
111
0
        else
112
0
            delete [] eback();
113
0
    }
114
0
}
115
116
void
117
strstreambuf::swap(strstreambuf& __rhs)
118
0
{
119
0
    streambuf::swap(__rhs);
120
0
    _VSTD::swap(__strmode_, __rhs.__strmode_);
121
0
    _VSTD::swap(__alsize_, __rhs.__alsize_);
122
0
    _VSTD::swap(__palloc_, __rhs.__palloc_);
123
0
    _VSTD::swap(__pfree_, __rhs.__pfree_);
124
0
}
125
126
void
127
strstreambuf::freeze(bool __freezefl)
128
0
{
129
0
    if (__strmode_ & __dynamic)
130
0
    {
131
0
        if (__freezefl)
132
0
            __strmode_ |= __frozen;
133
0
        else
134
0
            __strmode_ &= ~__frozen;
135
0
    }
136
0
}
137
138
char*
139
strstreambuf::str()
140
0
{
141
0
    if (__strmode_ & __dynamic)
142
0
        __strmode_ |= __frozen;
143
0
    return eback();
144
0
}
145
146
int
147
strstreambuf::pcount() const
148
0
{
149
0
    return static_cast<int>(pptr() - pbase());
150
0
}
151
152
strstreambuf::int_type
153
strstreambuf::overflow(int_type __c)
154
0
{
155
0
    if (__c == EOF)
156
0
        return int_type(0);
157
0
    if (pptr() == epptr())
158
0
    {
159
0
        if ((__strmode_ & __dynamic) == 0 || (__strmode_ & __frozen) != 0)
160
0
            return int_type(EOF);
161
0
        size_t old_size = static_cast<size_t> ((epptr() ? epptr() : egptr()) - eback());
162
0
        size_t new_size = max<size_t>(static_cast<size_t>(__alsize_), 2*old_size);
163
0
        if (new_size == 0)
164
0
            new_size = __default_alsize;
165
0
        char* buf = nullptr;
166
0
        if (__palloc_)
167
0
            buf = static_cast<char*>(__palloc_(new_size));
168
0
        else
169
0
            buf = new char[new_size];
170
0
        if (buf == nullptr)
171
0
            return int_type(EOF);
172
0
        if (old_size != 0) {
173
0
            _LIBCPP_ASSERT(eback(), "overflow copying from NULL");
174
0
            memcpy(buf, eback(), static_cast<size_t>(old_size));
175
0
        }
176
0
        ptrdiff_t ninp = gptr()  - eback();
177
0
        ptrdiff_t einp = egptr() - eback();
178
0
        ptrdiff_t nout = pptr()  - pbase();
179
0
        if (__strmode_ & __allocated)
180
0
        {
181
0
            if (__pfree_)
182
0
                __pfree_(eback());
183
0
            else
184
0
                delete [] eback();
185
0
        }
186
0
        setg(buf, buf + ninp, buf + einp);
187
0
        setp(buf + einp, buf + new_size);
188
0
        __pbump(nout);
189
0
        __strmode_ |= __allocated;
190
0
    }
191
0
    *pptr() = static_cast<char>(__c);
192
0
    pbump(1);
193
0
    return int_type(static_cast<unsigned char>(__c));
194
0
}
195
196
strstreambuf::int_type
197
strstreambuf::pbackfail(int_type __c)
198
0
{
199
0
    if (eback() == gptr())
200
0
        return EOF;
201
0
    if (__c == EOF)
202
0
    {
203
0
        gbump(-1);
204
0
        return int_type(0);
205
0
    }
206
0
    if (__strmode_ & __constant)
207
0
    {
208
0
        if (gptr()[-1] == static_cast<char>(__c))
209
0
        {
210
0
            gbump(-1);
211
0
            return __c;
212
0
        }
213
0
        return EOF;
214
0
    }
215
0
    gbump(-1);
216
0
    *gptr() = static_cast<char>(__c);
217
0
    return __c;
218
0
}
219
220
strstreambuf::int_type
221
strstreambuf::underflow()
222
0
{
223
0
    if (gptr() == egptr())
224
0
    {
225
0
        if (egptr() >= pptr())
226
0
            return EOF;
227
0
        setg(eback(), gptr(), pptr());
228
0
    }
229
0
    return int_type(static_cast<unsigned char>(*gptr()));
230
0
}
231
232
strstreambuf::pos_type
233
strstreambuf::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which)
234
0
{
235
0
    off_type __p(-1);
236
0
    bool pos_in = (__which & ios::in) != 0;
237
0
    bool pos_out = (__which & ios::out) != 0;
238
0
    bool legal = false;
239
0
    switch (__way)
240
0
    {
241
0
    case ios::beg:
242
0
    case ios::end:
243
0
        if (pos_in || pos_out)
244
0
            legal = true;
245
0
        break;
246
0
    case ios::cur:
247
0
        if (pos_in != pos_out)
248
0
            legal = true;
249
0
        break;
250
0
    }
251
0
    if (pos_in && gptr() == nullptr)
252
0
        legal = false;
253
0
    if (pos_out && pptr() == nullptr)
254
0
        legal = false;
255
0
    if (legal)
256
0
    {
257
0
        off_type newoff;
258
0
        char* seekhigh = epptr() ? epptr() : egptr();
259
0
        switch (__way)
260
0
        {
261
0
        case ios::beg:
262
0
            newoff = 0;
263
0
            break;
264
0
        case ios::cur:
265
0
            newoff = (pos_in ? gptr() : pptr()) - eback();
266
0
            break;
267
0
        case ios::end:
268
0
            newoff = seekhigh - eback();
269
0
            break;
270
0
        default:
271
0
            _LIBCPP_UNREACHABLE();
272
0
        }
273
0
        newoff += __off;
274
0
        if (0 <= newoff && newoff <= seekhigh - eback())
275
0
        {
276
0
            char* newpos = eback() + newoff;
277
0
            if (pos_in)
278
0
                setg(eback(), newpos, _VSTD::max(newpos, egptr()));
279
0
            if (pos_out)
280
0
            {
281
0
                // min(pbase, newpos), newpos, epptr()
282
0
                __off = epptr() - newpos;
283
0
                setp(min(pbase(), newpos), epptr());
284
0
                __pbump((epptr() - pbase()) - __off);
285
0
            }
286
0
            __p = newoff;
287
0
        }
288
0
    }
289
0
    return pos_type(__p);
290
0
}
291
292
strstreambuf::pos_type
293
strstreambuf::seekpos(pos_type __sp, ios_base::openmode __which)
294
0
{
295
0
    off_type __p(-1);
296
0
    bool pos_in = (__which & ios::in) != 0;
297
0
    bool pos_out = (__which & ios::out) != 0;
298
0
    if (pos_in || pos_out)
299
0
    {
300
0
        if (!((pos_in && gptr() == nullptr) || (pos_out && pptr() == nullptr)))
301
0
        {
302
0
            off_type newoff = __sp;
303
0
            char* seekhigh = epptr() ? epptr() : egptr();
304
0
            if (0 <= newoff && newoff <= seekhigh - eback())
305
0
            {
306
0
                char* newpos = eback() + newoff;
307
0
                if (pos_in)
308
0
                    setg(eback(), newpos, _VSTD::max(newpos, egptr()));
309
0
                if (pos_out)
310
0
                {
311
0
                    // min(pbase, newpos), newpos, epptr()
312
0
                    off_type temp = epptr() - newpos;
313
0
                    setp(min(pbase(), newpos), epptr());
314
0
                    __pbump((epptr() - pbase()) - temp);
315
0
                }
316
0
                __p = newoff;
317
0
            }
318
0
        }
319
0
    }
320
0
    return pos_type(__p);
321
0
}
322
323
istrstream::~istrstream()
324
0
{
325
0
}
326
327
ostrstream::~ostrstream()
328
0
{
329
0
}
330
331
strstream::~strstream()
332
0
{
333
0
}
334
335
_LIBCPP_END_NAMESPACE_STD