Skip to content

Commit 6d05774

Browse files
committed
Map internall err code to errno
1 parent b03c545 commit 6d05774

23 files changed

+121
-49
lines changed

libc/src/__support/File/file.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
2020

2121
FileIOResult File::write_unlocked(const void *data, size_t len) {
2222
if (!write_allowed()) {
23-
error_code = EBADF;
23+
err_code = EBADF;
2424
return {0, EBADF};
2525
}
2626

@@ -46,15 +46,15 @@ FileIOResult File::write_unlocked_nbf(const uint8_t *data, size_t len) {
4646
pos = 0; // Buffer is now empty so reset pos to the beginning.
4747
// If less bytes were written than expected, then an error occurred.
4848
if (write_result.has_error() || write_result < write_size) {
49-
error_code = write_result.has_error() ? write_result.error : EIO;
49+
err_code = write_result.has_error() ? write_result.error : EIO;
5050
// No bytes from data were written, so return 0.
51-
return {0, error_code};
51+
return {0, err_code};
5252
}
5353
}
5454

5555
FileIOResult write_result = platform_write(this, data, len);
5656
if (write_result.has_error() || write_result < len)
57-
error_code = write_result.has_error() ? write_result.error : EIO;
57+
err_code = write_result.has_error() ? write_result.error : EIO;
5858
return write_result;
5959
}
6060

@@ -106,9 +106,8 @@ FileIOResult File::write_unlocked_fbf(const uint8_t *data, size_t len) {
106106
// If less bytes were written than expected, then an error occurred. Return
107107
// the number of bytes that have been written from |data|.
108108
if (buf_result.has_error() || bytes_written < write_size) {
109-
error_code = buf_result.has_error() ? buf_result.error : EIO;
110-
return {bytes_written <= init_pos ? 0 : bytes_written - init_pos,
111-
error_code};
109+
err_code = buf_result.has_error() ? buf_result.error : EIO;
110+
return {bytes_written <= init_pos ? 0 : bytes_written - init_pos, err_code};
112111
}
113112

114113
// The second piece is handled basically the same as the first, although we
@@ -128,8 +127,8 @@ FileIOResult File::write_unlocked_fbf(const uint8_t *data, size_t len) {
128127
// If less bytes were written than expected, then an error occurred. Return
129128
// the number of bytes that have been written from |data|.
130129
if (result.has_error() || bytes_written < remainder.size()) {
131-
error_code = result.has_error() ? result.error : EIO;
132-
return {primary.size() + bytes_written, error_code};
130+
err_code = result.has_error() ? result.error : EIO;
131+
return {primary.size() + bytes_written, err_code};
133132
}
134133
}
135134

@@ -169,25 +168,26 @@ FileIOResult File::write_unlocked_lbf(const uint8_t *data, size_t len) {
169168
auto write_result = write_unlocked_nbf(primary.data(), primary.size());
170169
written += write_result;
171170
if (write_result.has_error() || written < primary.size()) {
172-
error_code = write_result.has_error() ? write_result.error : EIO;
173-
return {written, error_code};
171+
err_code = write_result.has_error() ? write_result.error : EIO;
172+
return {written, err_code};
174173
}
175174

176175
flush_unlocked();
177176

178177
write_result = write_unlocked_fbf(remainder.data(), remainder.size());
179-
written += write_result;;
178+
written += write_result;
179+
;
180180
if (write_result.has_error() || written < len) {
181-
error_code = write_result.has_error() ? write_result.error : EIO;
182-
return {written, error_code};
181+
err_code = write_result.has_error() ? write_result.error : EIO;
182+
return {written, err_code};
183183
}
184184

185185
return len;
186186
}
187187

188188
FileIOResult File::read_unlocked(void *data, size_t len) {
189189
if (!read_allowed()) {
190-
error_code = EBADF;
190+
err_code = EBADF;
191191
return {0, EBADF};
192192
}
193193

@@ -246,7 +246,7 @@ FileIOResult File::read_unlocked_fbf(uint8_t *data, size_t len) {
246246
if (!result.has_error())
247247
eof = true;
248248
else
249-
error_code = result.error;
249+
err_code = result.error;
250250
return {available_data + fetched_size, result.error};
251251
}
252252
return len;
@@ -264,7 +264,7 @@ FileIOResult File::read_unlocked_fbf(uint8_t *data, size_t len) {
264264
if (!result.has_error())
265265
eof = true;
266266
else
267-
error_code = result.error;
267+
err_code = result.error;
268268
}
269269
return {transfer_size + available_data, result.error};
270270
}
@@ -284,7 +284,7 @@ FileIOResult File::read_unlocked_nbf(uint8_t *data, size_t len) {
284284
if (!result.has_error())
285285
eof = true;
286286
else
287-
error_code = result.error;
287+
err_code = result.error;
288288
}
289289
return {result + available_data, result.error};
290290
}
@@ -323,7 +323,7 @@ int File::ungetc_unlocked(int c) {
323323
}
324324

