Handle lack of <bits/c++config.h> and <bits/functexcept.h>
authorPeter Griess <pgriess@fb.com>
Tue, 26 Nov 2013 20:12:14 +0000 (12:12 -0800)
committerJordan DeLong <jdelong@fb.com>
Fri, 20 Dec 2013 21:08:11 +0000 (13:08 -0800)
Summary:
- Clang's libc++ doesn't provide these header files. Detect libc++ via
the _LIBCPP_VERSION symbol (pulling it in by sourcing some header files
earlier if necessary) and avoid using these files.
- This is another attempt at D1074481.

Test Plan: .

Reviewed By: andrei.alexandrescu@fb.com

FB internal diff: D1074723

folly/FBString.h
folly/Makefile.am
folly/Malloc.h
folly/Portability.h
folly/Range.h
folly/Traits.h
folly/configure.ac
folly/detail/FunctionalExcept.cpp [new file with mode: 0644]
folly/detail/FunctionalExcept.h [new file with mode: 0644]

index b75db3d4b79465a76cfdd4fef23fd0bd7f9caa87..02e4d276464ffc3c68971e3fd7c3e3b5299105e5 100644 (file)
 #endif
 #endif
 
+#include <atomic>
+#include <limits>
+#include <type_traits>
+
+// libc++ doesn't provide this header
+#ifndef _LIBCPP_VERSION
 // This file appears in two locations: inside fbcode and in the
 // libstdc++ source code (when embedding fbstring as std::string).
 // To aid in this schizophrenic use, two macros are defined in
@@ -65,6 +71,7 @@
 //   _LIBSTDCXX_FBSTRING - Set inside libstdc++.  This is useful to
 //      gate use inside fbcode v. libstdc++
 #include <bits/c++config.h>
+#endif
 
 #ifdef _LIBSTDCXX_FBSTRING
 
 #define FBSTRING_LIKELY(x)   (__builtin_expect((x), 1))
 #define FBSTRING_UNLIKELY(x) (__builtin_expect((x), 0))
 
-#include <atomic>
-#include <limits>
-#include <type_traits>
-
 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wshadow"
index 45c5371679ef26fc38a7c018b7855162f73e0808..f0a3493570b1029150f4185eeb99ee92d0f6f0c7 100644 (file)
@@ -41,6 +41,7 @@ nobase_follyinclude_HEADERS = \
        detail/DiscriminatedPtrDetail.h \
        detail/FileUtilDetail.h \
        detail/FingerprintPolynomial.h \
+       detail/FunctionalExcept.h \
        detail/Futex.h \
        detail/GroupVarintDetail.h \
        detail/Malloc.h \
@@ -174,6 +175,10 @@ if !HAVE_WEAK_SYMBOLS
 libfolly_la_SOURCES += detail/Malloc.cpp
 endif
 
+if !HAVE_BITS_FUNCTEXCEPT
+libfolly_la_SOURCES += detail/FunctionalExcept.cpp
+endif
+
 FingerprintTables.cpp: generate_fingerprint_tables
        ./generate_fingerprint_tables
 CLEANFILES += FingerprintTables.cpp
index 72c0fb1077734b3539700637b54c1ca5e06ebf95..d3ad40d03529d85225bfd4a0b87756e894933fd0 100644 (file)
@@ -54,6 +54,7 @@ __attribute__((weak));
 extern "C" int allocm(void**, size_t*, size_t, int)
 __attribute__((weak));
 
+#include <bits/functexcept.h>
 #define FOLLY_HAVE_MALLOC_H 1
 #else
 #include "folly/detail/Malloc.h"
@@ -76,8 +77,6 @@ __attribute__((weak));
 
 #include <new>
 
