Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/ios.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-------------------------- ios.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 "__config"
10
11
#include "ios"
12
13
#include <stdlib.h>
14
15
#include "__locale"
16
#include "algorithm"
17
#include "include/config_elast.h"
18
#include "limits"
19
#include "memory"
20
#include "new"
21
#include "string"
22
#include "__undef_macros"
23
24
_LIBCPP_BEGIN_NAMESPACE_STD
25
26
class _LIBCPP_HIDDEN __iostream_category
27
    : public __do_message
28
{
29
public:
30
    virtual const char* name() const noexcept;
31
    virtual string message(int ev) const;
32
};
33
34
const char*
35
__iostream_category::name() const noexcept
36
0
{
37
0
    return "iostream";
38
0
}
39
40
string
41
__iostream_category::message(int ev) const
42
0
{
43
0
    if (ev != static_cast<int>(io_errc::stream)
44
0
#ifdef _LIBCPP_ELAST
45
0
        && ev <= _LIBCPP_ELAST
46
0
#endif // _LIBCPP_ELAST
47
0
        )
48
0
        return __do_message::message(ev);
49
0
    return string("unspecified iostream_category error");
50
0
}
51
52
const error_category&
53
iostream_category() noexcept
54
0
{
55
0
    static __iostream_category s;
56
0
    return s;
57
0
}
58
59
// ios_base::failure
60
61
ios_base::failure::failure(const string& msg, const error_code& ec)
62
    : system_error(ec, msg)
63
0
{
64
0
}
65
66
ios_base::failure::failure(const char* msg, const error_code& ec)
67
    : system_error(ec, msg)
68
0
{
69
0
}
70
71
ios_base::failure::~failure() throw()
72
0
{
73
0
}
74
75
// ios_base locale
76
77
const ios_base::fmtflags ios_base::boolalpha;
78
const ios_base::fmtflags ios_base::dec;
79
const ios_base::fmtflags ios_base::fixed;
80
const ios_base::fmtflags ios_base::hex;
81
const ios_base::fmtflags ios_base::internal;
82
const ios_base::fmtflags ios_base::left;
83
const ios_base::fmtflags ios_base::oct;
84
const ios_base::fmtflags ios_base::right;
85
const ios_base::fmtflags ios_base::scientific;
86
const ios_base::fmtflags ios_base::showbase;
87
const ios_base::fmtflags ios_base::showpoint;
88
const ios_base::fmtflags ios_base::showpos;
89
const ios_base::fmtflags ios_base::skipws;
90
const ios_base::fmtflags ios_base::unitbuf;
91
const ios_base::fmtflags ios_base::uppercase;
92
const ios_base::fmtflags ios_base::adjustfield;
93
const ios_base::fmtflags ios_base::basefield;
94
const ios_base::fmtflags ios_base::floatfield;
95
96
const ios_base::iostate ios_base::badbit;
97
const ios_base::iostate ios_base::eofbit;
98
const ios_base::iostate ios_base::failbit;
99
const ios_base::iostate ios_base::goodbit;
100
101
const ios_base::openmode ios_base::app;
102
const ios_base::openmode ios_base::ate;
103
const ios_base::openmode ios_base::binary;
104
const ios_base::openmode ios_base::in;
105
const ios_base::openmode ios_base::out;
106
const ios_base::openmode ios_base::trunc;
107
108
void
109
ios_base::__call_callbacks(event ev)
110
152M
{
111
152M
    for (size_t i = __event_size_; i;)
112
0
    {
113
0
        --i;
114
0
        __fn_[i](ev, *this, __index_[i]);
115
0
    }
116
152M
}
117
118
// locale
119
120
locale
121
ios_base::imbue(const locale& newloc)
122
0
{
123
0
    static_assert(sizeof(locale) == sizeof(__loc_), "");
124
0
    locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
125
0
    locale oldloc = loc_storage;
126
0
    loc_storage = newloc;
127
0
    __call_callbacks(imbue_event);
128
0
    return oldloc;
129
0
}
130
131
locale
132
ios_base::getloc() const
133
57.1M
{
134
57.1M
    const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
135
57.1M
    return loc_storage;
136
57.1M
}
137
138
// xalloc
139
#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
140
atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
141
#else
142
int ios_base::__xindex_ = 0;
143
#endif
144
145
template <typename _Tp>
146
static size_t __ios_new_cap(size_t __req_size, size_t __current_cap)
147
0
{ // Precondition: __req_size > __current_cap
148
0
  const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);
