[libc-commits] [libc] [libc] Add printf error handling (PR #162876)
via libc-commits
libc-commits at lists.llvm.org
Fri Oct 10 09:09:18 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- libc/src/stdio/asprintf.cpp libc/src/stdio/baremetal/printf.cpp libc/src/stdio/baremetal/vprintf.cpp libc/src/stdio/generic/fprintf.cpp libc/src/stdio/generic/printf.cpp libc/src/stdio/generic/vfprintf.cpp libc/src/stdio/generic/vprintf.cpp libc/src/stdio/printf_core/core_structs.h libc/src/stdio/printf_core/fixed_converter.h libc/src/stdio/printf_core/int_converter.h libc/src/stdio/printf_core/printf_main.h libc/src/stdio/printf_core/vasprintf_internal.h libc/src/stdio/printf_core/vfprintf_internal.h libc/src/stdio/printf_core/write_int_converter.h libc/src/stdio/printf_core/writer.h libc/src/stdio/snprintf.cpp libc/src/stdio/sprintf.cpp libc/src/stdio/vasprintf.cpp libc/src/stdio/vsnprintf.cpp libc/src/stdio/vsprintf.cpp libc/src/stdlib/strfromd.cpp libc/src/stdlib/strfromf.cpp libc/src/stdlib/strfroml.cpp libc/src/time/strftime_core/strftime_main.h libc/test/src/stdio/fprintf_test.cpp libc/test/src/stdio/printf_core/converter_test.cpp libc/test/src/stdio/printf_core/writer_test.cpp libc/test/src/stdio/vfprintf_test.cpp libc/test/src/stdlib/StrfromTest.h
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp
index e100f946a..a2e308ed5 100644
--- a/libc/src/stdio/baremetal/printf.cpp
+++ b/libc/src/stdio/baremetal/printf.cpp
@@ -59,7 +59,7 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
return -1;
}
- return static_cast<int>(retval.value);
+ return static_cast<int>(retval.value);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp
index 992618d06..003d7d707 100644
--- a/libc/src/stdio/baremetal/vprintf.cpp
+++ b/libc/src/stdio/baremetal/vprintf.cpp
@@ -50,7 +50,6 @@ LLVM_LIBC_FUNCTION(int, vprintf,
if (flushval != printf_core::WRITE_OK) {
libc_errno = -flushval;
return -1;
-
}
if (retval.value > cpp::numeric_limits<int>::max()) {
diff --git a/libc/src/stdio/printf_core/fixed_converter.h b/libc/src/stdio/printf_core/fixed_converter.h
index a7185dcb1..77384b189 100644
--- a/libc/src/stdio/printf_core/fixed_converter.h
+++ b/libc/src/stdio/printf_core/fixed_converter.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FIXED_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FIXED_CONVERTER_H
+#include "hdr/errno_macros.h"
#include "include/llvm-libc-macros/stdfix-macros.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/ctype_utils.h"
@@ -20,7 +21,6 @@
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
-#include "hdr/errno_macros.h"
#include <inttypes.h>
#include <stddef.h>
@@ -60,7 +60,7 @@ LIBC_INLINE constexpr uint32_t const_ten_exp(uint32_t exponent) {
READ_FX_BITS(unsigned LENGTH_MODIFIER accum); \
} else { \
LIBC_ASSERT(false && "Invalid conversion name passed to convert_fixed"); \
- return -EINVAL; \
+ return -EINVAL; \
} \
} while (false)
diff --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h
index 08e27e1a6..554436c90 100644
--- a/libc/src/stdio/printf_core/int_converter.h
+++ b/libc/src/stdio/printf_core/int_converter.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
+#include "hdr/errno_macros.h"
#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/ctype_utils.h"
@@ -17,7 +18,6 @@
#include "src/stdio/printf_core/converter_utils.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
-#include "hdr/errno_macros.h"
#include <inttypes.h>
#include <stddef.h>
diff --git a/libc/src/stdio/printf_core/write_int_converter.h b/libc/src/stdio/printf_core/write_int_converter.h
index 15c32d9ac..b424278c6 100644
--- a/libc/src/stdio/printf_core/write_int_converter.h
+++ b/libc/src/stdio/printf_core/write_int_converter.h
@@ -9,10 +9,10 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
+#include "hdr/errno_macros.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
-#include "hdr/errno_macros.h"
#include <inttypes.h>
#include <stddef.h>
diff --git a/libc/src/stdlib/strfromd.cpp b/libc/src/stdlib/strfromd.cpp
index 2ef8d7e7b..a0e8bb3ff 100644
--- a/libc/src/stdlib/strfromd.cpp
+++ b/libc/src/stdlib/strfromd.cpp
@@ -28,7 +28,8 @@ LLVM_LIBC_FUNCTION(int, strfromd,
if (section.has_conv)
result = internal::strfromfloat_convert<double>(&writer, section);
else
- result = writer.write(section.raw_string); // TODO everywhere where writer is used, set errno and ret -1
+ result = writer.write(section.raw_string); // TODO everywhere where writer
+ // is used, set errno and ret -1
if (result < 0)
return result;
diff --git a/libc/test/src/stdio/vfprintf_test.cpp b/libc/test/src/stdio/vfprintf_test.cpp
index 6664d500f..804adbc26 100644
--- a/libc/test/src/stdio/vfprintf_test.cpp
+++ b/libc/test/src/stdio/vfprintf_test.cpp
@@ -14,29 +14,29 @@
#include "src/stdio/fclose.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fopen.h"
-#include "src/stdio/fread.h"
#include "src/stdio/fopencookie.h"
+#include "src/stdio/fread.h"
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/vfprintf.h"
-#include "test/UnitTest/Test.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
namespace printf_test {
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using LIBC_NAMESPACE::fclose;
using LIBC_NAMESPACE::ferror;
using LIBC_NAMESPACE::fopen;
-using LIBC_NAMESPACE::fread;
using LIBC_NAMESPACE::fopencookie;
+using LIBC_NAMESPACE::fread;
#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
using ::fclose;
using ::ferror;
using ::fopen;
-using ::fread;
using ::fopencookie;
+using ::fread;
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace printf_test
diff --git a/libc/test/src/stdlib/StrfromTest.h b/libc/test/src/stdlib/StrfromTest.h
index f24178b23..1442f172e 100644
--- a/libc/test/src/stdlib/StrfromTest.h
+++ b/libc/test/src/stdlib/StrfromTest.h
@@ -9,9 +9,9 @@
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FPBits.h"
-#include "test/UnitTest/Test.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
#define ASSERT_STREQ_LEN(actual_written, actual_str, expected_str) \
EXPECT_EQ(actual_written, static_cast<int>(sizeof(expected_str) - 1)); \
``````````
</details>
https://github.com/llvm/llvm-project/pull/162876
More information about the libc-commits
mailing list