Skip to content

[libc] Fix bugs found when testing with all headers #144049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2025

Conversation

saturn691
Copy link
Contributor

@saturn691 saturn691 commented Jun 13, 2025

Fixes a couple of bugs found when building. The PR to enable the headers can be found here: #144114.

  • math.yaml: float128 guard
  • wchar.yaml: __restrict keyword order

- math.yaml: float128 guard
- wchar.yaml: __restrict keyword order
- converter_test.cpp: add float test
@llvmbot llvmbot added the libc label Jun 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 13, 2025

@llvm/pr-subscribers-libc

Author: William Huynh (saturn691)

Changes

Fixes a couple of bugs found when building, and adds a test, which fails on AArch32 baremetal.

  • math.yaml: float128 guard
  • wchar.yaml: __restrict keyword order
  • converter_test.cpp: add float test

Full diff: https://github.com/llvm/llvm-project/pull/144049.diff

3 Files Affected:

  • (modified) libc/include/math.yaml (+1-1)
  • (modified) libc/include/wchar.yaml (+10-10)
  • (modified) libc/test/src/stdio/printf_core/converter_test.cpp (+31-1)
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index 466c08ade6fc4..11bead0745954 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -734,7 +734,7 @@ functions:
       - type: float128
       - type: float128
       - type: float128
-    guards: LIBC_TYPES_HAS_FLOAT128
+    guard: LIBC_TYPES_HAS_FLOAT128
   - name: ffmal
     standards:
       - stdc
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 57f4f6660827e..39c3a05848dfc 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -109,24 +109,24 @@ functions:
       - stdc
     return_type: wchar_t *
     arguments: 
-      - type: __restrict wchar_t *
-      - type: const __restrict wchar_t *
+      - type: wchar_t *__restrict 
+      - type: const wchar_t *__restrict
       - type: size_t
   - name: wcsncpy
     standards:
       - stdc
     return_type: wchar_t *
     arguments:
-      - type: __restrict wchar_t *
-      - type: const __restrict wchar_t *
+      - type: wchar_t *__restrict
+      - type: const wchar_t *__restrict
       - type: size_t
   - name: wcscat
     standards:
       - stdc
     return_type: wchar_t *
     arguments: 
-      - type: __restrict wchar_t *
-      - type: const __restrict wchar_t *
+      - type: wchar_t *__restrict
+      - type: const wchar_t *__restrict
   - name: wcsstr
     standards:
       - stdc
@@ -139,13 +139,13 @@ functions:
       - stdc
     return_type: wchar_t *
     arguments:
-      - type: __restrict wchar_t *
-      - type: const __restrict wchar_t *
+      - type: wchar_t *__restrict
+      - type: const wchar_t *__restrict
       - type: size_t
   - name: wcscpy
     standards:
       - stdc
     return_type: wchar_t *
     arguments:
