Coverage Report

Created: 2020-02-25 14:32

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