Fix ABI compatibility of `<stdexcept>` with VCRuntime.

Summary:
Currently, libc++'s `<stdexcept>` 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 `<stdexcept>` types.
* libc++'s `<stdexcept>` constructors and assignment operators may have different manglings than `vcruntimes`.

This patch makes libc++'s declarations in `<stdexcept>` 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
This commit is contained in:
Eric Fiselier 2019-03-06 20:31:57 +00:00
parent 5ced596198
commit 2710d8e105
4 changed files with 114 additions and 83 deletions

View File

@ -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

View File

@ -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 <cxxabi.h>
#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

View File

@ -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 <cxxabi.h>
#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

View File

@ -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