-#include <bits/functexcept.h>
-
 /**
  * Define various ALLOCM_* macros normally provided by jemalloc.  We define
  * them so that we don't have to include jemalloc.h, in case the program is
index 1bec1699f6403b56b24a7a2d21ffc50bbdcd23bd..3b07fec9ced4b870e20b60b5d4ed994dabbae1da 100644 (file)
@@ -104,6 +104,13 @@ struct MaxAlign { char c; } __attribute__((aligned));
 #include "folly/detail/Clock.h"
 #endif
 
+// Provide our own std::__throw_* wrappers for platforms that don't have them
+#if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
+#include <bits/functexcept.h>
+#else
+#include "folly/detail/FunctionalExcept.h"
+#endif
+
 #if defined(__cplusplus)
 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
 // usage of __has_trivial_copy(), so we can't use it as a
index 5466c6d6b8d611f067b0fd4bbcfa9f8b06a78763..1d3d0dd0dd3c7693af96749249f6abbfdd6d79b2 100644 (file)
 #include <stdexcept>
 #include <type_traits>
 #include <boost/operators.hpp>
+
+// libc++ doesn't provide this header
+#if !FOLLY_USE_LIBCPP
+// This file appears in two locations: inside fbcode and in the
+// libstdc++ source code (when embedding fbstring as std::string).
+// To aid in this schizophrenic use, two macros are defined in
+// c++config.h:
+//   _LIBSTDCXX_FBSTRING - Set inside libstdc++.  This is useful to
+//      gate use inside fbcode v. libstdc++
 #include <bits/c++config.h>
+#endif
+
 #include "folly/CpuId.h"
 #include "folly/Traits.h"
 #include "folly/Likely.h"
index f5fe5816cf0e2f456aa665788a2795ec289345a1..4d6deeb5628799cc4a61d0964411588586ba8589 100644 (file)
 
 #include "folly/Portability.h"
 
+// libc++ doesn't provide this header
+#if !FOLLY_USE_LIBCPP
+// This file appears in two locations: inside fbcode and in the
+// libstdc++ source code (when embedding fbstring as std::string).
+// To aid in this schizophrenic use, two macros are defined in
+// c++config.h:
+//   _LIBSTDCXX_FBSTRING - Set inside libstdc++.  This is useful to
+//      gate use inside fbcode v. libstdc++
 #include <bits/c++config.h>
+#endif
 
 #include <boost/type_traits.hpp>
 #include <boost/mpl/and.hpp>
index 70afc3f087e930d711799b6db465e6f0ea73170a..825668f74a456e0f3a2a08b19df27e97c1c0ee95 100644 (file)
@@ -52,7 +52,7 @@ AX_BOOST_SYSTEM
 
 # Checks for header files.
 AC_HEADER_STDC
-AC_CHECK_HEADERS([fcntl.h features.h inttypes.h limits.h stdint.h stdlib.h string.h sys/time.h unistd.h mutex.h malloc.h emmintrin.h byteswap.h])
+AC_CHECK_HEADERS([fcntl.h features.h inttypes.h limits.h stdint.h stdlib.h string.h sys/time.h unistd.h mutex.h malloc.h emmintrin.h byteswap.h bits/functexcept.h])
 
 AC_CHECK_HEADER(double-conversion.h, [], [AC_MSG_ERROR(
                 [Couldn't find double-conversion.h, please download from \
@@ -175,6 +175,7 @@ AM_CONDITIONAL([HAVE_STD_THREAD], [test "$ac_cv_header_features" = "yes"])
 AM_CONDITIONAL([HAVE_X86_64], [test "$build_cpu" = "x86_64"])
 AM_CONDITIONAL([HAVE_LINUX], [test "$build_os" == "linux-gnu"])
 AM_CONDITIONAL([HAVE_WEAK_SYMBOLS], [test "$ac_have_weak_symbols" = "yes"])
+AM_CONDITIONAL([HAVE_BITS_FUNCTEXCEPT], [test "$ac_cv_header_bits_functexcept" = "yes"])
 
 # Output
 AC_CONFIG_FILES([Makefile
diff --git a/folly/detail/FunctionalExcept.cpp b/folly/detail/FunctionalExcept.cpp
new file mode 100644 (file)
index 0000000..a6986f7
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2013 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "folly/detail/FunctionalExcept.h"
+
+#include <stdexcept>
+
+FOLLY_NAMESPACE_STD_BEGIN
+
+void __throw_length_error(const char* msg) FOLLY_NORETURN {
+  throw std::length_error(msg);
+}
+
+void __throw_logic_error(const char* msg) FOLLY_NORETURN {
+  throw std::logic_error(msg);
+}
+
+void __throw_out_of_range(const char* msg) FOLLY_NORETURN {
+  throw std::out_of_range(msg);
+}
+
+FOLLY_NAMESPACE_STD_END
diff --git a/folly/detail/FunctionalExcept.h b/folly/detail/FunctionalExcept.h
new file mode 100644 (file)
index 0000000..cda1192
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2013 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FOLLY_DETAIL_FUNCTIONAL_EXCEPT_H
+#define FOLLY_DETAIL_FUNCTIONAL_EXCEPT_H
+
+#include "folly/Portability.h"
+
+FOLLY_NAMESPACE_STD_BEGIN
+
+void __throw_length_error(const char* msg) FOLLY_NORETURN;
+void __throw_logic_error(const char* msg) FOLLY_NORETURN;
+void __throw_out_of_range(const char* msg) FOLLY_NORETURN;
+
+FOLLY_NAMESPACE_STD_END
+
+#endif