[PATCH] D38567: [config] Warn when POSIX_C_SOURCE breaks threading support on Darwin

Vedant Kumar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 4 15:43:12 PDT 2017


vsk created this revision.

Prior to macOS 10.13 and iOS 11, defining POSIX_C_SOURCE before
including <thread> resulted in hard-to-understand errors. That
definition causes a bunch of important definitions from the system
headers to be skipped, so users see failures like "can't find
mach_port_t".

This patch adds a friendly warning message about the issue.

rdar://problem/31263056


https://reviews.llvm.org/D38567

Files:
  include/__config
  include/__threading_support
  test/libcxx/posix_source.sh.cpp


Index: test/libcxx/posix_source.sh.cpp
===================================================================
--- /dev/null
+++ test/libcxx/posix_source.sh.cpp
@@ -0,0 +1,29 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// Test that we get a warning when using the threading support header without
+// the right defines present.
+
+// REQUIRES: apple-darwin
+// UNSUPPORTED: libcpp-has-no-threads
+
+// RUN: %cxx -c %s -o /dev/null %compile_flags -arch x86_64 -mmacosx-version-min=10.12 -D_LIBCPP_DISABLE_AVAILABILITY 2>&1 | not grep "warning"
+// RUN: %cxx -c %s -o /dev/null %compile_flags -arch x86_64 -mmacosx-version-min=10.12 -D_LIBCPP_DISABLE_AVAILABILITY -D_POSIX_C_SOURCE=200112L 2>&1 | grep "warning" | grep "Define _DARWIN_C_SOURCE"
+// RUN: %cxx -c %s -o /dev/null %compile_flags -arch x86_64 -mmacosx-version-min=10.12 -D_LIBCPP_DISABLE_AVAILABILITY -D_POSIX_C_SOURCE=200112L -D_DARWIN_C_SOURCE 2>&1 | not grep "warning"
+
+// RUN: %cxx -c %s -o /dev/null %compile_flags -arch x86_64 -mmacosx-version-min=10.13 -D_LIBCPP_DISABLE_AVAILABILITY 2>&1 | not grep "warning"
+// RUN: %cxx -c %s -o /dev/null %compile_flags -arch x86_64 -mmacosx-version-min=10.13 -D_LIBCPP_DISABLE_AVAILABILITY -D_POSIX_C_SOURCE=200112L 2>&1 | not grep "warning"
+// RUN: %cxx -c %s -o /dev/null %compile_flags -arch x86_64 -mmacosx-version-min=10.13 -D_LIBCPP_DISABLE_AVAILABILITY -D_POSIX_C_SOURCE=200112L -D_DARWIN_C_SOURCE 2>&1 | not grep "warning"
+
+#include <thread>
+
+int main() {
+  return 0;
+}
Index: include/__threading_support
===================================================================
--- include/__threading_support
+++ include/__threading_support
@@ -23,6 +23,15 @@
 # include <__external_threading>
 #elif !defined(_LIBCPP_HAS_NO_THREADS)
 
+#if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED < 101300)) \
+    || (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && (__IPHONE_OS_VERSION_MIN_REQUIRED < 110000)) \
+    || (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && (__WATCH_OS_VERSION_MIN_REQUIRED < 40000)) \
+    || (defined(__TV_OS_VERSION_MIN_REQUIRED) && (__TV_OS_VERSION_MIN_REQUIRED < 110000))
+# if defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE)
+#  warning "Define _DARWIN_C_SOURCE (or undefine _POSIX_C_SOURCE) for threading support."
+# endif
+#endif
+
 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
 # include <pthread.h>
 # include <sched.h>
Index: include/__config
===================================================================
--- include/__config
+++ include/__config
@@ -901,6 +901,18 @@
      defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
 #   define __MAC_OS_X_VERSION_MIN_REQUIRED __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
 # endif
+# if !defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
+     defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
+#   define __IPHONE_OS_VERSION_MIN_REQUIRED __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
+# endif
+# if !defined(__WATCH_OS_VERSION_MIN_REQUIRED) && \
+     defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__)
+#   define __WATCH_OS_VERSION_MIN_REQUIRED __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
+# endif
+# if !defined(__TV_OS_VERSION_MIN_REQUIRED) && \
+     defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__)
+#   define __TV_OS_VERSION_MIN_REQUIRED __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__
+# endif
 # if defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
 #   if __MAC_OS_X_VERSION_MIN_REQUIRED < 1060
 #     define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38567.117753.patch
Type: text/x-patch
Size: 3828 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171004/14ef071d/attachment-0001.bin>


More information about the cfe-commits mailing list