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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "error_to_string.h"
10#include "platform_errors.h"
11
12#include "src/__support/CPP/span.h"
13#include "src/__support/CPP/string_view.h"
14#include "src/__support/CPP/stringstream.h"
15#include "src/__support/StringUtil/message_mapper.h"
16#include "src/__support/integer_to_string.h"
17#include "src/__support/macros/attributes.h"
18
19#include <stddef.h>
20
21namespace LIBC_NAMESPACE {
22namespace internal {
23
24constexpr size_t max_buff_size() {
25 constexpr size_t unknown_str_len = sizeof("Unknown error");
26 // the buffer should be able to hold "Unknown error" + ' ' + num_str
27 return (unknown_str_len + 1 + IntegerToString<int>::buffer_size()) *
28 sizeof(char);
29}
30
31// This is to hold error strings that have to be custom built. It may be
32// rewritten on every call to strerror (or other error to string function).
33constexpr size_t ERR_BUFFER_SIZE = max_buff_size();
34LIBC_THREAD_LOCAL char error_buffer[ERR_BUFFER_SIZE];
35
36constexpr size_t TOTAL_STR_LEN = total_str_len(table: PLATFORM_ERRORS);
37
38// Since the StringMappings array is a map from error numbers to their
39// corresponding strings, we have to have an array large enough we can use the
40// error numbers as indexes. The current linux configuration has 132 values with
41// the maximum value being 133 (41 and 58 are skipped). If other platforms use
42// negative numbers or discontiguous ranges, then the array should be turned
43// into a proper hashmap.
44constexpr size_t ERR_ARRAY_SIZE = max_key_val(table: PLATFORM_ERRORS) + 1;
45
46constexpr MessageMapper<ERR_ARRAY_SIZE, TOTAL_STR_LEN>
47 error_mapper(PLATFORM_ERRORS);
48
49cpp::string_view build_error_string(int err_num, cpp::span<char> buffer) {
50 // if the buffer can't hold "Unknown error" + ' ' + num_str, then just
51 // return "Unknown error".
52 if (buffer.size() <
53 (sizeof("Unknown error") + 1 + IntegerToString<int>::buffer_size()))
54 return const_cast<char *>("Unknown error");
55
56 cpp::StringStream buffer_stream(
57 {const_cast<char *>(buffer.data()), buffer.size()});
58 buffer_stream << "Unknown error" << ' ' << err_num << '\0';
59 return buffer_stream.str();
60}
61
62} // namespace internal
63
64cpp::string_view get_error_string(int err_num) {
65 return get_error_string(err_num,
66 buffer: {internal::error_buffer, internal::ERR_BUFFER_SIZE});
67}
68
69cpp::string_view get_error_string(int err_num, cpp::span<char> buffer) {
70 auto opt_str = internal::error_mapper.get_str(num: err_num);
71 if (opt_str)
72 return *opt_str;
73 else
74 return internal::build_error_string(err_num, buffer);
75}
76
77} // namespace LIBC_NAMESPACE
78

source code of libc/src/__support/StringUtil/error_to_string.cpp