[libc-commits] [libc] ad233c6 - [libc] add printf
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Wed Jun 15 11:45:45 PDT 2022
Author: Michael Jones
Date: 2022-06-15T11:45:38-07:00
New Revision: ad233c6047fc8702ddff833259fbe06364449dc1
URL: https://github.com/llvm/llvm-project/commit/ad233c6047fc8702ddff833259fbe06364449dc1
DIFF: https://github.com/llvm/llvm-project/commit/ad233c6047fc8702ddff833259fbe06364449dc1.diff
LOG: [libc] add printf
This patch adds the entrypoint for printf. With this, building a
"hello world" program with just LLVM-libc is possible.
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D126831
Added:
libc/src/stdio/printf.cpp
libc/src/stdio/printf.h
libc/test/src/stdio/printf_test.cpp
Modified:
libc/config/linux/x86_64/entrypoints.txt
libc/src/stdio/CMakeLists.txt
libc/test/src/stdio/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6e8cb2c1f1112..c744e37fae49d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -280,6 +280,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.sprintf
libc.src.stdio.snprintf
libc.src.stdio.fprintf
+ libc.src.stdio.printf
libc.src.stdio.stderr
libc.src.stdio.stdout
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 7432e0e3b1531..20e33498b07c8 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -261,3 +261,17 @@ add_entrypoint_object(
libc.src.__support.arg_list
libc.src.stdio.printf_core.vfprintf_internal
)
+
+
+add_entrypoint_object(
+ printf
+ SRCS
+ printf.cpp
+ HDRS
+ printf.h
+ DEPENDS
+ libc.src.__support.File.file
+ libc.src.__support.File.platform_file
+ libc.src.__support.arg_list
+ libc.src.stdio.printf_core.vfprintf_internal
+)
diff --git a/libc/src/stdio/printf.cpp b/libc/src/stdio/printf.cpp
new file mode 100644
index 0000000000000..8fd8b9cc57fad
--- /dev/null
+++ b/libc/src/stdio/printf.cpp
@@ -0,0 +1,31 @@
+//===-- Implementation of printf --------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/printf.h"
+
+#include "src/__support/File/file.h"
+#include "src/__support/arg_list.h"
+#include "src/stdio/printf_core/vfprintf_internal.h"
+
+#include <stdarg.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
+ va_list vlist;
+ va_start(vlist, format);
+ internal::ArgList args(vlist); // This holder class allows for easier copying
+ // and pointer semantics, as well as handling
+ // destruction automatically.
+ va_end(vlist);
+ int ret_val = printf_core::vfprintf_internal(
+ reinterpret_cast<::FILE *>(__llvm_libc::stdout), format, args);
+ return ret_val;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdio/printf.h b/libc/src/stdio/printf.h
new file mode 100644
index 0000000000000..c568b04adb7c5
--- /dev/null
+++ b/libc/src/stdio/printf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of printf -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_H
+#define LLVM_LIBC_SRC_STDIO_PRINTF_H
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+int printf(const char *__restrict format, ...);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_PRINTF_H
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index e0e7357620a7c..caf7f4264cae0 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -97,6 +97,17 @@ add_libc_unittest(
libc.src.stdio.fread
)
+
+add_libc_unittest(
+ printf_test
+ SUITE
+ libc_stdio_unittests
+ SRCS
+ printf_test.cpp
+ DEPENDS
+ libc.src.stdio.printf
+)
+
add_subdirectory(printf_core)
add_subdirectory(testdata)
diff --git a/libc/test/src/stdio/printf_test.cpp b/libc/test/src/stdio/printf_test.cpp
new file mode 100644
index 0000000000000..f781d90403b59
--- /dev/null
+++ b/libc/test/src/stdio/printf_test.cpp
@@ -0,0 +1,29 @@
+//===-- Unittests for printf ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/printf.h"
+
+#include "utils/UnitTest/Test.h"
+
+TEST(LlvmLibcPrintfTest, PrintOut) {
+ int written;
+
+ constexpr char simple[] = "A simple string with no conversions.\n";
+ written = __llvm_libc::printf(simple);
+ EXPECT_EQ(written, static_cast<int>(sizeof(simple) - 1));
+
+ constexpr char numbers[] = "1234567890\n";
+ written = __llvm_libc::printf("%s", numbers);
+ EXPECT_EQ(written, static_cast<int>(sizeof(numbers) - 1));
+
+ constexpr char format_more[] = "%s and more\n";
+ constexpr char short_numbers[] = "1234";
+ written = __llvm_libc::printf(format_more, short_numbers);
+ EXPECT_EQ(written,
+ static_cast<int>(sizeof(format_more) + sizeof(short_numbers) - 4));
+}
More information about the libc-commits
mailing list