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

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 25 12:33:14 PDT 2016


Author: ericwf
Date: Tue Oct 25 14:33:14 2016
New Revision: 285100

URL: http://llvm.org/viewvc/llvm-project?rev=285100&view=rev
Log:
[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

Modified:
    libcxx/trunk/include/__refstring
    libcxx/trunk/include/stdexcept
    libcxx/trunk/src/stdexcept.cpp

Modified: libcxx/trunk/include/__refstring
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__refstring?rev=285100&r1=285099&r2=285100&view=diff
==============================================================================
--- libcxx/trunk/include/__refstring (original)
+++ libcxx/trunk/include/__refstring Tue Oct 25 14:33:14 2016
@@ -11,6 +11,7 @@
 #define _LIBCPP___REFSTRING
 
 #include <__config>
+#include <stdexcept>
 #include <cstddef>
 #include <cstring>
 #ifdef __APPLE__
@@ -20,119 +21,106 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-class _LIBCPP_HIDDEN __libcpp_refstring
-{
-private:
-    const char* str_;
-
-    typedef int count_t;
-
-    struct _Rep_base
-    {
-        std::size_t len;
-        std::size_t cap;
-        count_t     count;
-    };
-
-    static
-    _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
-    {
-        char *data = reinterpret_cast<char *>(rep);
-        return data + sizeof(*rep);
-    }
+namespace __refstring_imp { namespace {
+typedef int count_t;
 
-#ifdef __APPLE__
-    static
-    const char*
-    compute_gcc_empty_string_storage() _NOEXCEPT
-    {
-        void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
-        if (handle == nullptr)
-            return nullptr;
-        void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE");
-        if (sym == nullptr)
-            return nullptr;
-        return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
-    }
-
-    static
-    const char*
-    get_gcc_empty_string_storage() _NOEXCEPT
-    {
-        static const char* p = compute_gcc_empty_string_storage();
-        return p;
-    }
+struct _Rep_base {
+    std::size_t len;
+    std::size_t cap;
+    count_t     count;
+};
 
-    bool
-    uses_refcount() const
-    {
-        return str_ != get_gcc_empty_string_storage();
-    }
-#else
-    bool
-    uses_refcount() const
-    {
-        return true;
-    }
+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));
+}
+
+inline char * data_from_rep(_Rep_base *rep) noexcept {
+    char *data = reinterpret_cast<char *>(rep);
+    return data + sizeof(*rep);
+}
+
+#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)
+        return nullptr;
+    void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE");
+    if (sym == nullptr)
+        return nullptr;
+    return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
+}
+
+inline
+const char*
+get_gcc_empty_string_storage() _NOEXCEPT
+{
+    static const char* p = compute_gcc_empty_string_storage();
+    return p;
+}
 #endif
 
-public:
-    explicit __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;
-        rep->cap = len;
-        rep->count = 0;
-        char *data = data_from_rep(rep);
-        std::memcpy(data, msg, len + 1);
-        str_ = data;
-    }
+}} // namespace __refstring_imp
 
-    __libcpp_refstring(const __libcpp_refstring& s) _NOEXCEPT : str_(s.str_)
-    {
-        if (uses_refcount())
-            __sync_add_and_fetch(&rep_from_data(str_)->count, 1);
-    }
+using namespace __refstring_imp;
 
-    __libcpp_refstring& operator=(const __libcpp_refstring& s) _NOEXCEPT
+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;
+    rep->cap = len;
+    rep->count = 0;
+    char *data = data_from_rep(rep);
+    std::memcpy(data, msg, len + 1);
+    __imp_ = data;
+}
+
+inline
+__libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) _NOEXCEPT
+    : __imp_(s.__imp_)
+{
+    if (__uses_refcount())
+        __sync_add_and_fetch(&rep_from_data(__imp_)->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)
     {
-        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);
-        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)
-            {
-                ::operator delete(old_rep);
-            }
+            ::operator delete(old_rep);
         }
-        return *this;
     }
+    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)
-            {
-                ::operator delete(rep);
-            }
+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
 

Modified: libcxx/trunk/include/stdexcept
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/stdexcept?rev=285100&r1=285099&r2=285100&view=diff
==============================================================================
--- libcxx/trunk/include/stdexcept (original)
+++ libcxx/trunk/include/stdexcept Tue Oct 25 14:33:14 2016
@@ -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
-    const char *__imp_;
-#endif
+
+class _LIBCPP_HIDDEN __libcpp_refstring
+{
+    const char* __imp_;
+
+    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
 {

Modified: libcxx/trunk/src/stdexcept.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/stdexcept.cpp?rev=285100&r1=285099&r2=285100&view=diff
==============================================================================
--- libcxx/trunk/src/stdexcept.cpp (original)
+++ libcxx/trunk/src/stdexcept.cpp Tue Oct 25 14:33:14 2016
@@ -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
 {
 




More information about the cfe-commits mailing list