Skip to content

Commit 561c42d

Browse files
[libc][errno] Use macro instead of system header (#91150)
## Why Currently, the system header `errno.h` is included in `libc_errno.h`, which is supposed to be consumed by internal implementations only. As unit and hermetic tests should never use `#include <errno.h>` but instead use `#include "src/errno/libc_errno.h"`, we do not want to implicitly include `errno.h`. In order to have a clear seperation between those two, we want to pull out the definitions of errno numbers from `errno.h`. ## What * Extract the definitions of errno numbers from [include/errno.h.def](https://github.com/llvm/llvm-project/pull/91150/files#diff-ed38ed463ed50571b498a5b69039cab58dc9d145da7f751a24da9d77f07781cd) and place it under [include/llvm-libc-macros/linux/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-d6192866629690ebb7cefa1f0a90b6675073e9642f3279df08a04dcdb05fd892) * Provide mips-specific errno numbers in [include/llvm-libc-macros/linux/mips/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-3fd35a4c94e0cc359933e497b10311d857857b2e173e8afebc421b04b7527743) * Find definition of mips errno numbers in glibc [here](https://github.com/bminor/glibc/blob/ea73eb5f581ef5931fd67005aa0c526ba43366c9/sysdeps/unix/sysv/linux/mips/bits/errno.h#L32-L50) (equally defined in the Linux kernel) * Provide sparc-specific errno numbers in [include/llvm-libc-macros/linux/sparc/error-number-macros.h](https://github.com/llvm/llvm-project/pull/91150/files#diff-5f16ffb2a51a6f72ebd4403aca7e1edea48289c99dd5978a1c84385bec4f226b) * Find definition of sparc errno numbers in glibc [here](https://github.com/bminor/glibc/blob/ea73eb5f581ef5931fd67005aa0c526ba43366c9/sysdeps/unix/sysv/linux/sparc/bits/errno.h#L33-L51) (equally defined in the Linux kernel) * Include proxy header `errno_macros.h` instead of the system header `errno.h` in `libc_errno.h`/`libc_errno.cpp` Closes #80172
1 parent efc7bbb commit 561c42d

15 files changed

+158
-26
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ add_proxy_header_library(
3232
libc.include.math
3333
)
3434

35+
add_proxy_header_library(
36+
errno_macros
37+
HDRS
38+
errno_macros.h
39+
FULL_BUILD_DEPENDS
40+
libc.include.errno
41+
libc.include.llvm-libc-macros.error_number_macros
42+
libc.include.llvm-libc-macros.generic_error_number_macros
43+
)
44+
3545
add_proxy_header_library(
3646
fcntl_macros
3747
HDRS

libc/hdr/errno_macros.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Definition of macros from errno.h ---------------------------------===//
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_HDR_ERRNO_MACROS_H
10+
#define LLVM_LIBC_HDR_ERRNO_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#ifdef __linux__
15+
#include "llvm-libc-macros/error-number-macros.h"
16+
#else // __linux__
17+
#include "llvm-libc-macros/generic-error-number-macros.h"
18+
#endif
19+
20+
#else // Overlay mode
21+
22+
#include <errno.h>
23+
24+
#endif // LLVM_LIBC_FULL_BUILD
25+
26+
#endif // LLVM_LIBC_HDR_ERRNO_MACROS_H

libc/include/errno.h.def

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,11 @@
1515

1616
#include <linux/errno.h>
1717

18-
#ifndef ERFKILL
19-
#define ERFKILL 132
20-
#endif // ERFKILL
21-
22-
#ifndef EOWNERDEAD
23-
#define EOWNERDEAD 130
24-
#endif // EOWNERDEAD
25-
26-
#ifndef EHWPOISON
27-
#define EHWPOISON 133
28-
#endif // EHWPOISON
29-
30-
#ifndef ECANCELED
31-
#define ECANCELED 125
32-
#endif // ECANCELED
33-
3418
#ifndef ENOTSUP
3519
#define ENOTSUP EOPNOTSUPP
3620
#endif // ENOTSUP
3721

38-
#ifndef ENOTRECOVERABLE
39-
#define ENOTRECOVERABLE 131
40-
#endif // ENOTRECOVERABLE
22+
#include "llvm-libc-macros/linux/error-number-macros.h"
4123

4224
#else // __linux__
4325
#include "llvm-libc-macros/generic-error-number-macros.h"

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ add_macro_header(
3737
assert-macros.h
3838
)
3939

40+
add_macro_header(
41+
error_number_macros
42+
HDR
43+
error-number-macros.h
44+
)
45+
4046
add_macro_header(
4147
generic_error_number_macros
4248
HDR
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H
2+
#define LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H
3+
4+
#ifdef __linux__
5+
#include "linux/error-number-macros.h"
6+
#endif
7+
8+
#endif // LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H

libc/include/llvm-libc-macros/generic-error-number-macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@
4444
#define EDOM 33
4545
#define ERANGE 34
4646
#define EILSEQ 35
47+
#define ENAMETOOLONG 36
48+
#define EOVERFLOW 75
4749

4850
#endif // LLVM_LIBC_MACROS_GENERIC_ERROR_NUMBER_MACROS_H

libc/include/llvm-libc-macros/linux/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/mips)
2+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sparc)
3+
4+
add_header(
5+
error_number_macros
6+
HDR
7+
error-number-macros.h
8+
DEPENDS
9+
.mips.error_number_macros
10+
.sparc.error_number_macros
11+
)
12+
113
add_header(
214
fcntl_macros
315
HDR
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
2+
#define LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
3+
4+
#if defined(__mips__)
5+
#include "mips/error-number-macros.h"
6+
7+
#elif defined(__sparc__)
8+
#include "sparc/error-number-macros.h"
9+
10+
#else
11+
#ifndef ECANCELED
12+
#define ECANCELED 125
13+
#endif // ECANCELED
14+
15+
#ifndef EOWNERDEAD
16+
#define EOWNERDEAD 130
17+
#endif // EOWNERDEAD
18+
19+
#ifndef ENOTRECOVERABLE
20+
#define ENOTRECOVERABLE 131
21+
#endif // ENOTRECOVERABLE
22+
23+
#ifndef ERFKILL
24+
#define ERFKILL 132
25+
#endif // ERFKILL
26+
27+
#ifndef EHWPOISON
28+
#define EHWPOISON 133
29+
#endif // EHWPOISON
30+
#endif
31+
32+
#endif // LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_header(
2+
error_number_macros
3+
HDR
4+
error-number-macros.h
5+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
2+
#define LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
3+
4+
#ifndef ECANCELED
5+
#define ECANCELED 158
6+
#endif // ECANCELED
7+
8+
#ifndef EOWNERDEAD
9+
#define EOWNERDEAD 165
10+
#endif // EOWNERDEAD
11+
12+
#ifndef ENOTRECOVERABLE
13+
#define ENOTRECOVERABLE 166
14+
#endif // ENOTRECOVERABLE
15+
16+
#ifndef ERFKILL
17+
#define ERFKILL 167
18+
#endif // ERFKILL
19+
20+
#ifndef EHWPOISON
21+
#define EHWPOISON 168
22+
#endif // EHWPOISON
23+
24+
#endif // LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_header(
2+
error_number_macros
3+
HDR
4+
error-number-macros.h
5+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H
2+
#define LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H
3+
4+
#ifndef ECANCELED
5+
#define ECANCELED 127
6+
#endif // ECANCELED
7+
8+
#ifndef EOWNERDEAD
9+
#define EOWNERDEAD 132
10+
#endif // EOWNERDEAD
11+
12+
#ifndef ENOTRECOVERABLE
13+
#define ENOTRECOVERABLE 133
14+
#endif // ENOTRECOVERABLE
15+
16+
#ifndef ERFKILL
17+
#define ERFKILL 134
18+
#endif // ERFKILL
19+
20+
#ifndef EHWPOISON
21+
#define EHWPOISON 135
22+
#endif // EHWPOISON
23+
24+
#endif // LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H

libc/src/errno/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ add_entrypoint_object(
1818
COMPILE_OPTIONS
1919
${full_build_flag}
2020
DEPENDS
21-
libc.include.errno
21+
libc.hdr.errno_macros
2222
libc.src.__support.common
2323
)

libc/src/errno/libc_errno.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; }
3737

3838
#else
3939
// In overlay mode, we simply use the system errno.
40-
#include <errno.h>
40+
#include "hdr/errno_macros.h"
4141

4242
void LIBC_NAMESPACE::Errno::operator=(int a) { errno = a; }
4343
LIBC_NAMESPACE::Errno::operator int() { return errno; }

libc/src/errno/libc_errno.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
#include "src/__support/macros/attributes.h"
1313
#include "src/__support/macros/properties/architectures.h"
1414

15-
// TODO: https://github.com/llvm/llvm-project/issues/80172
16-
// Separate just the definition of errno numbers in
17-
// include/llvm-libc-macros/* and only include that instead of the system
18-
// <errno.h>.
19-
#include <errno.h>
15+
#include "hdr/errno_macros.h"
2016

2117
// This header is to be consumed by internal implementations, in which all of
2218
// them should refer to `libc_errno` instead of using `errno` directly from

0 commit comments

Comments
 (0)