[libc-commits] [libc] [libc][errno] Use macro instead of system header (PR #91150)

Robin Caloudis via libc-commits libc-commits at lists.llvm.org
Sun May 5 15:26:06 PDT 2024


https://github.com/robincaloudis created https://github.com/llvm/llvm-project/pull/91150

Closes https://github.com/llvm/llvm-project/issues/80172

>From b80b84970d89f5c51a4db1ddf0bdc9aa66a4c893 Mon Sep 17 00:00:00 2001
From: Robin Caloudis <robin.caloudis at gmx.de>
Date: Mon, 6 May 2024 00:19:59 +0200
Subject: [PATCH] [libc][errno] Use macro instead of system header

---
 libc/include/errno.h.def                      | 20 +-----------
 libc/include/llvm-libc-macros/CMakeLists.txt  |  6 ++++
 .../llvm-libc-macros/error-number-macros.h    |  8 +++++
 .../llvm-libc-macros/linux/CMakeLists.txt     |  9 ++++++
 .../linux/error-number-macros.h               | 32 +++++++++++++++++++
 .../linux/mips/error-number-macros.h          | 24 ++++++++++++++
 .../linux/sparc/error-number-macros.h         | 24 ++++++++++++++
 libc/src/errno/CMakeLists.txt                 |  2 ++
 libc/src/errno/libc_errno.h                   |  7 ++--
 9 files changed, 108 insertions(+), 24 deletions(-)
 create mode 100644 libc/include/llvm-libc-macros/error-number-macros.h
 create mode 100644 libc/include/llvm-libc-macros/linux/error-number-macros.h
 create mode 100644 libc/include/llvm-libc-macros/linux/mips/error-number-macros.h
 create mode 100644 libc/include/llvm-libc-macros/linux/sparc/error-number-macros.h

diff --git a/libc/include/errno.h.def b/libc/include/errno.h.def
index d7ae90ad45247d..3ffcd3fe4c721c 100644
--- a/libc/include/errno.h.def
+++ b/libc/include/errno.h.def
@@ -15,29 +15,11 @@
 
 #include <linux/errno.h>
 
-#ifndef ERFKILL
-#define ERFKILL 132
-#endif // ERFKILL
-
-#ifndef EOWNERDEAD
-#define EOWNERDEAD 130
-#endif // EOWNERDEAD
-
-#ifndef EHWPOISON
-#define EHWPOISON 133
-#endif // EHWPOISON
-
-#ifndef ECANCELED
-#define ECANCELED 125
-#endif // ECANCELED
-
 #ifndef ENOTSUP
 #define ENOTSUP EOPNOTSUPP
 #endif // ENOTSUP
 
-#ifndef ENOTRECOVERABLE
-#define ENOTRECOVERABLE 131
-#endif // ENOTRECOVERABLE
+#include "llvm-libc-macros/linux/error-number-macros.h"
 
 #else // __linux__
 #include "llvm-libc-macros/generic-error-number-macros.h"
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 68ba110aec80f1..961830ef97668c 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -37,6 +37,12 @@ add_macro_header(
     assert-macros.h
 )
 
+add_macro_header(
+  error_number_macros
+  HDR
+    error-number-macros.h
+)
+
 add_macro_header(
   generic_error_number_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/error-number-macros.h b/libc/include/llvm-libc-macros/error-number-macros.h
new file mode 100644
index 00000000000000..29bd54d07f2e42
--- /dev/null
+++ b/libc/include/llvm-libc-macros/error-number-macros.h
@@ -0,0 +1,8 @@
+#ifndef LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H
+#define LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H
+
+#ifdef __linux__
+#include "linux/error-number-macros.h"
+#endif
+
+#endif // LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H
diff --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index 4ee429d1db166a..4e8b131f8cf32a 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -1,3 +1,12 @@
+add_header(
+  error_number_macros
+  HDR
+    error-number-macros.h
+  DEPENDS
+    mips/error-number-macros.h
+    sparc/error-number-macros.h
+)
+
 add_header(
   fcntl_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/linux/error-number-macros.h b/libc/include/llvm-libc-macros/linux/error-number-macros.h
new file mode 100644
index 00000000000000..4c8b3feb3dc395
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/error-number-macros.h
@@ -0,0 +1,32 @@
+#ifndef LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
+
+#if defined(__mips__)
+#include "mips/error-number-macros.h"
+
+#elif defined(__sparc__)
+#include "sparc/error-number-macros.h"
+
+#else
+#ifndef ECANCELED
+#define ECANCELED 125
+#endif // ECANCELED
+
+#ifndef EOWNERDEAD
+#define EOWNERDEAD 130
+#endif // EOWNERDEAD
+
+#ifndef ENOTRECOVERABLE
+#define ENOTRECOVERABLE 131
+#endif // ENOTRECOVERABLE
+
+#ifndef ERFKILL
+#define ERFKILL 132
+#endif // ERFKILL
+
+#ifndef EHWPOISON
+#define EHWPOISON 133
+#endif // EHWPOISON
+#endif
+
+#endif // LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
diff --git a/libc/include/llvm-libc-macros/linux/mips/error-number-macros.h b/libc/include/llvm-libc-macros/linux/mips/error-number-macros.h
new file mode 100644
index 00000000000000..af2a4243e3cea7
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/mips/error-number-macros.h
@@ -0,0 +1,24 @@
+#ifndef LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
+
+#ifndef ECANCELED
+#define ECANCELED 158
+#endif // ECANCELED
+
+#ifndef EOWNERDEAD
+#define EOWNERDEAD 165
+#endif // EOWNERDEAD
+
+#ifndef ENOTRECOVERABLE
+#define ENOTRECOVERABLE 166
+#endif // ENOTRECOVERABLE
+
+#ifndef ERFKILL
+#define ERFKILL 167
+#endif // ERFKILL
+
+#ifndef EHWPOISON
+#define EHWPOISON 168
+#endif // EHWPOISON
+
+#endif // LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
diff --git a/libc/include/llvm-libc-macros/linux/sparc/error-number-macros.h b/libc/include/llvm-libc-macros/linux/sparc/error-number-macros.h
new file mode 100644
index 00000000000000..76a1408bf76014
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/sparc/error-number-macros.h
@@ -0,0 +1,24 @@
+#ifndef LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H
+
+#ifndef ECANCELED
+#define ECANCELED 127
+#endif // ECANCELED
+
+#ifndef EOWNERDEAD
+#define EOWNERDEAD 132
+#endif // EOWNERDEAD
+
+#ifndef ENOTRECOVERABLE
+#define ENOTRECOVERABLE 133
+#endif // ENOTRECOVERABLE
+
+#ifndef ERFKILL
+#define ERFKILL 134
+#endif // ERFKILL
+
+#ifndef EHWPOISON
+#define EHWPOISON 135
+#endif // EHWPOISON
+
+#endif // LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H
diff --git a/libc/src/errno/CMakeLists.txt b/libc/src/errno/CMakeLists.txt
index d9b8d9957c1702..5fc8a0263ea4a6 100644
--- a/libc/src/errno/CMakeLists.txt
+++ b/libc/src/errno/CMakeLists.txt
@@ -19,5 +19,7 @@ add_entrypoint_object(
     ${full_build_flag}
   DEPENDS
     libc.include.errno
+    libc.include.llvm-libc-macros.error_number_macros
+    libc.include.llvm-libc-macros.generic_error_number_macros
     libc.src.__support.common
 )
diff --git a/libc/src/errno/libc_errno.h b/libc/src/errno/libc_errno.h
index 5afc0a41d348ae..1d290b9c4a3fd2 100644
--- a/libc/src/errno/libc_errno.h
+++ b/libc/src/errno/libc_errno.h
@@ -12,11 +12,8 @@
 #include "src/__support/macros/attributes.h"
 #include "src/__support/macros/properties/architectures.h"
 
-// TODO: https://github.com/llvm/llvm-project/issues/80172
-// Separate just the definition of errno numbers in
-// include/llvm-libc-macros/* and only include that instead of the system
-// <errno.h>.
-#include <errno.h>
+#include <error-number-macros.h>
+#include <generic-error-number-macros.h>
 
 // This header is to be consumed by internal implementations, in which all of
 // them should refer to `libc_errno` instead of using `errno` directly from



More information about the libc-commits mailing list