39 lines · cpp
1//===-- Implementation of vasprintf -----------------------------*- 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/vasprintf.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/stdio/printf_core/core_structs.h"14#include "src/stdio/printf_core/error_mapper.h"15#include "src/stdio/printf_core/vasprintf_internal.h"16 17namespace LIBC_NAMESPACE_DECL {18 19LLVM_LIBC_FUNCTION(int, vasprintf,20 (char **__restrict ret, const char *__restrict format,21 va_list vlist)) {22 internal::ArgList args(vlist); // This holder class allows for easier copying23 // and pointer semantics, as well as handling24 // destruction automatically.25 auto ret_val = printf_core::vasprintf_internal(ret, format, args);26 if (!ret_val.has_value()) {27 libc_errno = printf_core::internal_error_to_errno(ret_val.error());28 return -1;29 }30 if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {31 libc_errno =32 printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);33 return -1;34 }35 return static_cast<int>(ret_val.value());36}37 38} // namespace LIBC_NAMESPACE_DECL39