149
0
  if (__req_size < mx/2)
150
0
    return _VSTD::max(2 * __current_cap, __req_size);
151
0
  else
152
0
    return mx;
153
0
}
Unexecuted instantiation: ios.cpp:unsigned long std::__1::__ios_new_cap<long>(unsigned long, unsigned long)
Unexecuted instantiation: ios.cpp:unsigned long std::__1::__ios_new_cap<void*>(unsigned long, unsigned long)
Unexecuted instantiation: ios.cpp:unsigned long std::__1::__ios_new_cap<void (*)(std::__1::ios_base::event, std::__1::ios_base&, int)>(unsigned long, unsigned long)
154
155
int
156
ios_base::xalloc()
157
0
{
158
0
    return __xindex_++;
159
0
}
160
161
long&
162
ios_base::iword(int index)
163
0
{
164
0
    size_t req_size = static_cast<size_t>(index)+1;
165
0
    if (req_size > __iarray_cap_)
166
0
    {
167
0
        size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);
168
0
        long* iarray = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));
169
0
        if (iarray == 0)
170
0
        {
171
0
            setstate(badbit);
172
0
            static long error;
173
0
            error = 0;
174
0
            return error;
175
0
        }
176
0
        __iarray_ = iarray;
177
0
        for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)
178
0
            *p = 0;
179
0
        __iarray_cap_ = newcap;
180
0
    }
181
0
    __iarray_size_ = max<size_t>(__iarray_size_, req_size);
182
0
    return __iarray_[index];
183
0
}
184
185
void*&
186
ios_base::pword(int index)
187
0
{
188
0
    size_t req_size = static_cast<size_t>(index)+1;
189
0
    if (req_size > __parray_cap_)
190
0
    {
191
0
        size_t newcap = __ios_new_cap<void *>(req_size, __iarray_cap_);
192
0
        void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void *)));
193
0
        if (parray == 0)
194
0
        {
195
0
            setstate(badbit);
196
0
            static void* error;
197
0
            error = 0;
198
0
            return error;
199
0
        }
200
0
        __parray_ = parray;
201
0
        for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)
202
0
            *p = 0;
203
0
        __parray_cap_ = newcap;
204
0
    }
205
0
    __parray_size_ = max<size_t>(__parray_size_, req_size);
206
0
    return __parray_[index];
207
0
}
208
209
// register_callback
210
211
void
212
ios_base::register_callback(event_callback fn, int index)
213
0
{
214
0
    size_t req_size = __event_size_ + 1;
215
0
    if (req_size > __event_cap_)
216
0
    {
217
0
        size_t newcap = __ios_new_cap<event_callback>(req_size, __event_cap_);
218
0
        event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));
219
0
        if (fns == 0)
220
0
            setstate(badbit);
221
0
        __fn_ = fns;
222
0
        int* indxs = static_cast<int *>(realloc(__index_, newcap * sizeof(int)));
223
0
        if (indxs == 0)
224
0
            setstate(badbit);
225
0
        __index_ = indxs;
226
0
        __event_cap_ = newcap;
227
0
    }
228
0
    __fn_[__event_size_] = fn;
229
0
    __index_[__event_size_] = index;
230
0
    ++__event_size_;
