Coverage Report

Created: 2026-10-08 07:16

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