[libc] Implement sys/sysmacros.h Linux header. - #216865
Conversation
|
@llvm/pr-subscribers-libc Author: Alexey Samsonov (vonosmas) Changes
Implement them as simple macro that redirect to the llvm-libc entrypoints with the same names. This would allow us to have more type safety (and explicitly specify argument/return types in our entrypoint implementation), and opens the door for defining those macro in a different way on other systems, wrapping the invocation of our implementation with type conversions, if needed (e.g. FreeBSD versions of these functions use "int" instead of "unsigned int" and are supposed to be provided by a different header - Patch is 22.25 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/216865.diff 25 Files Affected:
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 071a7830b43d3..193bd59a7473e 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -365,6 +365,11 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/auxv.h entrypoints
libc.src.sys.auxv.getauxval
+ # sys/sysmacros.h entrypoints
+ libc.src.sys.sysmacros.__llvm_libc_makedev
+ libc.src.sys.sysmacros.__llvm_libc_major
+ libc.src.sys.sysmacros.__llvm_libc_minor
+
# termios.h entrypoints
libc.src.termios.cfgetispeed
libc.src.termios.cfgetospeed
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 0b129f738c945..dce49809cc86d 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -66,6 +66,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_statfs
libc.include.sys_statvfs
libc.include.sys_syscall
+ libc.include.sys_sysmacros
libc.include.sys_time
libc.include.sys_types
libc.include.sys_uio
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6045886c2f11c..e28d255599ab3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -404,6 +404,11 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/auxv.h entrypoints
libc.src.sys.auxv.getauxval
+ # sys/sysmacros.h entrypoints
+ libc.src.sys.sysmacros.__llvm_libc_makedev
+ libc.src.sys.sysmacros.__llvm_libc_major
+ libc.src.sys.sysmacros.__llvm_libc_minor
+
# termios.h entrypoints
libc.src.termios.cfgetispeed
libc.src.termios.cfgetospeed
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 3aea6394c98cb..5cd89ca621931 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -69,6 +69,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_statfs
libc.include.sys_statvfs
libc.include.sys_syscall
+ libc.include.sys_sysmacros
libc.include.sys_time
libc.include.sys_types
libc.include.sys_ucontext
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 50cf928e1e95f..4983109aa82f8 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -225,6 +225,14 @@ add_proxy_header_library(
libc.include.llvm-libc-types.ssize_t
)
+add_proxy_header_library(
+ dev_t
+ HDRS
+ dev_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.dev_t
+)
+
add_proxy_header_library(
mode_t
HDRS
diff --git a/libc/hdr/types/dev_t.h b/libc/hdr/types/dev_t.h
new file mode 100644
index 0000000000000..95dea5da04a91
--- /dev/null
+++ b/libc/hdr/types/dev_t.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Proxy header for dev_t.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_DEV_T_H
+#define LLVM_LIBC_HDR_TYPES_DEV_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/dev_t.h"
+
+#else
+
+#include <sys/types.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_DEV_T_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 3c960301d6e0f..5d19a7d38374e 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -865,6 +865,16 @@ add_header_macro(
.llvm-libc-types.struct_statvfs
)
+add_header_macro(
+ sys_sysmacros
+ ../libc/include/sys/sysmacros.yaml
+ sys/sysmacros.h
+ DEPENDS
+ .llvm_libc_common_h
+ .llvm-libc-macros.sys_sysmacros_macros
+ .llvm-libc-types.dev_t
+)
+
add_header_macro(
sys_syscall
../libc/include/sys/syscall.yaml
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 6ada2d8aaa0bb..16afc54970a21 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -352,6 +352,12 @@ add_macro_header(
sys-wait-macros.h
)
+add_macro_header(
+ sys_sysmacros_macros
+ HDR
+ sys-sysmacros-macros.h
+)
+
add_macro_header(
termios_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index c07c6dee8c797..8fcab45d13017 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -82,6 +82,12 @@ add_header(
sys-wait-macros.h
)
+add_header(
+ sys_sysmacros_macros
+ HDR
+ sys-sysmacros-macros.h
+)
+
add_header(
time_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/sys-sysmacros-macros.h b/libc/include/llvm-libc-macros/linux/sys-sysmacros-macros.h
new file mode 100644
index 0000000000000..e51e1bd7483e8
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/sys-sysmacros-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux specific declarations of macros from sys/sysmacros.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_LINUX_SYS_SYSMACROS_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_SYS_SYSMACROS_MACROS_H
+
+#define major(dev) __llvm_libc_major(dev)
+#define minor(dev) __llvm_libc_minor(dev)
+#define makedev(maj, min) __llvm_libc_makedev(maj, min)
+
+#endif // LLVM_LIBC_MACROS_LINUX_SYS_SYSMACROS_MACROS_H
diff --git a/libc/include/llvm-libc-macros/sys-sysmacros-macros.h b/libc/include/llvm-libc-macros/sys-sysmacros-macros.h
new file mode 100644
index 0000000000000..29e4ad6ac3b0e
--- /dev/null
+++ b/libc/include/llvm-libc-macros/sys-sysmacros-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 macros from sys/sysmacros.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_SYS_SYSMACROS_MACROS_H
+#define LLVM_LIBC_MACROS_SYS_SYSMACROS_MACROS_H
+
+#ifdef __linux__
+#include "linux/sys-sysmacros-macros.h"
+#endif
+
+#endif // LLVM_LIBC_MACROS_SYS_SYSMACROS_MACROS_H
diff --git a/libc/include/sys/sysmacros.yaml b/libc/include/sys/sysmacros.yaml
new file mode 100644
index 0000000000000..8845e4c84f7bf
--- /dev/null
+++ b/libc/include/sys/sysmacros.yaml
@@ -0,0 +1,26 @@
+header: sys/sysmacros.h
+standards:
+ - linux
+macros:
+ - macro_name: major
+ macro_header: sys-sysmacros-macros.h
+ - macro_name: minor
+ macro_header: sys-sysmacros-macros.h
+ - macro_name: makedev
+ macro_header: sys-sysmacros-macros.h
+enums: []
+objects: []
+functions:
+ - name: __llvm_libc_major
+ return_type: unsigned int
+ arguments:
+ - type: dev_t
+ - name: __llvm_libc_minor
+ return_type: unsigned int
+ arguments:
+ - type: dev_t
+ - name: __llvm_libc_makedev
+ return_type: dev_t
+ arguments:
+ - type: unsigned int
+ - type: unsigned int
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 95334f0084f07..5917b91b33eaa 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -19,3 +19,4 @@ add_subdirectory(prctl)
add_subdirectory(ptrace)
add_subdirectory(uio)
add_subdirectory(ioctl)
+add_subdirectory(sysmacros)
diff --git a/libc/src/sys/sysmacros/CMakeLists.txt b/libc/src/sys/sysmacros/CMakeLists.txt
new file mode 100644
index 0000000000000..4558bf5ec8272
--- /dev/null
+++ b/libc/src/sys/sysmacros/CMakeLists.txt
@@ -0,0 +1,24 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+ __llvm_libc_makedev
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.__llvm_libc_makedev
+)
+
+add_entrypoint_object(
+ __llvm_libc_major
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.__llvm_libc_major
+)
+
+add_entrypoint_object(
+ __llvm_libc_minor
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.__llvm_libc_minor
+)
diff --git a/libc/src/sys/sysmacros/linux/CMakeLists.txt b/libc/src/sys/sysmacros/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..f7d753db0a563
--- /dev/null
+++ b/libc/src/sys/sysmacros/linux/CMakeLists.txt
@@ -0,0 +1,35 @@
+add_entrypoint_object(
+ __llvm_libc_makedev
+ SRCS
+ makedev.cpp
+ HDRS
+ ../makedev.h
+ DEPENDS
+ libc.hdr.types.dev_t
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
+add_entrypoint_object(
+ __llvm_libc_major
+ SRCS
+ major.cpp
+ HDRS
+ ../major.h
+ DEPENDS
+ libc.hdr.types.dev_t
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
+add_entrypoint_object(
+ __llvm_libc_minor
+ SRCS
+ minor.cpp
+ HDRS
+ ../minor.h
+ DEPENDS
+ libc.hdr.types.dev_t
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/sys/sysmacros/linux/major.cpp b/libc/src/sys/sysmacros/linux/major.cpp
new file mode 100644
index 0000000000000..dbe52bbd788cd
--- /dev/null
+++ b/libc/src/sys/sysmacros/linux/major.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux implementation of __llvm_libc_major.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/sysmacros/major.h"
+#include "hdr/types/dev_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(dev_t) == 8, "dev_t must be 64-bit");
+
+// dev_t is encoded as MMMM Mmmm mmmM MMmm, where each M represents 4 bytes
+// of the major number and m represents 4 bytes of the minor number. We don't
+// support older systems with smaller dev_t types.
+
+LLVM_LIBC_FUNCTION(unsigned int, __llvm_libc_major, (dev_t dev)) {
+ return static_cast<unsigned int>(((dev >> 8) & 0x00000fff) |
+ ((dev >> 32) & 0xfffff000));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/sysmacros/linux/makedev.cpp b/libc/src/sys/sysmacros/linux/makedev.cpp
new file mode 100644
index 0000000000000..a163ce1127fff
--- /dev/null
+++ b/libc/src/sys/sysmacros/linux/makedev.cpp
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux implementation of __llvm_libc_makedev.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/sysmacros/makedev.h"
+#include "hdr/types/dev_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(dev_t) == 8, "dev_t must be 64-bit");
+
+// dev_t is encoded as MMMM Mmmm mmmM MMmm, where each M represents 4 bytes
+// of the major number and m represents 4 bytes of the minor number. We don't
+// support older systems with smaller dev_t types.
+
+LLVM_LIBC_FUNCTION(dev_t, __llvm_libc_makedev,
+ (unsigned int major, unsigned int minor)) {
+ return (static_cast<dev_t>(major & 0x00000fff) << 8) |
+ (static_cast<dev_t>(major & 0xfffff000) << 32) |
+ (static_cast<dev_t>(minor & 0x000000ff) << 0) |
+ (static_cast<dev_t>(minor & 0xffffff00) << 12);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/sysmacros/linux/minor.cpp b/libc/src/sys/sysmacros/linux/minor.cpp
new file mode 100644
index 0000000000000..5ed666ecd0f0f
--- /dev/null
+++ b/libc/src/sys/sysmacros/linux/minor.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux implementation of __llvm_libc_minor.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/sysmacros/minor.h"
+#include "hdr/types/dev_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(dev_t) == 8, "dev_t must be 64-bit");
+
+// dev_t is encoded as MMMM Mmmm mmmM MMmm, where each M represents 4 bytes
+// of the major number and m represents 4 bytes of the minor number. We don't
+// support older systems with smaller dev_t types.
+
+LLVM_LIBC_FUNCTION(unsigned int, __llvm_libc_minor, (dev_t dev)) {
+ return static_cast<unsigned int>(((dev >> 0) & 0x000000ff) |
+ ((dev >> 12) & 0xffffff00));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/sysmacros/major.h b/libc/src/sys/sysmacros/major.h
new file mode 100644
index 0000000000000..9c57e1f015649
--- /dev/null
+++ b/libc/src/sys/sysmacros/major.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Implementation header for __llvm_libc_major.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_SYSMACROS_MAJOR_H
+#define LLVM_LIBC_SRC_SYS_SYSMACROS_MAJOR_H
+
+#include "hdr/types/dev_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+unsigned int __llvm_libc_major(dev_t dev);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_SYSMACROS_MAJOR_H
diff --git a/libc/src/sys/sysmacros/makedev.h b/libc/src/sys/sysmacros/makedev.h
new file mode 100644
index 0000000000000..e4eeec3376432
--- /dev/null
+++ b/libc/src/sys/sysmacros/makedev.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Implementation header for __llvm_libc_makedev.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_SYSMACROS_MAKEDEV_H
+#define LLVM_LIBC_SRC_SYS_SYSMACROS_MAKEDEV_H
+
+#include "hdr/types/dev_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+dev_t __llvm_libc_makedev(unsigned int major, unsigned int minor);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_SYSMACROS_MAKEDEV_H
diff --git a/libc/src/sys/sysmacros/minor.h b/libc/src/sys/sysmacros/minor.h
new file mode 100644
index 0000000000000..bd8b0a0c8023a
--- /dev/null
+++ b/libc/src/sys/sysmacros/minor.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Implementation header for __llvm_libc_minor.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_SYSMACROS_MINOR_H
+#define LLVM_LIBC_SRC_SYS_SYSMACROS_MINOR_H
+
+#include "hdr/types/dev_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+unsigned int __llvm_libc_minor(dev_t dev);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_SYSMACROS_MINOR_H
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 85f702edcb14e..75b733d1c525c 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -18,3 +18,4 @@ add_subdirectory(uio)
add_subdirectory(time)
add_subdirectory(ioctl)
add_subdirectory(sem)
+add_subdirectory(sysmacros)
diff --git a/libc/test/src/sys/sysmacros/CMakeLists.txt b/libc/test/src/sys/sysmacros/CMakeLists.txt
new file mode 100644
index 0000000000000..b4bbe81c92ff2
--- /dev/null
+++ b/libc/test/src/sys/sysmacros/CMakeLists.txt
@@ -0,0 +1,3 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/sys/sysmacros/linux/CMakeLists.txt b/libc/test/src/sys/sysmacros/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..32df22f5a1d3e
--- /dev/null
+++ b/libc/test/src/sys/sysmacros/linux/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_custom_target(libc_sys_sysmacros_unittests)
+
+add_libc_test(
+ sysmacros_test
+ SUITE
+ libc_sys_sysmacros_unittests
+ SRCS
+ sysmacros_test.cpp
+ DEPENDS
+ libc.hdr.types.dev_t
+ libc.src.sys.sysmacros.__llvm_libc_makedev
+ libc.src.sys.sysmacros.__llvm_libc_major
+ libc.src.sys.sysmacros.__llvm_libc_minor
+)
diff --git a/libc/test/src/sys/sysmacros/linux/sysmacros_test.cpp b/libc/test/src/sys/sysmacros/linux/sysmacros_test.cpp
new file mode 100644
index 0000000000000..3056ff4122a91
--- /dev/null
+++ b/libc/test/src/sys/sysmacros/linux/sysmacros_test.cpp
@@ -0,0 +1,91 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Unittests for sysmacros private entrypoints.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/dev_t.h"
+#include "src/sys/sysmacros/major.h"
+#include "src/sys/sysmacros/makedev.h"
+#include "src/sys/sysmacros/minor.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSysmacrosTest, Zero) {
+ dev_t dev = LIBC_NAMESPACE::__llvm_libc_makedev(0, 0);
+ EXPECT_EQ(dev, static_cast<dev_t>(0));
+ EXPECT_EQ(LIBC_NAMESPACE::__llvm_libc_major(dev), 0u);
+ EXPECT_EQ(LIBC_NAMESPACE::__llvm_libc_minor(dev), 0u);
+}
+
+TEST(LlvmLibcSysmacrosTest, StandardLinuxDevices) {
+ // /dev/null: major 1, minor 3 -> dev = (1 << 8) | 3 = 0x103
+ dev_t dev_null = LIBC_NAMESPACE::__llvm_libc_makedev(1, 3);
+ EXPECT_EQ(dev_null, static_cast<dev_t>(0x103));
+ EXPECT_EQ(LIBC_NAMESPACE::__llvm_libc_major(dev_null), 1u);
+ EXPECT_EQ(LIBC_NAMESPACE::__llvm_libc_minor(dev_null), 3u);
+
+ // /dev/pts/0: major 136 (0x8...
[truncated]
|
<sys/sysmacros.h>is a Linux header with routines for managing device number (construct device ID from major/minor IDs) - and contains functionsmajor,minor, andmakedev.Implement them as simple macro that redirect to the llvm-libc entrypoints with the same names. This would allow us to have more type safety (and explicitly specify argument/return types in our entrypoint implementation), and opens the door for defining those macro in a different way on other systems, wrapping the invocation of our implementation with type conversions, if needed (e.g. FreeBSD versions of these functions use "int" instead of "unsigned int" and are supposed to be provided by a different header -
<sys/types.h>).