[libc-commits] [libc] [libc][annex_k] Add errno_t. (PR #163094)
Muhammad Bassiouni via libc-commits
libc-commits at lists.llvm.org
Wed Mar 18 12:56:40 PDT 2026
https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/163094
>From 4aad9e556f118006f7a174d92a56133fbcaf2946 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sun, 12 Oct 2025 22:18:17 +0300
Subject: [PATCH 1/6] [libc][annex_k] Add errno_t.
---
libc/hdr/types/CMakeLists.txt | 9 +++++++++
libc/hdr/types/errno_t.h | 18 ++++++++++++++++++
libc/include/CMakeLists.txt | 1 +
libc/include/errno.h.def | 2 ++
libc/include/llvm-libc-types/CMakeLists.txt | 2 ++
libc/include/llvm-libc-types/errno_t.h | 18 ++++++++++++++++++
6 files changed, 50 insertions(+)
create mode 100644 libc/hdr/types/errno_t.h
create mode 100644 libc/include/llvm-libc-types/errno_t.h
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index dec73b0665f08..f3a12ed0161cf 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -162,6 +162,15 @@ add_proxy_header_library(
libc.include.fcntl
)
+add_proxy_header_library(
+ errno_t
+ HDRS
+ errno_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.errno_t
+ libc.include.errno
+)
+
add_proxy_header_library(
fenv_t
HDRS
diff --git a/libc/hdr/types/errno_t.h b/libc/hdr/types/errno_t.h
new file mode 100644
index 0000000000000..91706b05c9155
--- /dev/null
+++ b/libc/hdr/types/errno_t.h
@@ -0,0 +1,18 @@
+//===-- Proxy for errno_t -------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_ERRNO_T_H
+#define LLVM_LIBC_HDR_TYPES_ERRNO_T_H
+
+#define LIBC_HAS_ANNEX_K
+
+#include "include/llvm-libc-types/errno_t.h"
+
+#undef LIBC_HAS_ANNEX_K
+
+#endif // LLVM_LIBC_HDR_TYPES_ERRNO_T_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index b2ebd035872aa..9eae20c820e98 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -318,6 +318,7 @@ add_header_macro(
DEPENDS
.llvm-libc-macros.generic_error_number_macros
.llvm-libc-macros.error_number_macros
+ .llvm-libc-types.errno_t
)
add_header_macro(
diff --git a/libc/include/errno.h.def b/libc/include/errno.h.def
index 0ac8e57f7a158..05a79bb966435 100644
--- a/libc/include/errno.h.def
+++ b/libc/include/errno.h.def
@@ -39,4 +39,6 @@ __END_C_DECLS
#define errno (*__llvm_libc_errno())
+#include "llvm-libc-types/errno_t.h"
+
#endif // LLVM_LIBC_ERRNO_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 5d82660fd6762..c447910fe684c 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -563,3 +563,5 @@ add_header(EFI_SYSTEM_TABLE
.EFI_TABLE_HEADER
.char16_t
)
+
+add_header(errno_t HDR errno_t.h)
diff --git a/libc/include/llvm-libc-types/errno_t.h b/libc/include/llvm-libc-types/errno_t.h
new file mode 100644
index 0000000000000..f99fe1266ccd1
--- /dev/null
+++ b/libc/include/llvm-libc-types/errno_t.h
@@ -0,0 +1,18 @@
+//===-- Definition of type errno_t ----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
+#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
+
+#ifdef LIBC_HAS_ANNEX_K
+
+typedef int errno_t;
+
+#endif // LIBC_HAS_ANNEX_K
+
+#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
>From 8a2db42490d89bef53f7d495852601098b71826e Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Tue, 14 Oct 2025 00:44:08 +0300
Subject: [PATCH 2/6] change location of errno_t
---
libc/include/llvm-libc-types/CMakeLists.txt | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index c447910fe684c..6139bfeba5cfe 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -29,6 +29,7 @@ add_header(double_t HDR double_t.h)
add_header(DIR HDR DIR.h)
add_header(dev_t HDR dev_t.h)
add_header(div_t HDR div_t.h)
+add_header(errno_t HDR errno_t.h)
add_header(ldiv_t HDR ldiv_t.h)
add_header(lldiv_t HDR lldiv_t.h)
add_header(FILE HDR FILE.h)
@@ -563,5 +564,3 @@ add_header(EFI_SYSTEM_TABLE
.EFI_TABLE_HEADER
.char16_t
)
-
-add_header(errno_t HDR errno_t.h)
>From 731048efa6275eff24f291092eb8873f058bf242 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 17 Oct 2025 01:17:20 +0300
Subject: [PATCH 3/6] add clarifying comment about LIBC_HAS_ANNEX_K
---
libc/include/llvm-libc-types/errno_t.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/libc/include/llvm-libc-types/errno_t.h b/libc/include/llvm-libc-types/errno_t.h
index f99fe1266ccd1..a37b5dece2c35 100644
--- a/libc/include/llvm-libc-types/errno_t.h
+++ b/libc/include/llvm-libc-types/errno_t.h
@@ -9,6 +9,9 @@
#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
+// LIBC_HAS_ANNEX_K is a necessary check guard here because errno_t is only
+// defined when Annex K is enabled. We use LIBC_HAS_ANNEX_K internally to
+// indicate whether Annex K is enabled or not.
#ifdef LIBC_HAS_ANNEX_K
typedef int errno_t;
>From 0b86d8f5a16fdbb646019a6165579b8ac05056a2 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 17 Oct 2025 01:57:02 +0300
Subject: [PATCH 4/6] add errno_t to hdrgen
---
libc/include/errno.h.def | 2 --
libc/include/errno.yaml | 3 ++-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/libc/include/errno.h.def b/libc/include/errno.h.def
index 05a79bb966435..0ac8e57f7a158 100644
--- a/libc/include/errno.h.def
+++ b/libc/include/errno.h.def
@@ -39,6 +39,4 @@ __END_C_DECLS
#define errno (*__llvm_libc_errno())
-#include "llvm-libc-types/errno_t.h"
-
#endif // LLVM_LIBC_ERRNO_H
diff --git a/libc/include/errno.yaml b/libc/include/errno.yaml
index 188a9fa1211a1..86afd2f03b20c 100644
--- a/libc/include/errno.yaml
+++ b/libc/include/errno.yaml
@@ -5,7 +5,8 @@ standards:
- Linux
- POSIX
macros: []
-types: []
+types:
+ - type_name: errno_t
enums: []
objects: []
functions: []
>From 167a6e3f2d7baa9543dea9f3442e4b9b4c6e6181 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 17 Oct 2025 02:06:05 +0300
Subject: [PATCH 5/6] change clarifying comment
---
libc/include/llvm-libc-types/errno_t.h | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/libc/include/llvm-libc-types/errno_t.h b/libc/include/llvm-libc-types/errno_t.h
index a37b5dece2c35..e2c544ad19532 100644
--- a/libc/include/llvm-libc-types/errno_t.h
+++ b/libc/include/llvm-libc-types/errno_t.h
@@ -9,9 +9,12 @@
#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
-// LIBC_HAS_ANNEX_K is a necessary check guard here because errno_t is only
-// defined when Annex K is enabled. We use LIBC_HAS_ANNEX_K internally to
-// indicate whether Annex K is enabled or not.
+// LIBC_HAS_ANNEX_K is used to check whether C11 Annex K (the optional
+// “Bounds-checking interfaces”) is enabled in this libc implementation. Annex K
+// introduces additional types and functions, including `errno_t` (a typedef
+// used by the *_s functions). Since `errno_t` is *not defined* in the standard
+// library unless Annex K is enabled, we must guard any code that uses it with
+// LIBC_HAS_ANNEX_K.
#ifdef LIBC_HAS_ANNEX_K
typedef int errno_t;
>From 170b87dc39186c8f229f2370af839fa006736f28 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 18 Mar 2026 21:50:03 +0200
Subject: [PATCH 6/6] apply suggestions from code review
---
libc/hdr/types/CMakeLists.txt | 1 -
libc/hdr/types/errno_t.h | 16 ++++++++++++++--
libc/include/llvm-libc-types/CMakeLists.txt | 2 +-
libc/include/llvm-libc-types/errno_t.h | 2 ++
4 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index f3a12ed0161cf..ba99a02a10118 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -168,7 +168,6 @@ add_proxy_header_library(
errno_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.errno_t
- libc.include.errno
)
add_proxy_header_library(
diff --git a/libc/hdr/types/errno_t.h b/libc/hdr/types/errno_t.h
index 91706b05c9155..d83692a6585dd 100644
--- a/libc/hdr/types/errno_t.h
+++ b/libc/hdr/types/errno_t.h
@@ -9,10 +9,22 @@
#ifndef LLVM_LIBC_HDR_TYPES_ERRNO_T_H
#define LLVM_LIBC_HDR_TYPES_ERRNO_T_H
-#define LIBC_HAS_ANNEX_K
+#ifndef __STDC_WANT_LIB_EXT1__
+#define __STDC_WANT_LIB_EXT1__ 1
+#endif
+
+#ifdef LIBC_FULL_BUILD
#include "include/llvm-libc-types/errno_t.h"
-#undef LIBC_HAS_ANNEX_K
+#else // Overlay mode
+
+#include <errno.h>
+
+#endif // LIBC_FULL_BUILD
+
+#ifdef __STDC_WANT_LIB_EXT1__
+#undef __STDC_WANT_LIB_EXT1__
+#endif
#endif // LLVM_LIBC_HDR_TYPES_ERRNO_T_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 6139bfeba5cfe..09f9d759f0dd8 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -29,7 +29,7 @@ add_header(double_t HDR double_t.h)
add_header(DIR HDR DIR.h)
add_header(dev_t HDR dev_t.h)
add_header(div_t HDR div_t.h)
-add_header(errno_t HDR errno_t.h)
+add_header(errno_t HDR errno_t.h DEPENDS libc.include.llvm-libc-macros.annex_k_macros)
add_header(ldiv_t HDR ldiv_t.h)
add_header(lldiv_t HDR lldiv_t.h)
add_header(FILE HDR FILE.h)
diff --git a/libc/include/llvm-libc-types/errno_t.h b/libc/include/llvm-libc-types/errno_t.h
index e2c544ad19532..4ce694959ef2e 100644
--- a/libc/include/llvm-libc-types/errno_t.h
+++ b/libc/include/llvm-libc-types/errno_t.h
@@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
+#include "../llvm-libc-macros/annex-k-macros.h"
+
// LIBC_HAS_ANNEX_K is used to check whether C11 Annex K (the optional
// “Bounds-checking interfaces”) is enabled in this libc implementation. Annex K
// introduces additional types and functions, including `errno_t` (a typedef
More information about the libc-commits
mailing list