231
0
}
232
233
ios_base::~ios_base()
234
152M
{
235
152M
    __call_callbacks(erase_event);
236
152M
    locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
237
152M
    loc_storage.~locale();
238
152M
    free(__fn_);
239
152M
    free(__index_);
240
152M
    free(__iarray_);
241
152M
    free(__parray_);
242
152M
}
243
244
// iostate
245
246
void
247
ios_base::clear(iostate state)
248
18
{
249
18
    if (__rdbuf_)
250
18
        __rdstate_ = state;
251
0
    else
252
0
        __rdstate_ = state | badbit;
253
254
18
    if (((state | (__rdbuf_ ? goodbit : 
badbit0
)) & __exceptions_) != 0)
255
0
        __throw_failure("ios_base::clear");
256
18
}
257
258
// init
259
260
void
261
ios_base::init(void* sb)
262
152M
{
263
152M
    __rdbuf_ = sb;
264
152M
    __rdstate_ = __rdbuf_ ? goodbit : 
badbit0
;
265
152M
    __exceptions_ = goodbit;
266
152M
    __fmtflags_ = skipws | dec;
267
152M
    __width_ = 0;
268
152M
    __precision_ = 6;
269
152M
    __fn_ = 0;
270
152M
    __index_ = 0;
271
152M
    __event_size_ = 0;
272
152M
    __event_cap_ = 0;
273
152M
    __iarray_ = 0;
274
152M
    __iarray_size_ = 0;
275
152M
    __iarray_cap_ = 0;
276
152M
    __parray_ = 0;
277
152M
    __parray_size_ = 0;
278
152M
    __parray_cap_ = 0;
279
152M
    ::new(&__loc_) locale;
280
152M
}
281
282
void
283
ios_base::copyfmt(const ios_base& rhs)
284
0
{
285
    // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
286
    // Don't alter *this until all needed resources are acquired
287
0
    unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
288
0
    unique_ptr<int, void (*)(void*)> new_ints(0, free);
289
0
    unique_ptr<long, void (*)(void*)> new_longs(0, free);
290
0
    unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
291
0
    if (__event_cap_ < rhs.__event_size_)
292
0
    {
293
0
        size_t newesize = sizeof(event_callback) * rhs.__event_size_;
294
0
        new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
295
0
        if (!new_callbacks)
296
0
            __throw_bad_alloc();
297
298
0
        size_t newisize = sizeof(int) * rhs.__event_size_;
299
0
        new_ints.reset(static_cast<int *>(malloc(newisize)));
300
0
        if (!new_ints)
301
0
            __throw_bad_alloc();
302
0
    }
303
0
    if (__iarray_cap_ < rhs.__iarray_size_)
304
0
    {
305
0
        size_t newsize = sizeof(long) * rhs.__iarray_size_;
306
0
        new_longs.reset(static_cast<long*>(malloc(newsize)));
307
0
        if (!new_longs)
308
0
            __throw_bad_alloc();
309
0
    }
310
0
    if (__parray_cap_ < rhs.__parray_size_)
311
0
    {
312
0
        size_t newsize = sizeof(void*) * rhs.__parray_size_;
313
0
        new_pointers.reset(static_cast<void**>(malloc(newsize)));
314
0
        if (!new_pointers)
315
0
            __throw_bad_alloc();
316
0
    }
317
    // Got everything we need.  Copy everything but __rdstate_, __rdbuf_ and __exceptions_
318
0
    __fmtflags_ = rhs.__fmtflags_;
319
0
    __precision_ = rhs.__precision_;
320
0
    __width_ = rhs.__width_;
321
0
    locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
322
0
    const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
323
0
    lhs_loc = rhs_loc;
324
0
    if (__event_cap_ < rhs.__event_size_)
325
0
    {
326
0
        free(__fn_);
327
0
        __fn_ = new_callbacks.release();
328
0
        free(__index_);
329
0
        __index_ = new_ints.release();
330
0
        __event_cap_ = rhs.__event_size_;
331
0
    }
332
0
    for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_)
333
0
    {
334
0
        __fn_[__event_size_] = rhs.__fn_[__event_size_];
335
0
        __index_[__event_size_] = rhs.__index_[__event_size_];
336
0
    }
337
0
    if (__iarray_cap_ < rhs.__iarray_size_)
338
0
    {
339
0
        free(__iarray_);
340
0
        __iarray_ = new_longs.release();
341
0
        __iarray_cap_ = rhs.__iarray_size_;
342
0
    }
343
0
    for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
344
0
        __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
345
0
    if (__parray_cap_ < rhs.__parray_size_)
346
0
    {
347
0
        free(__parray_);
348
0
        __parray_ = new_pointers.release();
349
0
        __parray_cap_ = rhs.__parray_size_;
350
0
    }
351
0
    for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
352
0
        __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
