Skip to content

Commit 69e9362

Browse files
committed
Use ErroOr for print retval instead of custom struct
1 parent d194975 commit 69e9362

File tree

17 files changed

+71
-78
lines changed

17 files changed

+71
-78
lines changed

libc/src/stdio/asprintf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ LLVM_LIBC_FUNCTION(int, asprintf,
2323
// destruction automatically.
2424
va_end(vlist);
2525
auto ret_val = printf_core::vasprintf_internal(buffer, format, args);
26-
if (ret_val.has_error()) {
27-
libc_errno = printf_core::internal_error_to_errno(ret_val.error);
26+
if (!ret_val.has_value()) {
27+
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
2828
return -1;
2929
}
30-
if (ret_val.value > cpp::numeric_limits<int>::max()) {
30+
if (ret_val.value() > cpp::numeric_limits<int>::max()) {
3131
libc_errno = EOVERFLOW;
3232
return -1;
3333
}
3434

35-
return static_cast<int>(ret_val.value);
35+
return static_cast<int>(ret_val.value());
3636
}
3737

3838
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/baremetal/printf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
4343
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
4444

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

@@ -54,12 +54,12 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
5454
return -1;
5555
}
5656

57-
if (retval.value > cpp::numeric_limits<int>::max()) {
57+
if (retval.value() > cpp::numeric_limits<int>::max()) {
5858
libc_errno = EOVERFLOW;
5959
return -1;
6060
}
6161

62-
return static_cast<int>(retval.value);
62+
return static_cast<int>(retval.value());
6363
}
6464

6565
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/baremetal/vprintf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ LLVM_LIBC_FUNCTION(int, vprintf,
4141
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
4242

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

@@ -52,12 +52,12 @@ LLVM_LIBC_FUNCTION(int, vprintf,
5252
return -1;
5353
}
5454

55-
if (retval.value > cpp::numeric_limits<int>::max()) {
55+
if (retval.value() > cpp::numeric_limits<int>::max()) {
5656
libc_errno = EOVERFLOW;
5757
return -1;
5858
}
5959

60-
return static_cast<int>(retval.value);
60+
return static_cast<int>(retval.value());
6161
}
6262

6363
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/generic/fprintf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ LLVM_LIBC_FUNCTION(int, fprintf,
2828
// destruction automatically.
2929
va_end(vlist);
3030
auto ret_val = printf_core::vfprintf_internal(stream, format, args);
31-
if (ret_val.has_error()) {
32-
libc_errno = printf_core::internal_error_to_errno(ret_val.error);
31+
if (!ret_val.has_value()) {
32+
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
3333
return -1;
3434
}
35-
if (ret_val.value > cpp::numeric_limits<int>::max()) {
35+
if (ret_val.value() > cpp::numeric_limits<int>::max()) {
3636
libc_errno = EOVERFLOW;
3737
return -1;
3838
}
3939

40-
return static_cast<int>(ret_val.value);
40+
return static_cast<int>(ret_val.value());
4141
}
4242

4343
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/generic/printf.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,16 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
3434
va_end(vlist);
3535
auto ret_val = printf_core::vfprintf_internal(
3636
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
37-
if (ret_val.has_error()) {
38-
libc_errno = printf_core::internal_error_to_errno(ret_val.error);
39-
return -1;
40-
;
37+
if (!ret_val.has_value()) {
38+
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
4139
return -1;
4240
}
43-
if (ret_val.value > cpp::numeric_limits<int>::max()) {
41+
if (ret_val.value() > cpp::numeric_limits<int>::max()) {
4442
libc_errno = EOVERFLOW;
4543
return -1;
4644
}
4745

48-
return static_cast<int>(ret_val.value);
46+
return static_cast<int>(ret_val.value());
4947
}
5048

5149
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/generic/vfprintf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ LLVM_LIBC_FUNCTION(int, vfprintf,
2626
// and pointer semantics, as well as handling
2727
// destruction automatically.
2828
auto ret_val = printf_core::vfprintf_internal(stream, format, args);
29-
if (ret_val.has_error()) {
30-
libc_errno = printf_core::internal_error_to_errno(ret_val.error);
29+
if (!ret_val.has_value()) {
30+
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
3131
return -1;
3232
}
33-
if (ret_val.value > cpp::numeric_limits<int>::max()) {
33+
if (ret_val.value() > cpp::numeric_limits<int>::max()) {
3434
libc_errno = EOVERFLOW;
3535
return -1;
3636
}
3737

38-
return static_cast<int>(ret_val.value);
38+
return static_cast<int>(ret_val.value());
3939
}
4040

4141
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/generic/vprintf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ LLVM_LIBC_FUNCTION(int, vprintf,
3131
// destruction automatically.
3232
auto ret_val = printf_core::vfprintf_internal(
3333
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
34-
if (ret_val.has_error()) {
35-
libc_errno = printf_core::internal_error_to_errno(ret_val.error);
34+
if (!ret_val.has_value()) {
35+
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
3636
return -1;
3737
}
38-
if (ret_val.value > cpp::numeric_limits<int>::max()) {
38+
if (ret_val.value() > cpp::numeric_limits<int>::max()) {
3939
libc_errno = EOVERFLOW;
4040
return -1;
4141
}
4242

43-
return static_cast<int>(ret_val.value);
43+
return static_cast<int>(ret_val.value());
4444
}
4545

4646
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/printf_core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ add_header_library(
127127
.writer
128128
.core_structs
129129
libc.src.__support.arg_list
130+
libc.src.__support.error_or
130131
)
131132

132133
add_header_library(
@@ -138,6 +139,7 @@ add_header_library(
138139
libc.hdr.func.free
139140
libc.hdr.func.realloc
140141
libc.src.__support.arg_list
142+
libc.src.__support.error_or
141143
libc.src.stdio.printf_core.printf_main
142144
libc.src.stdio.printf_core.writer
143145
)
@@ -154,6 +156,7 @@ add_header_library(
154156
vfprintf_internal.h
155157
DEPENDS
156158
libc.src.__support.File.file
159+
libc.src.__support.error_or
157160
libc.src.__support.arg_list
158161
libc.src.stdio.printf_core.printf_main
159162
libc.src.stdio.printf_core.writer

libc/src/stdio/printf_core/core_structs.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@
2424
namespace LIBC_NAMESPACE_DECL {
2525
namespace printf_core {
2626

27-
struct PrintfResult {
28-
size_t value;
29-
int error;
30-
31-
constexpr PrintfResult(size_t val) : value(val), error(0) {}
32-
constexpr PrintfResult(size_t val, int error) : value(val), error(error) {}
33-
34-
constexpr bool has_error() { return error != 0; }
35-
36-
constexpr operator size_t() { return value; }
37-
};
38-
3927
// These length modifiers match the length modifiers in the format string, which
4028
// is why they are formatted differently from the rest of the file.
4129
enum class LengthModifier { hh, h, l, ll, j, z, t, L, w, wf, none };

libc/src/stdio/printf_core/printf_main.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H
1111

1212
#include "src/__support/arg_list.h"
13+
#include "src/__support/error_or.h"
1314
#include "src/__support/macros/config.h"
1415
#include "src/stdio/printf_core/converter.h"
1516
#include "src/stdio/printf_core/core_structs.h"
@@ -22,8 +23,9 @@ namespace LIBC_NAMESPACE_DECL {
2223
namespace printf_core {
2324

2425
template <WriteMode write_mode>
25-
PrintfResult printf_main(Writer<write_mode> *writer, const char *__restrict str,
26-
internal::ArgList &args) {
26+
ErrorOr<size_t> printf_main(Writer<write_mode> *writer,
27+
const char *__restrict str,
28+
internal::ArgList &args) {
2729
Parser<internal::ArgList> parser(str, args);
2830
int result = 0;
2931
for (FormatSection cur_section = parser.get_next_section();
@@ -34,7 +36,7 @@ PrintfResult printf_main(Writer<write_mode> *writer, const char *__restrict str,
3436
else
3537
result = writer->write(cur_section.raw_string);
3638
if (result < 0)
37-
return {0, -result};
39+
return Error(-result);
3840
}
3941

4042
return writer->get_chars_written();

0 commit comments

Comments
 (0)