mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2024-12-11 08:48:12 +00:00
[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
This commit is contained in:
parent
2e6eccfe34
commit
ad233c6047
@ -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
|
||||
|
||||
|
@ -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
|
||||
)
|
||||
|
31
libc/src/stdio/printf.cpp
Normal file
31
libc/src/stdio/printf.cpp
Normal file
@ -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
|
20
libc/src/stdio/printf.h
Normal file
20
libc/src/stdio/printf.h
Normal file
@ -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
|
@ -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)
|
||||
|
29
libc/test/src/stdio/printf_test.cpp
Normal file
29
libc/test/src/stdio/printf_test.cpp
Normal file
@ -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));
|
||||
}
|
Loading…
Reference in New Issue
Block a user