brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.0 KiB · 38916af Raw
85 lines · cpp
1//===-- Implementation of a class for mapping errors to strings -----------===//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 "error_to_string.h"10 11#include <stddef.h>12 13#include "platform_errors.h"14#include "src/__support/CPP/span.h"15#include "src/__support/CPP/string_view.h"16#include "src/__support/CPP/stringstream.h"17#include "src/__support/StringUtil/message_mapper.h"18#include "src/__support/integer_to_string.h"19#include "src/__support/macros/attributes.h"20#include "src/__support/macros/config.h"21 22namespace LIBC_NAMESPACE_DECL {23namespace {24 25constexpr size_t max_buff_size() {26  constexpr size_t unknown_str_len = sizeof("Unknown error");27  // the buffer should be able to hold "Unknown error" + ' ' + num_str28  return (unknown_str_len + 1 + IntegerToString<int>::buffer_size()) *29         sizeof(char);30}31 32// This is to hold error strings that have to be custom built. It may be33// rewritten on every call to strerror (or other error to string function).34constexpr size_t ERR_BUFFER_SIZE = max_buff_size();35LIBC_THREAD_LOCAL char error_buffer[ERR_BUFFER_SIZE];36 37constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_ERRORS);38 39// Since the StringMappings array is a map from error numbers to their40// corresponding strings, we have to have an array large enough we can use the41// error numbers as indexes. The current linux configuration has 132 values with42// the maximum value being 133 (41 and 58 are skipped). If other platforms use43// negative numbers or discontiguous ranges, then the array should be turned44// into a proper hashmap.45constexpr size_t ERR_ARRAY_SIZE = max_key_val(PLATFORM_ERRORS) + 1;46 47constexpr MessageMapper<ERR_ARRAY_SIZE, TOTAL_STR_LEN>48    ERROR_MAPPER(PLATFORM_ERRORS);49 50constexpr MessageMapper<ERR_ARRAY_SIZE, TOTAL_STR_LEN>51    ERRNO_NAME_MAPPER(PLATFORM_ERRNO_NAMES);52 53cpp::string_view build_error_string(int err_num, cpp::span<char> buffer) {54  // if the buffer can't hold "Unknown error" + ' ' + num_str, then just55  // return "Unknown error".56  if (buffer.size() <57      (sizeof("Unknown error") + 1 + IntegerToString<int>::buffer_size()))58    return const_cast<char *>("Unknown error");59 60  cpp::StringStream buffer_stream(61      {const_cast<char *>(buffer.data()), buffer.size()});62  buffer_stream << "Unknown error" << ' ' << err_num << '\0';63  return buffer_stream.str();64}65 66} // namespace67 68cpp::string_view get_error_string(int err_num) {69  return get_error_string(err_num, {error_buffer, ERR_BUFFER_SIZE});70}71 72cpp::string_view get_error_string(int err_num, cpp::span<char> buffer) {73  auto opt_str = ERROR_MAPPER.get_str(err_num);74  if (opt_str)75    return *opt_str;76  else77    return build_error_string(err_num, buffer);78}79 80cpp::optional<cpp::string_view> try_get_errno_name(int err_num) {81  return ERRNO_NAME_MAPPER.get_str(err_num);82}83 84} // namespace LIBC_NAMESPACE_DECL85