From 2710d8e105a683656cdaa825b6c0af46ae25f8ae Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Wed, 6 Mar 2019 20:31:57 +0000 Subject: [PATCH] Fix ABI compatibility of `` with VCRuntime. Summary: Currently, libc++'s `` doesn't play nice with `vcruntime`. Specifically: * `logic_error` and `runtime_error` have a different layout. * libc++'s `logic_error` and `runtime_error` override `what()` but `vcruntime` does not. * `vcruntime` uses weak vtables for `` types. * libc++'s `` constructors and assignment operators may have different manglings than `vcruntimes`. This patch makes libc++'s declarations in `` match those provided by MSVC's STL as closely as possible. If MSVC doesn't declare a special member, then neither do we. This ensures that the implicit definitions have the same linkage, visibility, triviality, and noexcept-ness. Reviewers: thomasanderson, ldionne, smeenai Reviewed By: thomasanderson Subscribers: jdoerfert, libcxx-commits Differential Revision: https://reviews.llvm.org/D58945 llvm-svn: 355546 --- libcxx/include/stdexcept | 29 ++++++- libcxx/src/stdexcept.cpp | 87 ++----------------- .../src/support/runtime/stdexcept_default.ipp | 65 ++++++++++++++ .../support/runtime/stdexcept_vcruntime.ipp | 16 ++++ 4 files changed, 114 insertions(+), 83 deletions(-) create mode 100644 libcxx/src/support/runtime/stdexcept_default.ipp create mode 100644 libcxx/src/support/runtime/stdexcept_vcruntime.ipp diff --git a/libcxx/include/stdexcept b/libcxx/include/stdexcept index 6eda619b8f44..481f9043c50d 100644 --- a/libcxx/include/stdexcept +++ b/libcxx/include/stdexcept @@ -54,6 +54,7 @@ public: _LIBCPP_BEGIN_NAMESPACE_STD +#ifndef _LIBCPP_ABI_VCRUNTIME class _LIBCPP_HIDDEN __libcpp_refstring { const char* __imp_; @@ -67,6 +68,7 @@ public: const char* c_str() const _NOEXCEPT {return __imp_;} }; +#endif // !_LIBCPP_ABI_VCRUNTIME _LIBCPP_END_NAMESPACE_STD @@ -76,6 +78,7 @@ namespace std // purposefully not using versioning namespace class _LIBCPP_EXCEPTION_ABI logic_error : public exception { +#ifndef _LIBCPP_ABI_VCRUNTIME private: _VSTD::__libcpp_refstring __imp_; public: @@ -88,11 +91,17 @@ public: virtual ~logic_error() _NOEXCEPT; virtual const char* what() const _NOEXCEPT; +#else +public: + explicit logic_error(const _VSTD::string&); // Symbol uses versioned std::string + _LIBCPP_INLINE_VISIBILITY explicit logic_error(const char* __s) : exception(__s) {} +#endif }; class _LIBCPP_EXCEPTION_ABI runtime_error : public exception { +#ifndef _LIBCPP_ABI_VCRUNTIME private: _VSTD::__libcpp_refstring __imp_; public: @@ -105,6 +114,11 @@ public: virtual ~runtime_error() _NOEXCEPT; virtual const char* what() const _NOEXCEPT; +#else +public: + explicit runtime_error(const _VSTD::string&); // Symbol uses versioned std::string + _LIBCPP_INLINE_VISIBILITY explicit runtime_error(const char* __s) : exception(__s) {} +#endif // _LIBCPP_ABI_VCRUNTIME }; class _LIBCPP_EXCEPTION_ABI domain_error @@ -114,7 +128,9 @@ public: _LIBCPP_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {} _LIBCPP_INLINE_VISIBILITY explicit domain_error(const char* __s) : logic_error(__s) {} +#ifndef _LIBCPP_ABI_VCRUNTIME virtual ~domain_error() _NOEXCEPT; +#endif }; class _LIBCPP_EXCEPTION_ABI invalid_argument @@ -124,7 +140,9 @@ public: _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {} _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s) : logic_error(__s) {} +#ifndef _LIBCPP_ABI_VCRUNTIME virtual ~invalid_argument() _NOEXCEPT; +#endif }; class _LIBCPP_EXCEPTION_ABI length_error @@ -133,8 +151,9 @@ class _LIBCPP_EXCEPTION_ABI length_error public: _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {} _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s) : logic_error(__s) {} - +#ifndef _LIBCPP_ABI_VCRUNTIME virtual ~length_error() _NOEXCEPT; +#endif }; class _LIBCPP_EXCEPTION_ABI out_of_range @@ -144,7 +163,9 @@ public: _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {} _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s) : logic_error(__s) {} +#ifndef _LIBCPP_ABI_VCRUNTIME virtual ~out_of_range() _NOEXCEPT; +#endif }; class _LIBCPP_EXCEPTION_ABI range_error @@ -154,7 +175,9 @@ public: _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {} _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s) : runtime_error(__s) {} +#ifndef _LIBCPP_ABI_VCRUNTIME virtual ~range_error() _NOEXCEPT; +#endif }; class _LIBCPP_EXCEPTION_ABI overflow_error @@ -164,7 +187,9 @@ public: _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {} _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s) : runtime_error(__s) {} +#ifndef _LIBCPP_ABI_VCRUNTIME virtual ~overflow_error() _NOEXCEPT; +#endif }; class _LIBCPP_EXCEPTION_ABI underflow_error @@ -174,7 +199,9 @@ public: _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {} _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s) : runtime_error(__s) {} +#ifndef _LIBCPP_ABI_VCRUNTIME virtual ~underflow_error() _NOEXCEPT; +#endif }; } // std diff --git a/libcxx/src/stdexcept.cpp b/libcxx/src/stdexcept.cpp index 1507062a2a27..f8f335f34ae1 100644 --- a/libcxx/src/stdexcept.cpp +++ b/libcxx/src/stdexcept.cpp @@ -10,87 +10,10 @@ #include "new" #include "string" #include "system_error" -#include "include/refstring.h" -/* For _LIBCPPABI_VERSION */ -#if !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) && \ - (defined(LIBCXX_BUILDING_LIBCXXABI) || defined(__APPLE__) || defined(LIBCXXRT)) -#include + +#ifdef _LIBCPP_ABI_VCRUNTIME +#include "support/runtime/stdexcept_vcruntime.ipp" +#else +#include "support/runtime/stdexcept_default.ipp" #endif - -static_assert(sizeof(std::__libcpp_refstring) == sizeof(const char *), ""); - - -namespace std // purposefully not using versioning namespace -{ - -logic_error::logic_error(const string& msg) : __imp_(msg.c_str()) -{ -} - -logic_error::logic_error(const char* msg) : __imp_(msg) -{ -} - -logic_error::logic_error(const logic_error& le) _NOEXCEPT : __imp_(le.__imp_) -{ -} - -logic_error& -logic_error::operator=(const logic_error& le) _NOEXCEPT -{ - __imp_ = le.__imp_; - return *this; -} - -runtime_error::runtime_error(const string& msg) : __imp_(msg.c_str()) -{ -} - -runtime_error::runtime_error(const char* msg) : __imp_(msg) -{ -} - -runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT - : __imp_(le.__imp_) -{ -} - -runtime_error& -runtime_error::operator=(const runtime_error& le) _NOEXCEPT -{ - __imp_ = le.__imp_; - return *this; -} - -#if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX) - -const char* -logic_error::what() const _NOEXCEPT -{ - return __imp_.c_str(); -} - -const char* -runtime_error::what() const _NOEXCEPT -{ - return __imp_.c_str(); -} - -#if !defined(_LIBCPP_ABI_VCRUNTIME) - -logic_error::~logic_error() _NOEXCEPT {} -domain_error::~domain_error() _NOEXCEPT {} -invalid_argument::~invalid_argument() _NOEXCEPT {} -length_error::~length_error() _NOEXCEPT {} -out_of_range::~out_of_range() _NOEXCEPT {} - -runtime_error::~runtime_error() _NOEXCEPT {} -range_error::~range_error() _NOEXCEPT {} -overflow_error::~overflow_error() _NOEXCEPT {} -underflow_error::~underflow_error() _NOEXCEPT {} - -#endif -#endif - -} // std diff --git a/libcxx/src/support/runtime/stdexcept_default.ipp b/libcxx/src/support/runtime/stdexcept_default.ipp new file mode 100644 index 000000000000..91dd54b28cfe --- /dev/null +++ b/libcxx/src/support/runtime/stdexcept_default.ipp @@ -0,0 +1,65 @@ +//===--------------------- stdexcept_default.ipp --------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "../../include/refstring.h" + +/* For _LIBCPPABI_VERSION */ +#if !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) && \ + (defined(LIBCXX_BUILDING_LIBCXXABI) || defined(__APPLE__) || \ + defined(LIBCXXRT)) +#include +#endif + +static_assert(sizeof(std::__libcpp_refstring) == sizeof(const char*), ""); + +namespace std // purposefully not using versioning namespace +{ + +logic_error::logic_error(const string& msg) : __imp_(msg.c_str()) {} + +logic_error::logic_error(const char* msg) : __imp_(msg) {} + +logic_error::logic_error(const logic_error& le) _NOEXCEPT : __imp_(le.__imp_) {} + +logic_error& logic_error::operator=(const logic_error& le) _NOEXCEPT { + __imp_ = le.__imp_; + return *this; +} + +runtime_error::runtime_error(const string& msg) : __imp_(msg.c_str()) {} + +runtime_error::runtime_error(const char* msg) : __imp_(msg) {} + +runtime_error::runtime_error(const runtime_error& re) _NOEXCEPT + : __imp_(re.__imp_) {} + +runtime_error& runtime_error::operator=(const runtime_error& re) _NOEXCEPT { + __imp_ = re.__imp_; + return *this; +} + +#if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX) + +const char* logic_error::what() const _NOEXCEPT { return __imp_.c_str(); } + +const char* runtime_error::what() const _NOEXCEPT { return __imp_.c_str(); } + +logic_error::~logic_error() _NOEXCEPT {} +domain_error::~domain_error() _NOEXCEPT {} +invalid_argument::~invalid_argument() _NOEXCEPT {} +length_error::~length_error() _NOEXCEPT {} +out_of_range::~out_of_range() _NOEXCEPT {} + +runtime_error::~runtime_error() _NOEXCEPT {} +range_error::~range_error() _NOEXCEPT {} +overflow_error::~overflow_error() _NOEXCEPT {} +underflow_error::~underflow_error() _NOEXCEPT {} + +#endif + +} // namespace std diff --git a/libcxx/src/support/runtime/stdexcept_vcruntime.ipp b/libcxx/src/support/runtime/stdexcept_vcruntime.ipp new file mode 100644 index 000000000000..94eed465ae97 --- /dev/null +++ b/libcxx/src/support/runtime/stdexcept_vcruntime.ipp @@ -0,0 +1,16 @@ +//===------------------- stdexcept_vcruntime.ipp --------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_ABI_VCRUNTIME +#error This file may only be used when defering to vcruntime +#endif + +namespace std { +logic_error::logic_error(std::string const& s) : exception(s.c_str()) {} +runtime_error::runtime_error(std::string const& s) : exception(s.c_str()) {} +} // namespace std