brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 0991dfc Raw
44 lines · cpp
1//===-- Implementation of asprintf -----------------------------*- 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/asprintf.h"10#include "src/__support/CPP/limits.h"11#include "src/__support/arg_list.h"12#include "src/__support/libc_errno.h"13#include "src/__support/macros/config.h"14#include "src/stdio/printf_core/core_structs.h"15#include "src/stdio/printf_core/error_mapper.h"16#include "src/stdio/printf_core/vasprintf_internal.h"17 18namespace LIBC_NAMESPACE_DECL {19 20LLVM_LIBC_FUNCTION(int, asprintf,21                   (char **__restrict buffer, const char *__restrict format,22                    ...)) {23  va_list vlist;24  va_start(vlist, format);25  internal::ArgList args(vlist); // This holder class allows for easier copying26                                 // and pointer semantics, as well as handling27                                 // destruction automatically.28  va_end(vlist);29  auto ret_val = printf_core::vasprintf_internal(buffer, format, args);30  if (!ret_val.has_value()) {31    libc_errno = printf_core::internal_error_to_errno(ret_val.error());32    return -1;33  }34  if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {35    libc_errno =36        printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);37    return -1;38  }39 40  return static_cast<int>(ret_val.value());41}42 43} // namespace LIBC_NAMESPACE_DECL44