[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libc / src / stdio / printf_core / vfprintf_internal.h
blobc16c3ae15fb7c55e2afb631c14bdef46f4a58be3
1 //===-- Internal implementation header of vfprintf --------------*- 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 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H
12 #include "src/__support/File/file.h"
13 #include "src/__support/arg_list.h"
14 #include "src/__support/macros/attributes.h" // For LIBC_INLINE
15 #include "src/stdio/printf_core/core_structs.h"
16 #include "src/stdio/printf_core/printf_main.h"
17 #include "src/stdio/printf_core/writer.h"
19 #include <stdio.h>
21 namespace LIBC_NAMESPACE {
23 namespace internal {
24 #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
25 LIBC_INLINE int ferror_unlocked(FILE *f) {
26 return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->error_unlocked();
29 LIBC_INLINE void flockfile(FILE *f) {
30 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->lock();
33 LIBC_INLINE void funlockfile(FILE *f) {
34 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->unlock();
37 LIBC_INLINE size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb,
38 FILE *f) {
39 return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->write_unlocked(
40 ptr, size * nmemb);
42 #else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
43 LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror_unlocked(f); }
45 LIBC_INLINE void flockfile(::FILE *f) { ::flockfile(f); }
47 LIBC_INLINE void funlockfile(::FILE *f) { ::funlockfile(f); }
49 LIBC_INLINE size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb,
50 ::FILE *f) {
51 return ::fwrite_unlocked(ptr, size, nmemb, f);
53 #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
54 } // namespace internal
56 namespace printf_core {
58 LIBC_INLINE int file_write_hook(cpp::string_view new_str, void *fp) {
59 ::FILE *target_file = reinterpret_cast<::FILE *>(fp);
60 // Write new_str to the target file. The logic preventing a zero-length write
61 // is in the writer, so we don't check here.
62 size_t written = internal::fwrite_unlocked(new_str.data(), sizeof(char),
63 new_str.size(), target_file);
64 if (written != new_str.size() || internal::ferror_unlocked(target_file))
65 return FILE_WRITE_ERROR;
66 return WRITE_OK;
69 LIBC_INLINE int vfprintf_internal(::FILE *__restrict stream,
70 const char *__restrict format,
71 internal::ArgList &args) {
72 constexpr size_t BUFF_SIZE = 1024;
73 char buffer[BUFF_SIZE];
74 printf_core::WriteBuffer wb(buffer, BUFF_SIZE, &file_write_hook,
75 reinterpret_cast<void *>(stream));
76 Writer writer(&wb);
77 internal::flockfile(stream);
78 int retval = printf_main(&writer, format, args);
79 int flushval = wb.overflow_write("");
80 if (flushval != WRITE_OK)
81 retval = flushval;
82 internal::funlockfile(stream);
83 return retval;
86 } // namespace printf_core
87 } // namespace LIBC_NAMESPACE
89 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H