[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / libc / src / stdio / baremetal / printf.cpp
blob04aa284ee0839dc6f92eef22d8bef9d0c6bd71bf
1 //===-- Implementation of printf for baremetal ------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/stdio/printf.h"
10 #include "src/__support/OSUtil/io.h"
11 #include "src/__support/arg_list.h"
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/printf_core/core_structs.h"
14 #include "src/stdio/printf_core/printf_main.h"
15 #include "src/stdio/printf_core/writer.h"
17 #include <stdarg.h>
18 #include <stddef.h>
20 namespace LIBC_NAMESPACE_DECL {
22 namespace {
24 LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
25 write_to_stderr(new_str);
26 return printf_core::WRITE_OK;
29 } // namespace
31 LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
32 va_list vlist;
33 va_start(vlist, format);
34 internal::ArgList args(vlist); // This holder class allows for easier copying
35 // and pointer semantics, as well as handling
36 // destruction automatically.
37 va_end(vlist);
38 constexpr size_t BUFF_SIZE = 1024;
39 char buffer[BUFF_SIZE];
41 printf_core::WriteBuffer wb(buffer, BUFF_SIZE, &raw_write_hook, nullptr);
42 printf_core::Writer writer(&wb);
44 int retval = printf_core::printf_main(&writer, format, args);
46 int flushval = wb.overflow_write("");
47 if (flushval != printf_core::WRITE_OK)
48 retval = flushval;
50 return retval;
53 } // namespace LIBC_NAMESPACE_DECL