This is an archive of the discontinued LLVM Phabricator instance.

[libc] Correct usage of __unix__ and __linux__
ClosedPublic

Authored by alfredfo on Jun 25 2023, 4:00 PM.

Diff Detail

Event Timeline

alfredfo created this revision.Jun 25 2023, 4:00 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 25 2023, 4:00 PM
alfredfo requested review of this revision.Jun 25 2023, 4:00 PM
alfredfo added a comment.EditedJun 25 2023, 4:03 PM

Here are things still using __unix__, and I'm pretty sure those are correct.

$ grep -rsin '__unix__' -C10 in libc/

gives the following output:

include/llvm-libc-types/once_flag.h-4-// See https://llvm.org/LICENSE.txt for license information.
include/llvm-libc-types/once_flag.h-5-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
include/llvm-libc-types/once_flag.h-6-//
include/llvm-libc-types/once_flag.h-7-//===----------------------------------------------------------------------===//
include/llvm-libc-types/once_flag.h-8-
include/llvm-libc-types/once_flag.h-9-#ifndef __LLVM_LIBC_TYPES_ONCE_FLAG_H__
include/llvm-libc-types/once_flag.h-10-#define __LLVM_LIBC_TYPES_ONCE_FLAG_H__
include/llvm-libc-types/once_flag.h-11-
include/llvm-libc-types/once_flag.h-12-#include <llvm-libc-types/__futex_word.h>
include/llvm-libc-types/once_flag.h-13-
include/llvm-libc-types/once_flag.h:14:#ifdef __unix__
include/llvm-libc-types/once_flag.h-15-typedef __futex_word once_flag;
include/llvm-libc-types/once_flag.h-16-#else
include/llvm-libc-types/once_flag.h-17-#error "Once flag type not defined for the target platform."
include/llvm-libc-types/once_flag.h-18-#endif
include/llvm-libc-types/once_flag.h-19-
include/llvm-libc-types/once_flag.h-20-#endif // __LLVM_LIBC_TYPES_ONCE_FLAG_H__
--
include/llvm-libc-types/pthread_once_t.h-4-// See https://llvm.org/LICENSE.txt for license information.
include/llvm-libc-types/pthread_once_t.h-5-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
include/llvm-libc-types/pthread_once_t.h-6-//
include/llvm-libc-types/pthread_once_t.h-7-//===----------------------------------------------------------------------===//
include/llvm-libc-types/pthread_once_t.h-8-
include/llvm-libc-types/pthread_once_t.h-9-#ifndef __LLVM_LIBC_TYPES_PTHREAD_ONCE_T_H__
include/llvm-libc-types/pthread_once_t.h-10-#define __LLVM_LIBC_TYPES_PTHREAD_ONCE_T_H__
include/llvm-libc-types/pthread_once_t.h-11-
include/llvm-libc-types/pthread_once_t.h-12-#include <llvm-libc-types/__futex_word.h>
include/llvm-libc-types/pthread_once_t.h-13-
include/llvm-libc-types/pthread_once_t.h:14:#ifdef __unix__
include/llvm-libc-types/pthread_once_t.h-15-typedef __futex_word pthread_once_t;
include/llvm-libc-types/pthread_once_t.h-16-#else
include/llvm-libc-types/pthread_once_t.h-17-#error "Once flag type not defined for the target platform."
include/llvm-libc-types/pthread_once_t.h-18-#endif
include/llvm-libc-types/pthread_once_t.h-19-
include/llvm-libc-types/pthread_once_t.h-20-#endif // __LLVM_LIBC_TYPES_PTHREAD_ONCE_T_H__
--
include/llvm-libc-types/__mutex_type.h-12-#include <llvm-libc-types/__futex_word.h>
include/llvm-libc-types/__mutex_type.h-13-
include/llvm-libc-types/__mutex_type.h-14-typedef struct {
include/llvm-libc-types/__mutex_type.h-15-  unsigned char __timed;
include/llvm-libc-types/__mutex_type.h-16-  unsigned char __recursive;
include/llvm-libc-types/__mutex_type.h-17-  unsigned char __robust;
include/llvm-libc-types/__mutex_type.h-18-
include/llvm-libc-types/__mutex_type.h-19-  void *__owner;
include/llvm-libc-types/__mutex_type.h-20-  unsigned long long __lock_count;
include/llvm-libc-types/__mutex_type.h-21-
include/llvm-libc-types/__mutex_type.h:22:#ifdef __unix__
include/llvm-libc-types/__mutex_type.h-23-  __futex_word __ftxw;
include/llvm-libc-types/__mutex_type.h-24-#else
include/llvm-libc-types/__mutex_type.h-25-#error "Mutex type not defined for the target platform."
include/llvm-libc-types/__mutex_type.h-26-#endif
include/llvm-libc-types/__mutex_type.h-27-} __mutex_type;
include/llvm-libc-types/__mutex_type.h-28-
include/llvm-libc-types/__mutex_type.h-29-#endif // __LLVM_LIBC_TYPES___MUTEX_T_H
alfredfo updated this revision to Diff 534381.Jun 25 2023, 4:17 PM

another one (__support/threads/mutex.h)

LGTM, but...

Here are things still using __unix__, and I'm pretty sure those are correct.

All of them seem to be related to the Linux futex word. Wouldn't __linux__ be the most appropriate for them as well?

LGTM, but...

Here are things still using __unix__, and I'm pretty sure those are correct.

All of them seem to be related to the Linux futex word. Wouldn't __linux__ be the most appropriate for them as well?

Oh right, definitely not available on all Unix systems.

libc/include/llvm-libc-types/struct_dirent.h
21

only d_ino (XSI) and d_name are specified by POSIX.

libc/include/llvm-libc-types/struct_utsname.h
15
26

GNU extension

libc/src/__support/File/dir.cpp
43

checked using _DIRENT_HAVE_D_RECLEN, respected by both musl and glibc

ditto _OFF, _TYPE

alfredfo updated this revision to Diff 534730.Jun 26 2023, 1:23 PM

added Linux specific futex word

alfredfo added a comment.EditedJun 26 2023, 1:24 PM

LGTM, but...

Here are things still using __unix__, and I'm pretty sure those are correct.

All of them seem to be related to the Linux futex word. Wouldn't __linux__ be the most appropriate for them as well?

done! I think there should be something addressing pthread_once_t and once_flag for other Unix platforms though. Both of these would've previously, before this commit, been valid on macOS, as __unix__ is defined and __futex_word is just an aligned 32 bit int. No internal Linux headers were used here before that would've caused an error.

typedef struct {
  _Alignas(sizeof(__UINT32_TYPE__) > _Alignof(__UINT32_TYPE__)
               ? sizeof(__UINT32_TYPE__)
               : _Alignof(__UINT32_TYPE__)) __UINT32_TYPE__ __word;
} __futex_word;

from __futex_word.h

michaelrj accepted this revision.Jun 26 2023, 2:06 PM

LGTM, and if there are spots where both linux and macos should be accepted then we should probably make those #if defined(__linux__) || defined(__APPLE__)

This revision is now accepted and ready to land.Jun 26 2023, 2:06 PM
thesamesam accepted this revision.Jul 2 2023, 5:07 PM
This revision was automatically updated to reflect the committed changes.

LGTM, and if there are spots where both linux and macos should be accepted then we should probably make those #if defined(__linux__) || defined(__APPLE__)

Yeah, definitely, or it'll end up biting us later on.