353
0
}
354
355
void
356
ios_base::move(ios_base& rhs)
357
0
{
358
    // *this is uninitialized
359
0
    __fmtflags_ = rhs.__fmtflags_;
360
0
    __precision_ = rhs.__precision_;
361
0
    __width_ = rhs.__width_;
362
0
    __rdstate_ = rhs.__rdstate_;
363
0
    __exceptions_ = rhs.__exceptions_;
364
0
    __rdbuf_ = 0;
365
0
    locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
366
0
    ::new(&__loc_) locale(rhs_loc);
367
0
    __fn_ = rhs.__fn_;
368
0
    rhs.__fn_ = 0;
369
0
    __index_ = rhs.__index_;
370
0
    rhs.__index_ = 0;
371
0
    __event_size_ = rhs.__event_size_;
372
0
    rhs.__event_size_ = 0;
373
0
    __event_cap_ = rhs.__event_cap_;
374
0
    rhs.__event_cap_ = 0;
375
0
    __iarray_ = rhs.__iarray_;
376
0
    rhs.__iarray_ = 0;
377
0
    __iarray_size_ = rhs.__iarray_size_;
378
0
    rhs.__iarray_size_ = 0;
379
0
    __iarray_cap_ = rhs.__iarray_cap_;
380
0
    rhs.__iarray_cap_ = 0;
381
0
    __parray_ = rhs.__parray_;
382
0
    rhs.__parray_ = 0;
383
0
    __parray_size_ = rhs.__parray_size_;
384
0
    rhs.__parray_size_ = 0;
385
0
    __parray_cap_ = rhs.__parray_cap_;
386
0
    rhs.__parray_cap_ = 0;
387
0
}
388
389
void
390
ios_base::swap(ios_base& rhs) noexcept
391
0
{
392
0
    _VSTD::swap(__fmtflags_, rhs.__fmtflags_);
393
0
    _VSTD::swap(__precision_, rhs.__precision_);
394
0
    _VSTD::swap(__width_, rhs.__width_);
395
0
    _VSTD::swap(__rdstate_, rhs.__rdstate_);
396
0
    _VSTD::swap(__exceptions_, rhs.__exceptions_);
397
0
    locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
398
0
    locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
399
0
    _VSTD::swap(lhs_loc, rhs_loc);
400
0
    _VSTD::swap(__fn_, rhs.__fn_);
401
0
    _VSTD::swap(__index_, rhs.__index_);
402
0
    _VSTD::swap(__event_size_, rhs.__event_size_);
403
0
    _VSTD::swap(__event_cap_, rhs.__event_cap_);
404
0
    _VSTD::swap(__iarray_, rhs.__iarray_);
405
0
    _VSTD::swap(__iarray_size_, rhs.__iarray_size_);
406
0
    _VSTD::swap(__iarray_cap_, rhs.__iarray_cap_);
407
0
    _VSTD::swap(__parray_, rhs.__parray_);
408
0
    _VSTD::swap(__parray_size_, rhs.__parray_size_);
409
0
    _VSTD::swap(__parray_cap_, rhs.__parray_cap_);
410
0
}
411
412
void
413
ios_base::__set_badbit_and_consider_rethrow()
414
0
{
415
0
    __rdstate_ |= badbit;
416
0
#ifndef _LIBCPP_NO_EXCEPTIONS
417
0
    if (__exceptions_ & badbit)
418
0
        throw;
419
0
#endif // _LIBCPP_NO_EXCEPTIONS
420
0
}
421
422
void
423
ios_base::__set_failbit_and_consider_rethrow()
424
0
{
425
0
    __rdstate_ |= failbit;
426
0
#ifndef _LIBCPP_NO_EXCEPTIONS
427
0
    if (__exceptions_ & failbit)
428
0
        throw;
429
0
#endif // _LIBCPP_NO_EXCEPTIONS
430
0
}
431
432
bool
433
ios_base::sync_with_stdio(bool sync)
434
0
{
435
0
    static bool previous_state = true;
436
0
    bool r = previous_state;
437
0
    previous_state = sync;
438
0
    return r;
439
0
}
440
441
_LIBCPP_END_NAMESPACE_STD