Skip to content

Commit 428c7e9

Browse files
committed
Add darwin and generic impl of error converter
1 parent 5f3b0af commit 428c7e9

File tree

6 files changed

+76
-12
lines changed

6 files changed

+76
-12
lines changed

libc/src/stdio/printf_core/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ if(printf_config_copts)
3232
list(PREPEND printf_config_copts "COMPILE_OPTIONS")
3333
endif()
3434

35-
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
36-
return()
35+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
36+
add_subdirectory(${LIBC_TARGET_OS})
37+
else()
38+
add_subdirectory(generic)
3739
endif()
38-
add_subdirectory(${LIBC_TARGET_OS})
3940

4041
set(target_error_converter libc.src.stdio.printf_core.${LIBC_TARGET_OS}.${LIBC_TARGET_OS}_error_converter)
4142
if(NOT TARGET ${target_error_converter})
42-
return()
43+
set(target_error_converter libc.src.stdio.printf_core.generic.generic_error_converter)
4344
endif()
4445

4546
add_header_library(

libc/src/stdio/printf_core/baremetal/CMakeLists.txt renamed to libc/src/stdio/printf_core/darwin/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
add_header_library(
2-
baremetal_error_converter
2+
darwin_error_converter
33
HDRS
44
error_converter.h
55
DEPENDS
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===-- Darwin implementation of error converter ----------------*- 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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_DARWIN_ERROR_CONVERTER_H
10+
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_DARWIN_ERROR_CONVERTER_H
11+
12+
#include "hdr/errno_macros.h"
13+
#include "src/stdio/printf_core/core_structs.h"
14+
#include "src/stdio/printf_core/error_converter.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
namespace printf_core {
18+
19+
LIBC_INLINE static int internal_error_to_errno(int internal_error) {
20+
// System error occured, return error as is.
21+
if (internal_error < 1001 && internal_error > 0) {
22+
return internal_error;
23+
}
24+
25+
// Map internal error to POSIX errnos.
26+
switch (-internal_error) {
27+
case WRITE_OK:
28+
return 0;
29+
case FILE_WRITE_ERROR:
30+
return EIO;
31+
case FILE_STATUS_ERROR:
32+
return EIO;
33+
case NULLPTR_WRITE_ERROR:
34+
return EINVAL;
35+
case INT_CONVERSION_ERROR:
36+
return ERANGE;
37+
case FIXED_POINT_CONVERSION_ERROR:
38+
return EINVAL;
39+
case ALLOCATION_ERROR:
40+
return ENOMEM;
41+
case OVERFLOW_ERROR:
42+
return EOVERFLOW;
43+
default:
44+
LIBC_ASSERT(
45+
false &&
46+
"Invalid internal printf error code passed to internal_error_to_errno");
47+
return EINVAL;
48+
}
49+
}
50+
51+
} // namespace printf_core
52+
} // namespace LIBC_NAMESPACE_DECL
53+
54+
#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_DARWIN_ERROR_CONVERTER_H

libc/src/stdio/printf_core/error_converter.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
// Maps internal errors to the available errnos on the platform.
1515
#if defined(__linux__)
1616
#include "linux/error_converter.h"
17-
#elif defined(__ELF__)
18-
// TODO: Ideally we would have LIBC_TARGET_OS_IS_BAREMETAL.
19-
#include "baremetal/error_converter.h"
17+
#elif defined(__APPLE__)
18+
#include "darwin/error_converter.h"
19+
#else
20+
#include "generic/error_converter.h"
2021
#endif
2122

2223
#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_ERROR_CONVERTER_H
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_header_library(
2+
generic_error_converter
3+
HDRS
4+
error_converter.h
5+
DEPENDS
6+
libc.src.stdio.printf_core.core_structs
7+
libc.hdr.errno_macros
8+
)

libc/src/stdio/printf_core/baremetal/error_converter.h renamed to libc/src/stdio/printf_core/generic/error_converter.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===-- Baremetal implementation of error converter -------------*- C++ -*-===//
1+
//===-- Generic implementation of error converter ---------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_BAREMETAL_ERROR_CONVERTER_H
10-
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_BAREMETAL_ERROR_CONVERTER_H
9+
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_GENERIC_ERROR_CONVERTER_H
10+
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_GENERIC_ERROR_CONVERTER_H
1111

1212
#include "hdr/errno_macros.h"
1313
#include "src/stdio/printf_core/core_structs.h"
@@ -46,4 +46,4 @@ LIBC_INLINE static int internal_error_to_errno(int internal_error) {
4646
} // namespace printf_core
4747
} // namespace LIBC_NAMESPACE_DECL
4848

49-
#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_BAREMETAL_ERROR_CONVERTER_H
49+
#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_GENERIC_ERROR_CONVERTER_H

0 commit comments

Comments
 (0)