[libc-commits] [libc] [libc] Support ls in printf (PR #178841)
via libc-commits
libc-commits at lists.llvm.org
Thu Jan 29 22:01:20 PST 2026
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 h,cpp -- libc/src/stdio/printf_core/parser.h libc/src/stdio/printf_core/string_converter.h libc/test/src/stdio/sprintf_test.cpp --diff_from_common_commit
``````````
: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/printf_core/parser.h b/libc/src/stdio/printf_core/parser.h
index 40f96772c..06619c826 100644
--- a/libc/src/stdio/printf_core/parser.h
+++ b/libc/src/stdio/printf_core/parser.h
@@ -28,8 +28,8 @@
#include "src/__support/libc_errno.h"
#endif // LIBC_COPT_PRINTF_DISABLE_STRERROR
#ifndef LIBC_COPT_PRINTF_DISABLE_WIDE
-#include "hdr/types/wint_t.h"
#include "hdr/types/wchar_t.h"
+#include "hdr/types/wint_t.h"
#endif // LIBC_COPT_PRINTF_DISABLE_WIDE
namespace LIBC_NAMESPACE_DECL {
@@ -297,7 +297,8 @@ public:
#else
using WideCharArgType = wchar_t;
#endif // LIBC_COPT_PRINTF_DISABLE_WIDE
- WRITE_ARG_VAL_SIMPLEST(section.conv_val_ptr, WideCharArgType *, conv_index);
+ WRITE_ARG_VAL_SIMPLEST(section.conv_val_ptr, WideCharArgType *,
+ conv_index);
} else {
WRITE_ARG_VAL_SIMPLEST(section.conv_val_ptr, char *, conv_index);
}
@@ -689,16 +690,16 @@ private:
#endif // LIBC_COPT_PRINTF_DISABLE_WRITE_INT
case ('p'):
case ('s'):
- if (lm == LengthModifier::l) {
+ if (lm == LengthModifier::l) {
#ifdef LIBC_COPT_PRINTF_DISABLE_WIDE
- using WideCharArgType = void;
+ using WideCharArgType = void;
#else
- using WideCharArgType = wchar_t;
+ using WideCharArgType = wchar_t;
#endif // LIBC_COPT_PRINTF_DISABLE_WIDE
- conv_size = type_desc_from_type<WideCharArgType *>();
- } else {
- conv_size = type_desc_from_type<char *>();
- }
+ conv_size = type_desc_from_type<WideCharArgType *>();
+ } else {
+ conv_size = type_desc_from_type<char *>();
+ }
break;
default:
conv_size = type_desc_from_type<int>();
diff --git a/libc/src/stdio/printf_core/string_converter.h b/libc/src/stdio/printf_core/string_converter.h
index 4da0c7b2f..490ebe939 100644
--- a/libc/src/stdio/printf_core/string_converter.h
+++ b/libc/src/stdio/printf_core/string_converter.h
@@ -44,14 +44,16 @@ LIBC_INLINE int convert_string(Writer<write_mode> *writer,
if (to_conv.length_modifier == LengthModifier::l) {
#ifndef LIBC_COPT_PRINTF_DISABLE_WIDE
- const wchar_t *wstr_ptr = reinterpret_cast<const wchar_t *>(to_conv.conv_val_ptr);
+ const wchar_t *wstr_ptr =
+ reinterpret_cast<const wchar_t *>(to_conv.conv_val_ptr);
#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
if (wstr_ptr == nullptr) {
wstr_ptr = L"(null)";
}
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
size_t written = 0;
- for (const wchar_t *cur_str = (wstr_ptr); cur_str[string_len]; ++string_len) {
+ for (const wchar_t *cur_str = (wstr_ptr); cur_str[string_len];
+ ++string_len) {
wchar_t wc = cur_str[string_len];
auto ret = internal::wcrtomb(buffer, wc, &mbstate);
@@ -60,7 +62,8 @@ LIBC_INLINE int convert_string(Writer<write_mode> *writer,
}
written += ret.value();
- if (to_conv.precision >= 0 && static_cast<size_t>(to_conv.precision) < written) {
+ if (to_conv.precision >= 0 &&
+ static_cast<size_t>(to_conv.precision) < written) {
written -= ret.value();
break;
}
@@ -70,18 +73,20 @@ LIBC_INLINE int convert_string(Writer<write_mode> *writer,
for (const char *cur_str = (str_ptr); cur_str[string_len]; ++string_len) {
;
}
- if (to_conv.precision >= 0 && static_cast<size_t>(to_conv.precision) < string_len) {
- string_len = to_conv.precision;
- }
+ if (to_conv.precision >= 0 &&
+ static_cast<size_t>(to_conv.precision) < string_len) {
+ string_len = to_conv.precision;
+ }
#endif // LIBC_COPT_PRINTF_DISABLE_WIDE
} else {
for (const char *cur_str = (str_ptr); cur_str[string_len]; ++string_len) {
;
}
- if (to_conv.precision >= 0 && static_cast<size_t>(to_conv.precision) < string_len) {
- string_len = to_conv.precision;
- }
+ if (to_conv.precision >= 0 &&
+ static_cast<size_t>(to_conv.precision) < string_len) {
+ string_len = to_conv.precision;
+ }
}
size_t padding_spaces = to_conv.min_width > static_cast<int>(string_len)
@@ -96,7 +101,8 @@ LIBC_INLINE int convert_string(Writer<write_mode> *writer,
if (to_conv.length_modifier == LengthModifier::l) {
#ifndef LIBC_COPT_PRINTF_DISABLE_WIDE
- const wchar_t *wstr_ptr = reinterpret_cast<const wchar_t *>(to_conv.conv_val_ptr);
+ const wchar_t *wstr_ptr =
+ reinterpret_cast<const wchar_t *>(to_conv.conv_val_ptr);
#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
if (wstr_ptr == nullptr) {
@@ -104,14 +110,14 @@ LIBC_INLINE int convert_string(Writer<write_mode> *writer,
}
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
size_t written = 0;
-
+
for (size_t i = 0; written < string_len; ++i) {
- // We don't need to check errors/precision here; Pass 1 guaranteed safety.
- auto ret = internal::wcrtomb(buffer, wstr_ptr[i], &mbstate);
- size_t mb_len = ret.value();
-
- RET_IF_RESULT_NEGATIVE(writer->write({buffer, mb_len}));
- written += mb_len;
+ // We don't need to check errors/precision here; Pass 1 guaranteed safety.
+ auto ret = internal::wcrtomb(buffer, wstr_ptr[i], &mbstate);
+ size_t mb_len = ret.value();
+
+ RET_IF_RESULT_NEGATIVE(writer->write({buffer, mb_len}));
+ written += mb_len;
}
#else
RET_IF_RESULT_NEGATIVE(writer->write({(str_ptr), string_len}));
diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp
index 5b5baed07..6ddb5c5e2 100644
--- a/libc/test/src/stdio/sprintf_test.cpp
+++ b/libc/test/src/stdio/sprintf_test.cpp
@@ -3633,7 +3633,7 @@ TEST(LlvmLibcSprintfTest, WideCharStringConversion) {
EXPECT_EQ(written, 6);
ASSERT_STREQ_LEN(written, buff, "(null)");
- // WEOF test.
+ // WEOF test.
EXPECT_EQ(LIBC_NAMESPACE::sprintf(buff, "%ls", WEOF), -1);
ASSERT_ERRNO_EQ(EILSEQ);
``````````
</details>
https://github.com/llvm/llvm-project/pull/178841
More information about the libc-commits
mailing list