brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · ae21602 Raw
51 lines · cpp
1//===-- Implementation of vprintf -------------------------------*- 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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/stdio/vprintf.h"10 11#include "src/__support/CPP/limits.h"12#include "src/__support/File/file.h"13#include "src/__support/arg_list.h"14#include "src/__support/macros/config.h"15#include "src/stdio/printf_core/core_structs.h"16#include "src/stdio/printf_core/error_mapper.h"17#include "src/stdio/printf_core/vfprintf_internal.h"18 19#include "hdr/types/FILE.h"20#include <stdarg.h>21 22#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE23#define PRINTF_STDOUT LIBC_NAMESPACE::stdout24#else // LIBC_COPT_STDIO_USE_SYSTEM_FILE25#define PRINTF_STDOUT ::stdout26#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE27 28namespace LIBC_NAMESPACE_DECL {29 30LLVM_LIBC_FUNCTION(int, vprintf,31                   (const char *__restrict format, va_list vlist)) {32  internal::ArgList args(vlist); // This holder class allows for easier copying33                                 // and pointer semantics, as well as handling34                                 // destruction automatically.35  auto ret_val = printf_core::vfprintf_internal(36      reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);37  if (!ret_val.has_value()) {38    libc_errno = printf_core::internal_error_to_errno(ret_val.error());39    return -1;40  }41  if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {42    libc_errno =43        printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);44    return -1;45  }46 47  return static_cast<int>(ret_val.value());48}49 50} // namespace LIBC_NAMESPACE_DECL51