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

View File

@@ -53,17 +53,23 @@ public:
#pragma GCC system_header #pragma GCC system_header
#endif #endif
#ifndef _LIBCPP___REFSTRING
_LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_BEGIN_NAMESPACE_STD
class _LIBCPP_HIDDEN __libcpp_refstring {
#ifdef __clang__ class _LIBCPP_HIDDEN __libcpp_refstring
const char *__imp_ __attribute__((__unused__)); // only clang emits a warning {
#else
const char* __imp_; 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 _LIBCPP_END_NAMESPACE_STD
#endif
namespace std // purposefully not using versioning namespace namespace std // purposefully not using versioning namespace
{ {

View File

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