[PATCH] D64940: FileSystem: Check for DTTOIF alone, not _DIRENT_HAVE_D_TYPE

Jordan Rose via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 11:58:50 PDT 2019


jordan_rose created this revision.
jordan_rose added reviewers: sammccall, kristina.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

While `d_type` is a non-standard extension to `struct dirent`, only glibc signals its presence with another macro `_DIRENT_HAVE_D_TYPE`. However, any platform with `d_type` also includes a way to convert to `mode_t` values using the macro `DTTOIF`, so we can check for that alone and still be confident that the `d_type` member exists.

(If this turns out to be wrong, I'll go back and set up an actual CMake check.)

I couldn't think of how to write a test for this, because I couldn't think of how to test that a `stat` call doesn't happen without controlling the filesystem or intercepting `stat`, and there's no good cross-platform way to do that that I know of.

Follow-up (almost a year later) to D51918 <https://reviews.llvm.org/D51918>.

rdar://problem/50592673


Repository:
  rL LLVM

https://reviews.llvm.org/D64940

Files:
  llvm/lib/Support/Unix/Path.inc


Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -833,7 +833,10 @@
 static file_type direntType(dirent* Entry) {
   // Most platforms provide the file type in the dirent: Linux/BSD/Mac.
   // The DTTOIF macro lets us reuse our status -> type conversion.
-#if defined(_DIRENT_HAVE_D_TYPE) && defined(DTTOIF)
+  // Note that while glibc provides a macro to see if this is supported,
+  // _DIRENT_HAVE_D_TYPE, it's not defined on BSD/Mac, so we test for the
+  // d_type-to-mode_t conversion macro instead.
+#if defined(DTTOIF)
   return typeForMode(DTTOIF(Entry->d_type));
 #else
   // Other platforms such as Solaris require a stat() to get the type.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64940.210641.patch
Type: text/x-patch
Size: 781 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190718/020db8d5/attachment.bin>


More information about the llvm-commits mailing list