Skip to content

Conversation

michaelrj-google
Copy link
Contributor

Add bazel targets and tests for asprintf and vasprintf. Also clean up a
few things I noticed in the process.

Add bazel targets and tests for asprintf and vasprintf. Also clean up a
few things I noticed in the process.
@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Sep 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

Changes

Add bazel targets and tests for asprintf and vasprintf. Also clean up a
few things I noticed in the process.


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

6 Files Affected:

  • (modified) libc/src/stdio/printf_core/vasprintf_internal.h (-1)
  • (modified) libc/test/src/stdio/CMakeLists.txt (-1)
  • (modified) libc/test/src/stdio/asprintf_test.cpp (+1)
  • (modified) libc/test/src/stdio/vasprintf_test.cpp (-1)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+35)
  • (modified) utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel (+19)
diff --git a/libc/src/stdio/printf_core/vasprintf_internal.h b/libc/src/stdio/printf_core/vasprintf_internal.h
index 9d46617da7751..283d8df2810fb 100644
--- a/libc/src/stdio/printf_core/vasprintf_internal.h
+++ b/libc/src/stdio/printf_core/vasprintf_internal.h
@@ -10,7 +10,6 @@
 #include "hdr/func/malloc.h"
 #include "hdr/func/realloc.h"
 #include "src/__support/arg_list.h"
-#include "src/stdio/printf.h"
 #include "src/stdio/printf_core/core_structs.h"
 #include "src/stdio/printf_core/printf_main.h"
 #include "src/stdio/printf_core/writer.h"
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 4aa8b95880018..eec108bc12ca5 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -270,7 +270,6 @@ add_libc_test(
      libc.src.stdio.vasprintf
      libc.src.string.memset
      libc.include.stdlib
-     libc.src.stdio.sprintf
  )
 
 if(LLVM_LIBC_FULL_BUILD)
diff --git a/libc/test/src/stdio/asprintf_test.cpp b/libc/test/src/stdio/asprintf_test.cpp
index 9292cebb80e24..f6a478be9d1b6 100644
--- a/libc/test/src/stdio/asprintf_test.cpp
+++ b/libc/test/src/stdio/asprintf_test.cpp
@@ -76,6 +76,7 @@ TEST(LlvmLibcASPrintfTest, LargeStringNoConv) {
 TEST(LlvmLibcASPrintfTest, ManyReAlloc) {
   char *buff = nullptr;
   char long_str[1001];
+  //TODO: simplify test to match with vasprintf_test
   auto expected_num_chars =
       LIBC_NAMESPACE::sprintf(long_str, "%200s%200s%200s", "a", "b", "c");
   long_str[expected_num_chars] = '\0';
diff --git a/libc/test/src/stdio/vasprintf_test.cpp b/libc/test/src/stdio/vasprintf_test.cpp
index 2eb1be3d7a9bf..72d7165aa800a 100644
--- a/libc/test/src/stdio/vasprintf_test.cpp
+++ b/libc/test/src/stdio/vasprintf_test.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/stdio/sprintf.h"
 #include "src/stdio/vasprintf.h"
 #include "src/string/memset.h"
 #include "test/UnitTest/Test.h"
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index d9b1bb5635aaf..79ea0e181f453 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6113,6 +6113,31 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "vasprintf_internal",
+    hdrs = ["src/stdio/printf_core/vasprintf_internal.h"],
+    deps = [
+        ":__support_arg_list",
+        ":__support_macros_attributes",
+        ":func_free",
+        ":func_malloc",
+        ":func_realloc",
+        ":printf_main",
+        ":printf_writer",
+        ":types_FILE",
+    ],
+)
+
+libc_function(
+    name = "asprintf",
+    srcs = ["src/stdio/asprintf.cpp"],
+    hdrs = ["src/stdio/asprintf.h"],
+    deps = [
+        ":__support_arg_list",
+        ":vasprintf_internal",
+    ],
+)
+
 libc_function(
     name = "sprintf",
     srcs = ["src/stdio/sprintf.cpp"],
@@ -6164,6 +6189,16 @@ libc_function(
     ],
 )
 
+libc_function(
+    name = "vasprintf",
+    srcs = ["src/stdio/vasprintf.cpp"],
+    hdrs = ["src/stdio/vasprintf.h"],
+    deps = [
+        ":__support_arg_list",
+        ":vasprintf_internal",
+    ],
+)
+
 libc_function(
     name = "vsprintf",
     srcs = ["src/stdio/vsprintf.cpp"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel
index 505b73fd77111..d06fe7bab72e7 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel
@@ -47,6 +47,16 @@ libc_test(
     ],
 )
 
+libc_test(
+    name = "asprintf_test",
+    srcs = ["asprintf_test.cpp"],
+    deps = [
+        "//libc:asprintf",
+        "//libc:memset",
+        "//libc:sprintf",
+    ],
+)
+
 libc_test(
     name = "sprintf_test",
     srcs = ["sprintf_test.cpp"],
@@ -81,6 +91,15 @@ libc_test(
     ],
 )
 
+libc_test(
+    name = "vasprintf_test",
+    srcs = ["vasprintf_test.cpp"],
+    deps = [
+        "//libc:memset",
+        "//libc:vasprintf",
+    ],
+)
+
 libc_test(
     name = "vsprintf_test",
     srcs = ["vsprintf_test.cpp"],

Copy link

github-actions bot commented Sep 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@michaelrj-google michaelrj-google merged commit b7f0bb9 into llvm:main Sep 18, 2025
20 checks passed
@michaelrj-google michaelrj-google deleted the libcAsprintfBazel branch September 18, 2025 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bazel "Peripheral" support tier build system: utils/bazel libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants