1 //===-- Implementation of a class for mapping errors to strings -----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/StringUtil/error_to_string.h"
11 #include "src/errno/libc_errno.h" // For error macros
13 #include "src/__support/CPP/array.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"
20 #include "src/__support/StringUtil/tables/error_table.h"
24 namespace __llvm_libc
{
27 constexpr size_t max_buff_size() {
28 constexpr size_t unknown_str_len
= sizeof("Unknown error");
29 constexpr size_t max_num_len
=
30 __llvm_libc::IntegerToString::dec_bufsize
<int>();
31 // the buffer should be able to hold "Unknown error" + ' ' + num_str
32 return (unknown_str_len
+ 1 + max_num_len
) * sizeof(char);
35 // This is to hold error strings that have to be custom built. It may be
36 // rewritten on every call to strerror (or other error to string function).
37 constexpr size_t ERR_BUFFER_SIZE
= max_buff_size();
38 thread_local
char error_buffer
[ERR_BUFFER_SIZE
];
40 constexpr size_t TOTAL_STR_LEN
= total_str_len(PLATFORM_ERRORS
);
42 // Since the StringMappings array is a map from error numbers to their
43 // corresponding strings, we have to have an array large enough we can use the
44 // error numbers as indexes. The current linux configuration has 132 values with
45 // the maximum value being 133 (41 and 58 are skipped). If other platforms use
46 // negative numbers or discontiguous ranges, then the array should be turned
47 // into a proper hashmap.
48 constexpr size_t ERR_ARRAY_SIZE
= max_key_val(PLATFORM_ERRORS
) + 1;
50 constexpr MessageMapper
<ERR_ARRAY_SIZE
, TOTAL_STR_LEN
>
51 error_mapper(PLATFORM_ERRORS
);
53 cpp::string_view
build_error_string(int err_num
, cpp::span
<char> buffer
) {
54 // if the buffer can't hold "Unknown error" + ' ' + num_str, then just
55 // return "Unknown error".
57 (sizeof("Unknown error") + 1 + IntegerToString::dec_bufsize
<int>()))
58 return const_cast<char *>("Unknown error");
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();
66 } // namespace internal
68 cpp::string_view
get_error_string(int err_num
) {
69 return get_error_string(err_num
,
70 {internal::error_buffer
, internal::ERR_BUFFER_SIZE
});
73 cpp::string_view
get_error_string(int err_num
, cpp::span
<char> buffer
) {
74 auto opt_str
= internal::error_mapper
.get_str(err_num
);
78 return internal::build_error_string(err_num
, buffer
);
81 } // namespace __llvm_libc