[libc++] Fix modules build - Rework __refstring definition

Summary:
`__libcpp_refstring` currently has two different definitions. First there is the complete definition in `<__refstring>` but there is also a second in  `<stdexcept>`.  The historical reason for this split is because both libc++ and libc++abi need to see the inline definitions of __libcpp_refstrings methods, but the `<stdexcept>` header doesn't.  However this is an ODR violation and breaks the modules build.

This patch fixes the issue by creating a single class definition in `<stdexcept>` and changing `<__refstring>` to contain only the inline method definitions. This way both `libcxx/src/stdexcept.cpp` and `libcxxabi/src/stdexcept.cpp` see the same declaration in `<stdexcept>` and definitions in `<__refstring>`

Reviewers: mclow.lists, EricWF

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25603

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@285100 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2016-10-25 19:33:14 +00:00
parent c4a7e9177a
commit 6979a42e44
3 changed files with 106 additions and 111 deletions

View File

@@ -11,6 +11,7 @@
#define _LIBCPP___REFSTRING
#include <__config>
#include <stdexcept>
#include <cstddef>
#include <cstring>
#ifdef __APPLE__
@@ -20,39 +21,28 @@
_LIBCPP_BEGIN_NAMESPACE_STD
class _LIBCPP_HIDDEN __libcpp_refstring
{
private:
const char* str_;
namespace __refstring_imp { namespace {
typedef int count_t;
struct _Rep_base
{
struct _Rep_base {
std::size_t len;
std::size_t cap;
count_t count;
};
static
_Rep_base*
rep_from_data(const char *data_) _NOEXCEPT
{
inline _Rep_base* rep_from_data(const char *data_) noexcept {
char *data = const_cast<char *>(data_);
return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base));
}
static
char *
data_from_rep(_Rep_base *rep) _NOEXCEPT
{
inline char * data_from_rep(_Rep_base *rep) noexcept {
char *data = reinterpret_cast<char *>(rep);
return data + sizeof(*rep);
}
#ifdef __APPLE__
static
const char*
compute_gcc_empty_string_storage() _NOEXCEPT
#if defined(__APPLE__)
inline
const char* compute_gcc_empty_string_storage() _NOEXCEPT
{
void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
if (handle == nullptr)
@@ -63,29 +53,21 @@ private:
return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
}
static
inline
const char*
get_gcc_empty_string_storage() _NOEXCEPT
{
static const char* p = compute_gcc_empty_string_storage();
return p;
}
bool
uses_refcount() const
{
return str_ != get_gcc_empty_string_storage();
}
#else
bool
uses_refcount() const
{
return true;
}
#endif
public:
explicit __libcpp_refstring(const char* msg) {
}} // namespace __refstring_imp
using namespace __refstring_imp;
inline
__libcpp_refstring::__libcpp_refstring(const char* msg) {
std::size_t len = strlen(msg);
_Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1));
rep->len = len;
@@ -93,22 +75,24 @@ public:
rep->count = 0;
char *data = data_from_rep(rep);
std::memcpy(data, msg, len + 1);
str_ = data;
__imp_ = data;
}
__libcpp_refstring(const __libcpp_refstring& s) _NOEXCEPT : str_(s.str_)
inline
__libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) _NOEXCEPT
: __imp_(s.__imp_)
{
if (uses_refcount())
__sync_add_and_fetch(&rep_from_data(str_)->count, 1);
if (__uses_refcount())
__sync_add_and_fetch(&rep_from_data(__imp_)->count, 1);
}
__libcpp_refstring& operator=(const __libcpp_refstring& s) _NOEXCEPT
{
bool adjust_old_count = uses_refcount();
struct _Rep_base *old_rep = rep_from_data(str_);
str_ = s.str_;
if (uses_refcount())
__sync_add_and_fetch(&rep_from_data(str_)->count, 1);
inline
__libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) _NOEXCEPT {
bool adjust_old_count = __uses_refcount();
struct _Rep_base *old_rep = rep_from_data(__imp_);
__imp_ = s.__imp_;
if (__uses_refcount())
__sync_add_and_fetch(&rep_from_data(__imp_)->count, 1);
if (adjust_old_count)
{
if (__sync_add_and_fetch(&old_rep->count, count_t(-1)) < 0)
@@ -119,20 +103,24 @@ public:
return *this;
}
~__libcpp_refstring()
{
if (uses_refcount())
{
_Rep_base* rep = rep_from_data(str_);
if (__sync_add_and_fetch(&rep->count, count_t(-1)) < 0)
{
inline
__libcpp_refstring::~__libcpp_refstring() {
if (__uses_refcount()) {
_Rep_base* rep = rep_from_data(__imp_);
if (__sync_add_and_fetch(&rep->count, count_t(-1)) < 0) {
::operator delete(rep);
}
}
}
const char* c_str() const _NOEXCEPT {return str_;}
};
inline
bool __libcpp_refstring::__uses_refcount() const {
#ifdef __APPLE__
return __imp_ != get_gcc_empty_string_storage();
#else
return true;
#endif
}
_LIBCPP_END_NAMESPACE_STD

View File

@@ -53,17 +53,23 @@ public:
#pragma GCC system_header
#endif
#ifndef _LIBCPP___REFSTRING
_LIBCPP_BEGIN_NAMESPACE_STD
class _LIBCPP_HIDDEN __libcpp_refstring {
#ifdef __clang__
const char *__imp_ __attribute__((__unused__)); // only clang emits a warning
#else
class _LIBCPP_HIDDEN __libcpp_refstring
{
const char* __imp_;
#endif
bool __uses_refcount() const;
public:
explicit __libcpp_refstring(const char* msg);
__libcpp_refstring(const __libcpp_refstring& s) _NOEXCEPT;
__libcpp_refstring& operator=(const __libcpp_refstring& s) _NOEXCEPT;
~__libcpp_refstring();
const char* c_str() const _NOEXCEPT {return __imp_;}
};
_LIBCPP_END_NAMESPACE_STD
#endif
namespace std // purposefully not using versioning namespace
{

View File

@@ -7,11 +7,11 @@
//
//===----------------------------------------------------------------------===//
#include "__refstring"
#include "stdexcept"
#include "new"
#include "string"
#include "system_error"
#include "__refstring"
/* For _LIBCPPABI_VERSION */
#if defined(LIBCXX_BUILDING_LIBCXXABI) || defined(__APPLE__) || defined(LIBCXXRT)
@@ -20,6 +20,7 @@
static_assert(sizeof(std::__libcpp_refstring) == sizeof(const char *), "");
namespace std // purposefully not using versioning namespace
{