Skip to content

Commit 6abc6e7

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

20 files changed

+83
-17
lines changed

libc/src/__support/File/file.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ FileIOResult File::write_unlocked_lbf(const uint8_t *data, size_t len) {
176176
flush_unlocked();
177177

178178
write_result = write_unlocked_fbf(remainder.data(), remainder.size());
179-
written += write_result;;
179+
written += write_result;
180+
;
180181
if (write_result.has_error() || written < len) {
181182
error_code = write_result.has_error() ? write_result.error : EIO;
182183
return {written, error_code};

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::map_internal_to_errno(ret_val.error);
2828
return -1;
2929
}
3030
if (ret_val.value > cpp::numeric_limits<int>::max()) {

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::map_internal_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::map_internal_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::map_internal_to_errno(ret_val.error, stream);
3031
return -1;
3132
}
3233
if (ret_val.value > cpp::numeric_limits<int>::max()) {

libc/src/stdio/generic/vprintf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ LLVM_LIBC_FUNCTION(int, vprintf,
3232
auto ret_val = printf_core::vfprintf_internal(
3333
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
3434
if (ret_val.has_error()) {
35-
libc_errno = ret_val.error;
35+
libc_errno = printf_core::map_internal_to_errno(
36+
ret_val.error, reinterpret_cast<::FILE *>(PRINTF_STDOUT));
3637
return -1;
3738
}
3839
if (ret_val.value > cpp::numeric_limits<int>::max()) {

libc/src/stdio/printf_core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ add_header_library(
4747
libc.include.inttypes
4848
libc.src.__support.CPP.string_view
4949
libc.src.__support.FPUtil.fp_bits
50+
libc.hdr.types.FILE
51+
libc.src.__support.File.file
5052
)
5153

5254
add_header_library(

libc/src/stdio/printf_core/core_structs.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
#include "src/__support/macros/config.h"
1313

14+
#include "hdr/types/FILE.h"
1415
#include "src/__support/CPP/string_view.h"
1516
#include "src/__support/CPP/type_traits.h"
1617
#include "src/__support/FPUtil/FPBits.h"
18+
#include "src/__support/File/file.h"
1719
#include "src/stdio/printf_core/printf_config.h"
1820

1921
#include <inttypes.h>
@@ -144,6 +146,49 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
144146

145147
// This is the value to be returned by conversions when no error has occurred.
146148
constexpr int WRITE_OK = 0;
149+
// These are the printf return values for when an error has occurred. They are
150+
// all negative, and should be distinct.
151+
constexpr int FILE_WRITE_ERROR = -1;
152+
constexpr int FILE_STATUS_ERROR = -2;
153+
constexpr int NULLPTR_WRITE_ERROR = -3;
154+
constexpr int INT_CONVERSION_ERROR = -4;
155+
constexpr int FIXED_POINT_CONVERSION_ERROR = -5;
156+
constexpr int ALLOCATION_ERROR = -6;
157+
158+
// TODO: const file ptr, and mutable for locks?
159+
LIBC_INLINE static int map_internal_to_errno(int internal_errno,
160+
FILE *f = nullptr) {
161+
#if !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
162+
LIBC_NAMESPACE::File *file = reinterpret_cast<LIBC_NAMESPACE::File *>(f);
163+
#else
164+
LIBC_NAMESPACE::File *file = nullptr;
165+
(void)f;
166+
#endif
167+
168+
switch (-internal_errno) {
169+
case -WRITE_OK:
170+
return 0;
171+
case FILE_WRITE_ERROR:
172+
if (file == nullptr)
173+
return EIO;
174+
return file->error() ? file->error() : EIO; // TODO: or unlocked?
175+
case FILE_STATUS_ERROR:
176+
return EIO; // TODO: or what?
177+
case NULLPTR_WRITE_ERROR:
178+
return EINVAL;
179+
case INT_CONVERSION_ERROR:
180+
return ERANGE;
181+
case FIXED_POINT_CONVERSION_ERROR:
182+
return EINVAL;
183+
case ALLOCATION_ERROR:
184+
return ENOMEM;
185+
default:
186+
LIBC_ASSERT(false &&
187+
"Invalid printf error code passed to map_internal_to_errno");
188+
return EINVAL;
189+
}
190+
}
191+
147192
} // namespace printf_core
148193
} // namespace LIBC_NAMESPACE_DECL
149194

0 commit comments

Comments
 (0)