[libc-commits] [libc] [libc] Move INET_ADDRSTRLEN and INET6_ADDRSTRLEN to a common header (PR #201083)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Tue Jun 2 04:00:29 PDT 2026
https://github.com/labath updated https://github.com/llvm/llvm-project/pull/201083
>From 31cf3eff3aec724f2ba632167ca2efd4c084ae6e Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Tue, 2 Jun 2026 09:08:40 +0000
Subject: [PATCH 1/2] [libc] Move INET_ADDRSTRLEN and INET6_ADDRSTRLEN to a
common header
INET_ADDRSTRLEN and INET6_ADDRSTRLEN are needed by both <netinet/in.h>
and <arpa/inet.h>. Previously we had them defined directly inside
netinet-in-macros.h, which meant arpa/inet.h did not have access to
them.
I've moved them to a new inet-address-macros.h header and configured
both YAML header targets to depend on it so they get included in both
generated headers.
I'm also updating the docgen YAML file for arpa/inet.h to document
these macros.
Note that other libc implementations simply have arpa/inet.h include
netinet/in.h (which is permitted by POSIX). This implementation takes a
stricter stance and only exposes the symbols which are specified by
POSIX.
Assisted by Gemini.
---
libc/include/CMakeLists.txt | 2 ++
libc/include/arpa/inet.yaml | 6 +++++-
libc/include/llvm-libc-macros/CMakeLists.txt | 6 ++++++
.../llvm-libc-macros/inet-address-macros.h | 20 +++++++++++++++++++
.../llvm-libc-macros/netinet-in-macros.h | 2 --
libc/include/netinet/in.yaml | 4 ++++
libc/utils/docgen/arpa/inet.yaml | 5 +++++
7 files changed, 42 insertions(+), 3 deletions(-)
create mode 100644 libc/include/llvm-libc-macros/inet-address-macros.h
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index a83c6bfc79537..cdb5259bc70cf 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -207,6 +207,7 @@ add_header_macro(
.inttypes
.llvm-libc-types.in_addr_t
.llvm-libc-types.struct_in_addr
+ .llvm-libc-macros.inet_address_macros
)
file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/netinet)
@@ -217,6 +218,7 @@ add_header_macro(
netinet/in.h
DEPENDS
.llvm-libc-macros.netinet_in_macros
+ .llvm-libc-macros.inet_address_macros
.llvm-libc-types.in_addr_t
.llvm-libc-types.in_port_t
.llvm-libc-types.sa_family_t
diff --git a/libc/include/arpa/inet.yaml b/libc/include/arpa/inet.yaml
index 726aeb24aa0cd..ab7b2e52dd127 100644
--- a/libc/include/arpa/inet.yaml
+++ b/libc/include/arpa/inet.yaml
@@ -1,7 +1,11 @@
header: arpa/inet.h
standards:
- posix
-macros: []
+macros:
+ - macro_name: INET_ADDRSTRLEN
+ macro_header: inet-address-macros.h
+ - macro_name: INET6_ADDRSTRLEN
+ macro_header: inet-address-macros.h
types:
- type_name: in_addr_t
- type_name: struct_in_addr
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 952da4a5544d9..5574f44cab21c 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -344,6 +344,12 @@ add_macro_header(
wchar-macros.h
)
+add_macro_header(
+ inet_address_macros
+ HDR
+ inet-address-macros.h
+)
+
add_macro_header(
inttypes_macros
HDR
diff --git a/libc/include/llvm-libc-macros/inet-address-macros.h b/libc/include/llvm-libc-macros/inet-address-macros.h
new file mode 100644
index 0000000000000..4817c57909bc1
--- /dev/null
+++ b/libc/include/llvm-libc-macros/inet-address-macros.h
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of internet address macros
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_INET_ADDRESS_MACROS_H
+#define LLVM_LIBC_MACROS_INET_ADDRESS_MACROS_H
+
+#define INET_ADDRSTRLEN 16
+#define INET6_ADDRSTRLEN 46
+
+#endif // LLVM_LIBC_MACROS_INET_ADDRESS_MACROS_H
diff --git a/libc/include/llvm-libc-macros/netinet-in-macros.h b/libc/include/llvm-libc-macros/netinet-in-macros.h
index ca10e7ff10a70..6f40352a6afa8 100644
--- a/libc/include/llvm-libc-macros/netinet-in-macros.h
+++ b/libc/include/llvm-libc-macros/netinet-in-macros.h
@@ -33,8 +33,6 @@
// Not specified by POSIX, added in SVR4
#define INADDR_LOOPBACK __LLVM_LIBC_CAST(static_cast, in_addr_t, 0x7f000001)
-#define INET_ADDRSTRLEN 16
-#define INET6_ADDRSTRLEN 46
// The following macros test for special IPv6 addresses. Each macro is of type
// int and takes a single argument of type const struct in6_addr *:
diff --git a/libc/include/netinet/in.yaml b/libc/include/netinet/in.yaml
index 2354e656c4bfc..942daf271d308 100644
--- a/libc/include/netinet/in.yaml
+++ b/libc/include/netinet/in.yaml
@@ -14,6 +14,10 @@ macros:
macro_header: netinet-in-macros.h
- macro_name: IPPROTO_RAW
macro_header: netinet-in-macros.h
+ - macro_name: INET_ADDRSTRLEN
+ macro_header: inet-address-macros.h
+ - macro_name: INET6_ADDRSTRLEN
+ macro_header: inet-address-macros.h
types:
- type_name: in_port_t
- type_name: in_addr_t
diff --git a/libc/utils/docgen/arpa/inet.yaml b/libc/utils/docgen/arpa/inet.yaml
index d72575981f799..6acf93d8d4d7b 100644
--- a/libc/utils/docgen/arpa/inet.yaml
+++ b/libc/utils/docgen/arpa/inet.yaml
@@ -1,3 +1,8 @@
+macros:
+ INET_ADDRSTRLEN:
+ in-latest-posix: ''
+ INET6_ADDRSTRLEN:
+ in-latest-posix: ''
functions:
htonl:
in-latest-posix: ''
>From f7ae4d7b80f3a1d8518d71ca1a4068c5893522c2 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Tue, 2 Jun 2026 12:34:25 +0200
Subject: [PATCH 2/2] remove empty line
---
libc/include/llvm-libc-macros/netinet-in-macros.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/include/llvm-libc-macros/netinet-in-macros.h b/libc/include/llvm-libc-macros/netinet-in-macros.h
index 6f40352a6afa8..f85f6f5403635 100644
--- a/libc/include/llvm-libc-macros/netinet-in-macros.h
+++ b/libc/include/llvm-libc-macros/netinet-in-macros.h
@@ -33,7 +33,6 @@
// Not specified by POSIX, added in SVR4
#define INADDR_LOOPBACK __LLVM_LIBC_CAST(static_cast, in_addr_t, 0x7f000001)
-
// The following macros test for special IPv6 addresses. Each macro is of type
// int and takes a single argument of type const struct in6_addr *:
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/netinet_in.h.html
More information about the libc-commits
mailing list