325325
eof = false; // There is atleast one character that can be read now.
326-
error_code = 0; // This operation was a success.
326+
err_code = 0; // This operation was a success.
327327
return c;
328328
}
329329

@@ -333,8 +333,8 @@ ErrorOr<int> File::seek(off_t offset, int whence) {
333333

334334
FileIOResult buf_result = platform_write(this, buf, pos);
335335
if (buf_result.has_error() || buf_result.value < pos) {
336-
error_code = buf_result.has_error() ? buf_result.error : EIO;
337-
return Error(error_code);
336+
err_code = buf_result.has_error() ? buf_result.error : EIO;
337+
return Error(err_code);
338338
}
339339
} else if (prev_op == FileOp::READ && whence == SEEK_CUR) {
340340
// More data could have been read out from the platform file than was
@@ -371,8 +371,8 @@ int File::flush_unlocked() {
371371
if (prev_op == FileOp::WRITE && pos > 0) {
372372
FileIOResult buf_result = platform_write(this, buf, pos);
373373
if (buf_result.has_error() || buf_result.value < pos) {
374-
error_code = buf_result.has_error() ? buf_result.error : EIO;
375-
return error_code;
374+
err_code = buf_result.has_error() ? buf_result.error : EIO;
375+
return err_code;
376376
}
377377
pos = 0;
378378
}

libc/src/__support/File/file.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class File {
118118
size_t read_limit;
119119

120120
bool eof;
121-
int error_code;
121+
int err_code;
122122

123123
// This is a convenience RAII class to lock and unlock file objects.
124124
class FileLock {
@@ -161,7 +161,7 @@ class File {
161161
/*robust=*/false, /*pshared=*/false),
162162
ungetc_buf(0), buf(buffer), bufsize(buffer_size), bufmode(buffer_mode),
163163
own_buf(owned), mode(modeflags), pos(0), prev_op(FileOp::NONE),
164-
read_limit(0), eof(false), error_code(0) {
164+
read_limit(0), eof(false), err_code(0) {
165165
adjust_buf();
166166
}
167167

@@ -214,8 +214,8 @@ class File {
214214
if (prev_op == FileOp::WRITE && pos > 0) {
215215
auto buf_result = platform_write(this, buf, pos);
216216
if (buf_result.has_error() || buf_result.value < pos) {
217-
error_code = buf_result.has_error() ? buf_result.error : EIO;
218-
return error_code;
217+
err_code = buf_result.has_error() ? buf_result.error : EIO;
218+
return err_code;
219219
}
220220
}
221221
}
@@ -250,14 +250,21 @@ class File {
250250
void lock() { mutex.lock(); }
251251
void unlock() { mutex.unlock(); }
252252

253-
bool error_unlocked() const { return error_code != 0; }
253+
bool error_unlocked() const { return err_code != 0; }
254254

255255
bool error() {
256256
FileLock l(this);
257257
return error_unlocked();
258258
}
259259

260-
void clearerr_unlocked() { error_code = 0; }
260+
int error_code_unlocked() const { return err_code; }
261+
262+
int error_code() {
263+
FileLock l(this);
264+
return error_code_unlocked();
265+
}
266+
267+
void clearerr_unlocked() { err_code = 0; }
261268

262269
void clearerr() {
263270
FileLock l(this);

libc/src/stdio/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ add_entrypoint_object(
125125
DEPENDS
126126
libc.src.stdio.printf_core.printf_main
127127
libc.src.stdio.printf_core.writer
128+
libc.src.stdio.printf_core.core_structs
128129
)
129130

130131
add_entrypoint_object(
@@ -136,6 +137,7 @@ add_entrypoint_object(
136137
DEPENDS
137138
libc.src.stdio.printf_core.printf_main
138139
libc.src.stdio.printf_core.writer
140+
libc.src.stdio.printf_core.core_structs
139141
)
140142

141143
add_entrypoint_object(
@@ -146,6 +148,7 @@ add_entrypoint_object(
146148
asprintf.h
147149
DEPENDS
148150
libc.src.stdio.printf_core.vasprintf_internal
151+
libc.src.stdio.printf_core.core_structs
149152
)
150153

151154
add_entrypoint_object(
@@ -157,6 +160,7 @@ add_entrypoint_object(
157160
DEPENDS
158161
libc.src.stdio.printf_core.printf_main
159162
libc.src.stdio.printf_core.writer
163+
libc.src.stdio.printf_core.core_structs
160164
)
161165

162166
add_entrypoint_object(
@@ -168,6 +172,7 @@ add_entrypoint_object(
168172
DEPENDS
169173
libc.src.stdio.printf_core.printf_main
170174
libc.src.stdio.printf_core.writer
175+
libc.src.stdio.printf_core.core_structs
171176
)
172177

173178
add_entrypoint_object(
@@ -178,6 +183,7 @@ add_entrypoint_object(
178183
vasprintf.h
179184
DEPENDS
180185
libc.src.stdio.printf_core.vasprintf_internal
186+
libc.src.stdio.printf_core.core_structs
181187
)
182188

183189
add_subdirectory(printf_core)

libc/src/stdio/asprintf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ LLVM_LIBC_FUNCTION(int, asprintf,
2424
va_end(vlist);
2525
auto ret_val = printf_core::vasprintf_internal(buffer, format, args);
2626
if (ret_val.has_error()) {
27-
libc_errno = ret_val.error;
27+
libc_errno = printf_core::internal_error_to_errno(ret_val.error);
2828
return -1;
2929
}
3030
if (ret_val.value > cpp::numeric_limits<int>::max()) {

libc/src/stdio/baremetal/printf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
4444

4545
auto retval = printf_core::printf_main(&writer, format, args);
4646
if (retval.has_error()) {
47-
libc_errno = retval.error;
47+
libc_errno = retval.error; // TODO map
4848
return -1;
4949
}
5050

5151
int flushval = wb.overflow_write("");
5252
if (flushval != printf_core::WRITE_OK) {
53-
libc_errno = -flushval;
53+
libc_errno = -flushval; // TODO map
5454
return -1;
5555
}
5656

libc/src/stdio/baremetal/vprintf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ LLVM_LIBC_FUNCTION(int, vprintf,
4242

4343
auto retval = printf_core::printf_main(&writer, format, args);
4444
if (retval.has_error()) {
45-
libc_errno = retval.error;
45+
libc_errno = retval.error; // TODO map
4646
return -1;
4747
}
4848

4949
int flushval = wb.overflow_write("");
5050
if (flushval != printf_core::WRITE_OK) {
51-
libc_errno = -flushval;
51+
libc_errno = -flushval; // TODO map
5252
return -1;
5353
}
5454

libc/src/stdio/generic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ list(APPEND fprintf_deps
394394
libc.hdr.types.FILE
395395
libc.src.__support.arg_list
396396
libc.src.stdio.printf_core.vfprintf_internal
397+
libc.src.stdio.printf_core.core_structs
397398
)
398399

399400
if(LLVM_LIBC_FULL_BUILD)

libc/src/stdio/generic/fprintf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LLVM_LIBC_FUNCTION(int, fprintf,
2929
va_end(vlist);
3030
auto ret_val = printf_core::vfprintf_internal(stream, format, args);
3131
if (ret_val.has_error()) {
32-
libc_errno = ret_val.error;
32+
libc_errno = printf_core::internal_error_to_errno(ret_val.error, stream);
3333
return -1;
3434
}
3535
if (ret_val.value > cpp::numeric_limits<int>::max()) {

libc/src/stdio/generic/printf.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/File/file.h"
1212
#include "src/__support/arg_list.h"
1313
#include "src/__support/macros/config.h"
14+
#include "src/stdio/printf_core/core_structs.h"
1415
#include "src/stdio/printf_core/vfprintf_internal.h"
1516

1617
#include "hdr/types/FILE.h"
@@ -34,7 +35,10 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
3435
auto ret_val = printf_core::vfprintf_internal(
3536
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
3637
if (ret_val.has_error()) {
37-
libc_errno = ret_val.error;
38+
libc_errno = printf_core::internal_error_to_errno(
39+
ret_val.error, reinterpret_cast<::FILE *>(PRINTF_STDOUT));
40+
return -1;
41+
;
3842
return -1;
3943
}
4044
if (ret_val.value > cpp::numeric_limits<int>::max()) {

libc/src/stdio/generic/vfprintf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/File/file.h"
1212
#include "src/__support/arg_list.h"
1313
#include "src/__support/macros/config.h"
14+
#include "src/stdio/printf_core/core_structs.h"
1415
#include "src/stdio/printf_core/vfprintf_internal.h"
1516

1617
#include "hdr/types/FILE.h"
@@ -26,7 +27,7 @@ LLVM_LIBC_FUNCTION(int, vfprintf,
2627
// destruction automatically.
2728
auto ret_val = printf_core::vfprintf_internal(stream, format, args);
2829
if (ret_val.has_error()) {
29-
libc_errno = ret_val.error;
30+
libc_errno = printf_core::internal_error_to_errno(ret_val.error, stream);
3031
return -1;
3132
}
3233
if (ret_val.value > cpp::numeric_limits<int>::max()) {

0 commit comments

Comments
 (0)