-      - type: __restrict wchar_t *
-      - type: const __restrict wchar_t *
+      - type: wchar_t *__restrict
+      - type: const wchar_t *__restrict
diff --git a/libc/test/src/stdio/printf_core/converter_test.cpp b/libc/test/src/stdio/printf_core/converter_test.cpp
index 96a00ae598ec2..b95328398c104 100644
--- a/libc/test/src/stdio/printf_core/converter_test.cpp
+++ b/libc/test/src/stdio/printf_core/converter_test.cpp
@@ -124,7 +124,7 @@ TEST_F(LlvmLibcPrintfConverterTest, StringConversionSimple) {
 TEST_F(LlvmLibcPrintfConverterTest, StringConversionPrecisionHigh) {
   LIBC_NAMESPACE::printf_core::FormatSection high_precision_conv;
   high_precision_conv.has_conv = true;
-  high_precision_conv.raw_string = "%4s";
+  high_precision_conv.raw_string = "%.4s";
   high_precision_conv.conv_name = 's';
   high_precision_conv.precision = 4;
   high_precision_conv.conv_val_ptr = const_cast<char *>("456");
@@ -255,3 +255,33 @@ TEST_F(LlvmLibcPrintfConverterTest, OctConversion) {
   ASSERT_STREQ(str, "1234");
   ASSERT_EQ(writer.get_chars_written(), 4);
 }
+
+TEST_F(LlvmLibcPrintfConverterTest, FloatConversionSimple) {
+
+  LIBC_NAMESPACE::printf_core::FormatSection section;
+  section.has_conv = true;
+  section.raw_string = "%f";
+  section.conv_name = 'f';
+  section.conv_val_raw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(1.234567);
+  LIBC_NAMESPACE::printf_core::convert(&writer, section);
+
+  wb.buff[wb.buff_cur] = '\0';
+  ASSERT_STREQ(str, "1.234567");
+  ASSERT_EQ(writer.get_chars_written(), 8);
+}
+
+TEST_F(LlvmLibcPrintfConverterTest, FloatConversionPrecisionHigh) {
+
+  LIBC_NAMESPACE::printf_core::FormatSection section;
+  section.has_conv = true;
+  section.raw_string = "%12.3f";
+  section.conv_name = 'f';
+  section.min_width = 12;
+  section.precision = 3;
+  section.conv_val_raw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(0.000123);
+  LIBC_NAMESPACE::printf_core::convert(&writer, section);
+
+  wb.buff[wb.buff_cur] = '\0';
+  ASSERT_STREQ(str, "       0.000");
+  ASSERT_EQ(writer.get_chars_written(), 12);
+}

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding these! This change looks good, though I left a comment on why I don't think we should have float tests in converter_test.cpp

Comment on lines 259 to 287
TEST_F(LlvmLibcPrintfConverterTest, FloatConversionSimple) {

LIBC_NAMESPACE::printf_core::FormatSection section;
section.has_conv = true;
section.raw_string = "%f";
section.conv_name = 'f';
section.conv_val_raw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(1.234567);
LIBC_NAMESPACE::printf_core::convert(&writer, section);

wb.buff[wb.buff_cur] = '\0';
ASSERT_STREQ(str, "1.234567");
ASSERT_EQ(writer.get_chars_written(), 8);
}

TEST_F(LlvmLibcPrintfConverterTest, FloatConversionPrecisionHigh) {

LIBC_NAMESPACE::printf_core::FormatSection section;
section.has_conv = true;
section.raw_string = "%12.3f";
section.conv_name = 'f';
section.min_width = 12;
section.precision = 3;
section.conv_val_raw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(0.000123);
LIBC_NAMESPACE::printf_core::convert(&writer, section);

wb.buff[wb.buff_cur] = '\0';
ASSERT_STREQ(str, " 0.000");
ASSERT_EQ(writer.get_chars_written(), 12);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having float conversion tests is useful, but currently they're all in sprintf_test.cpp because there are flags to turn float conversions on and off. There's special cmake logic to ensure that the sprintf tests also get those flags so they don't fail on targets without floats.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the cleanup! I'll merge this for you once the presubmits finish

@michaelrj-google michaelrj-google merged commit fd43215 into llvm:main Jun 13, 2025
13 checks passed
@saturn691 saturn691 deleted the bug-fixes branch June 13, 2025 17:27
michaelrj-google added a commit that referenced this pull request Jun 13, 2025
Following discussion from
https://discourse.llvm.org/t/missing-declarations-in-header-files/86678,
we decided to add a flag to output all headers. Requires #144049.

- Allows outputting all headers
- Minor whitespace change for alignment

---------

Co-authored-by: Michael Jones <[email protected]>
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
Fixes a couple of bugs found when building. The PR to enable the headers
can be found here: llvm#144114.

- math.yaml: float128 guard
- wchar.yaml: __restrict keyword order
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
)

Following discussion from
https://discourse.llvm.org/t/missing-declarations-in-header-files/86678,
we decided to add a flag to output all headers. Requires llvm#144049.

- Allows outputting all headers
- Minor whitespace change for alignment

---------

Co-authored-by: Michael Jones <[email protected]>
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
Fixes a couple of bugs found when building. The PR to enable the headers
can be found here: llvm#144114.

- math.yaml: float128 guard
- wchar.yaml: __restrict keyword order
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
)

Following discussion from
https://discourse.llvm.org/t/missing-declarations-in-header-files/86678,
we decided to add a flag to output all headers. Requires llvm#144049.

- Allows outputting all headers
- Minor whitespace change for alignment

---------

Co-authored-by: Michael Jones <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants