[libc-commits] [libc] [libc] Add DTTOIF and IFTODT (PR #241690)

Mike Lothian via libc-commits libc-commits at lists.llvm.org
Sat Nov 28 03:01:55 PST 2026


https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/241690

>From 1e82a29a270c10f3c94135dc275617b9b7c9e9d5 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Mon, 26 Oct 2026 03:20:10 +0000
Subject: [PATCH 1/2] [libc] Add DTTOIF and IFTODT

`DTTOIF` and `IFTODT` convert between the `d_type` of a directory entry and the file type bits of `st_mode`. They are not in POSIX but glibc and the BSDs both have them in `<dirent.h>`, and code that builds a `stat` from a directory entry uses them

They are listed in `dirent.yaml` with the `bsd` standard. The new test checks with static_asserts that each directory entry type maps to the matching `S_IF*` bit and back, and that `IFTODT` ignores the permission bits

AI disclosure: this patch was written with Claude Code. I reviewed the change, built it in a full build and ran the new test. The test fails to build if the shift in either macro is changed

Assisted-by: Claude
---
 libc/include/dirent.yaml                      |  8 ++++
 .../llvm-libc-macros/linux/dirent-macros.h    |  5 +++
 libc/test/include/CMakeLists.txt              | 13 +++++++
 libc/test/include/dirent_dttoif_test.cpp      | 37 +++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 libc/test/include/dirent_dttoif_test.cpp

diff --git a/libc/include/dirent.yaml b/libc/include/dirent.yaml
index 8dc939fa1158a8..e8f4e491cfda63 100644
--- a/libc/include/dirent.yaml
+++ b/libc/include/dirent.yaml
@@ -18,6 +18,14 @@ macros:
     macro_header: dirent-macros.h
   - macro_name: DT_SOCK
     macro_header: dirent-macros.h
+  - macro_name: IFTODT
+    standards:
+      - bsd
+    macro_header: dirent-macros.h
+  - macro_name: DTTOIF
+    standards:
+      - bsd
+    macro_header: dirent-macros.h
 types:
   - type_name: struct_dirent
   - type_name: struct_posix_dent
diff --git a/libc/include/llvm-libc-macros/linux/dirent-macros.h b/libc/include/llvm-libc-macros/linux/dirent-macros.h
index 7d9347593a4a28..7477d5096269f1 100644
--- a/libc/include/llvm-libc-macros/linux/dirent-macros.h
+++ b/libc/include/llvm-libc-macros/linux/dirent-macros.h
@@ -23,4 +23,9 @@
 #define DT_LNK 10
 #define DT_SOCK 12
 
+// Converting between the type a directory entry reports and the one a
+// mode holds, which are the same values twelve bits apart.
+#define IFTODT(mode) (((mode) & 0170000) >> 12)
+#define DTTOIF(dirtype) ((dirtype) << 12)
+
 #endif // LLVM_LIBC_MACROS_LINUX_DIRENT_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 28fdff096bbcea..7d8ed40ab3d4e0 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -10,6 +10,19 @@ add_libc_test(
     libc.include.llvm-libc-macros.alloca_macros
 )
 
+if(LIBC_TARGET_OS_IS_LINUX)
+  add_libc_test(
+    dirent_dttoif_test
+    SUITE
+      libc-include-tests
+    SRCS
+      dirent_dttoif_test.cpp
+    DEPENDS
+      libc.include.dirent
+      libc.include.sys_stat
+  )
+endif()
+
 add_libc_test(
   assert_test
   SUITE
diff --git a/libc/test/include/dirent_dttoif_test.cpp b/libc/test/include/dirent_dttoif_test.cpp
new file mode 100644
index 00000000000000..b5e2945c809bd8
--- /dev/null
+++ b/libc/test/include/dirent_dttoif_test.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Tests that DTTOIF and IFTODT translate between the d_type of a directory
+/// entry and the file type bits of st_mode.
+///
+//===----------------------------------------------------------------------===//
+
+#include "test/UnitTest/Test.h"
+
+#include "include/dirent.h"
+#include "include/sys/stat.h"
+
+// The translation is checked at compile time, so nothing here has to run.
+#define CHECK_TYPE(DT, IF)                                                     \
+  static_assert(DTTOIF(DT) == (IF), "DTTOIF(" #DT ") is not " #IF);            \
+  static_assert(IFTODT(IF) == (DT), "IFTODT(" #IF ") is not " #DT);            \
+  static_assert(IFTODT((IF) | 0777) == (DT),                                   \
+                "IFTODT ignores the permission bits of " #IF)
+
+CHECK_TYPE(DT_FIFO, S_IFIFO);
+CHECK_TYPE(DT_CHR, S_IFCHR);
+CHECK_TYPE(DT_DIR, S_IFDIR);
+CHECK_TYPE(DT_BLK, S_IFBLK);
+CHECK_TYPE(DT_REG, S_IFREG);
+CHECK_TYPE(DT_LNK, S_IFLNK);
+CHECK_TYPE(DT_SOCK, S_IFSOCK);
+
+TEST(LlvmLibcDirentDttoifTest, TypeBitsTranslateAtCompileTime) {
+  // The checks above are static_asserts; this only gives the test a body.
+}

>From 83781d4d7ed3e566fbcc43a3b010c2c10c1b125c Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sat, 28 Nov 2026 04:43:21 +0000
Subject: [PATCH 2/2] [libc] Move the DTTOIF and IFTODT checks into
 dirent_macros_test

Include the macro headers directly, so the test does not depend on the generated headers

Assisted-by: Claude
---
 libc/test/include/CMakeLists.txt              |  8 +++----
 ...dttoif_test.cpp => dirent_macros_test.cpp} | 23 ++++++++-----------
 2 files changed, 14 insertions(+), 17 deletions(-)
 rename libc/test/include/{dirent_dttoif_test.cpp => dirent_macros_test.cpp} (67%)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 7d8ed40ab3d4e0..9bd967af202b3b 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -12,14 +12,14 @@ add_libc_test(
 
 if(LIBC_TARGET_OS_IS_LINUX)
   add_libc_test(
-    dirent_dttoif_test
+    dirent_macros_test
     SUITE
       libc-include-tests
     SRCS
-      dirent_dttoif_test.cpp
+      dirent_macros_test.cpp
     DEPENDS
-      libc.include.dirent
-      libc.include.sys_stat
+      libc.include.llvm-libc-macros.dirent_macros
+      libc.include.llvm-libc-macros.sys_stat_macros
   )
 endif()
 
diff --git a/libc/test/include/dirent_dttoif_test.cpp b/libc/test/include/dirent_macros_test.cpp
similarity index 67%
rename from libc/test/include/dirent_dttoif_test.cpp
rename to libc/test/include/dirent_macros_test.cpp
index b5e2945c809bd8..db8764be8033a6 100644
--- a/libc/test/include/dirent_dttoif_test.cpp
+++ b/libc/test/include/dirent_macros_test.cpp
@@ -14,24 +14,21 @@
 
 #include "test/UnitTest/Test.h"
 
-#include "include/dirent.h"
-#include "include/sys/stat.h"
+#include "include/llvm-libc-macros/dirent-macros.h"
+#include "include/llvm-libc-macros/sys-stat-macros.h"
 
-// The translation is checked at compile time, so nothing here has to run.
 #define CHECK_TYPE(DT, IF)                                                     \
   static_assert(DTTOIF(DT) == (IF), "DTTOIF(" #DT ") is not " #IF);            \
   static_assert(IFTODT(IF) == (DT), "IFTODT(" #IF ") is not " #DT);            \
   static_assert(IFTODT((IF) | 0777) == (DT),                                   \
                 "IFTODT ignores the permission bits of " #IF)
 
-CHECK_TYPE(DT_FIFO, S_IFIFO);
-CHECK_TYPE(DT_CHR, S_IFCHR);
-CHECK_TYPE(DT_DIR, S_IFDIR);
-CHECK_TYPE(DT_BLK, S_IFBLK);
-CHECK_TYPE(DT_REG, S_IFREG);
-CHECK_TYPE(DT_LNK, S_IFLNK);
-CHECK_TYPE(DT_SOCK, S_IFSOCK);
-
-TEST(LlvmLibcDirentDttoifTest, TypeBitsTranslateAtCompileTime) {
-  // The checks above are static_asserts; this only gives the test a body.
+TEST(LlvmLibcDirentMacrosTest, TypeBitsTranslateAtCompileTime) {
+  CHECK_TYPE(DT_FIFO, S_IFIFO);
+  CHECK_TYPE(DT_CHR, S_IFCHR);
+  CHECK_TYPE(DT_DIR, S_IFDIR);
+  CHECK_TYPE(DT_BLK, S_IFBLK);
+  CHECK_TYPE(DT_REG, S_IFREG);
+  CHECK_TYPE(DT_LNK, S_IFLNK);
+  CHECK_TYPE(DT_SOCK, S_IFSOCK);
 }



More information about the libc-commits mailing list