[libcxx] r271237 - Make string_view work with -fno-exceptions and get tests passing.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Mon May 30 16:53:22 PDT 2016


Author: ericwf
Date: Mon May 30 18:53:19 2016
New Revision: 271237

URL: http://llvm.org/viewvc/llvm-project?rev=271237&view=rev
Log:
Make string_view work with -fno-exceptions and get tests passing.

Modified:
    libcxx/trunk/include/experimental/string_view
    libcxx/trunk/test/std/experimental/string.view/string.view.access/at.pass.cpp
    libcxx/trunk/test/std/experimental/string.view/string.view.comparison/opeq.string_view.string.pass.cpp
    libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.pointer_size.pass.cpp
    libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv.pass.cpp
    libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_pointer_size.pass.cpp
    libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_size_size.pass.cpp
    libcxx/trunk/test/std/experimental/string.view/string.view.ops/copy.pass.cpp
    libcxx/trunk/test/std/experimental/string.view/string.view.ops/substr.pass.cpp

Modified: libcxx/trunk/include/experimental/string_view
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/experimental/string_view?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/include/experimental/string_view (original)
+++ libcxx/trunk/include/experimental/string_view Mon May 30 18:53:19 2016
@@ -180,6 +180,7 @@ namespace std {
 #include <algorithm>
 #include <iterator>
 #include <ostream>
+#include <stdexcept>
 #include <iomanip>
 
 #include <__debug>
@@ -280,7 +281,7 @@ _LIBCPP_BEGIN_NAMESPACE_LFTS
         const_reference at(size_type __pos) const
         {
             return __pos >= size()
-                ? (throw out_of_range("string_view::at"), __data[0])
+                ? (__libcpp_throw(out_of_range("string_view::at")), __data[0])
                 : __data[__pos];
         }
 
@@ -351,7 +352,7 @@ _LIBCPP_BEGIN_NAMESPACE_LFTS
         size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
         {
             if ( __pos > size())
-                throw out_of_range("string_view::copy");
+                __libcpp_throw(out_of_range("string_view::copy"));
             size_type __rlen = _VSTD::min( __n, size() - __pos );
             _VSTD::copy_n(begin() + __pos, __rlen, __s );
             return __rlen;
@@ -365,7 +366,7 @@ _LIBCPP_BEGIN_NAMESPACE_LFTS
 //             size_type __rlen = _VSTD::min( __n, size() - __pos );
 //             return basic_string_view(data() + __pos, __rlen);
             return __pos > size()
-                ? throw out_of_range("string_view::substr")
+                ? (__libcpp_throw((out_of_range("string_view::substr"))), basic_string_view())
                 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
         }
 

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.access/at.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.access/at.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.access/at.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.access/at.pass.cpp Mon May 30 18:53:19 2016
@@ -10,7 +10,7 @@
 // NOTE: Older versions of clang have a bug where they fail to evalute
 // string_view::at as a constant expression.
 // XFAIL: clang-3.4, clang-3.3
-// XFAIL: libcpp-no-exceptions
+
 
 // <string_view>
 
@@ -20,6 +20,8 @@
 #include <stdexcept>
 #include <cassert>
 
+#include "test_macros.h"
+
 template <typename CharT>
 void test ( const CharT *s, size_t len ) {
     std::experimental::basic_string_view<CharT> sv ( s, len );
@@ -27,11 +29,13 @@ void test ( const CharT *s, size_t len )
     for ( size_t i = 0; i < len; ++i ) {
         assert (  sv.at(i) == s[i] );
         assert ( &sv.at(i) == s + i );
-        }
+    }
 
+#ifndef TEST_HAS_NO_EXCEPTIONS
     try { sv.at(len); } catch ( const std::out_of_range & ) { return ; }
     assert ( false );
-    }
+#endif
+}
     
 int main () {
     test ( "ABCDE", 5 );
@@ -40,7 +44,7 @@ int main () {
     test ( L"ABCDE", 5 );
     test ( L"a", 1 );
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     test ( u"ABCDE", 5 );
     test ( u"a", 1 );
 
@@ -48,7 +52,7 @@ int main () {
     test ( U"a", 1 );
 #endif
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     {
     constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
     static_assert ( sv.length() ==  2,  "" );

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.comparison/opeq.string_view.string.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.comparison/opeq.string_view.string.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.comparison/opeq.string_view.string.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.comparison/opeq.string_view.string.pass.cpp Mon May 30 18:53:19 2016
@@ -7,7 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++98, c++03, c++11
 // <string>
 
 // template<class charT, class traits, class Allocator>

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.pointer_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.pointer_size.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.pointer_size.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.pointer_size.pass.cpp Mon May 30 18:53:19 2016
@@ -7,7 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// XFAIL: libcpp-no-exceptions
 // <string_view>
 
 // constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
@@ -15,6 +14,7 @@
 #include <experimental/string_view>
 #include <cassert>
 
+#include "test_macros.h"
 #include "constexpr_char_traits.hpp"
 
 int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
@@ -22,11 +22,19 @@ int sign ( int x ) { return x > 0 ? 1 :
 template<typename CharT>
 void test1 ( std::experimental::basic_string_view<CharT> sv1, 
              size_t pos1, size_t n1, const CharT *s, int expected ) {
-    try {
+    if (pos1 > sv1.size()) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+        try {
+            sv1.compare(pos1, n1, s);
+            assert(false);
+        } catch (const std::out_of_range&) {
+        } catch (...) {
+            assert(false);
+        }
+#endif
+    } else {
         assert(sign(sv1.compare(pos1, n1, s)) == sign(expected));
-        assert(pos1 <= sv1.size());
     }
-    catch (const std::out_of_range&) { assert(pos1 > sv1.size()); }
 }
 
 template<typename CharT>
@@ -391,7 +399,7 @@ int main()
     test(L"abcdefghijklmnopqrst", 0, -1, L"abcdefghijklmnopqrst", 0);
     }
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     {
     test(U"", 0, 0, U"", 0);
     test(U"", 0, 0, U"abcde", -5);
@@ -431,7 +439,7 @@ int main()
     }
 #endif
 
-#if _LIBCPP_STD_VER > 11
+#if TEST_STD_VER > 11
     {
     typedef std::experimental::basic_string_view<char, constexpr_char_traits<char>> SV;
     constexpr SV  sv1;

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv.pass.cpp Mon May 30 18:53:19 2016
@@ -7,8 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-
-// XFAIL: libcpp-no-exceptions
 // <string_view>
 
 // constexpr int compare(size_type pos1, size_type n1, basic_string_view str) const;
@@ -16,6 +14,7 @@
 #include <experimental/string_view>
 #include <cassert>
 
+#include "test_macros.h"
 #include "constexpr_char_traits.hpp"
 
 int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
@@ -24,19 +23,25 @@ template<typename CharT>
 void test1 ( std::experimental::basic_string_view<CharT> sv1, size_t pos1, size_t n1,
             std::experimental::basic_string_view<CharT> sv2, int expected ) {
 
-    try
-    {
+    if (pos1 > sv1.size()) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+        try {
+            sv1.compare(pos1, n1, sv2);
+            assert(false);
+        } catch (const std::out_of_range&) {
+        } catch (...) {
+            assert(false);
+        }
+#endif
+    } else {
         assert ( sign( sv1.compare(pos1, n1, sv2)) == sign(expected));
-        assert(pos1 <= sv1.size());
     }
-    catch (const std::out_of_range&) { assert(pos1 > sv1.size()); }
 }
 
 
 template<typename CharT>
 void test ( const CharT *s1, size_t pos1, size_t n1, const CharT  *s2, int expected ) {
     typedef std::experimental::basic_string_view<CharT> string_view_t;
-    
     string_view_t sv1 ( s1 );
     string_view_t sv2 ( s2 );
     test1(sv1, pos1, n1, sv2, expected);
@@ -370,7 +375,7 @@ int main () {
     test(L"ABCde", 2, 4, L"abcde", -1);
     }
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     {
     test(u"abcde", 5, 1, u"", 0);
     test(u"abcde", 2, 4, u"", 3);
@@ -386,7 +391,7 @@ int main () {
     }
 #endif
 
-#if _LIBCPP_STD_VER > 11
+#if TEST_STD_VER > 11
     {
     typedef std::experimental::basic_string_view<char, constexpr_char_traits<char>> SV;
     constexpr SV  sv1 { "abcde", 5 };

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_pointer_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_pointer_size.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_pointer_size.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_pointer_size.pass.cpp Mon May 30 18:53:19 2016
@@ -7,8 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-
-// XFAIL: libcpp-no-exceptions
 // <string_view>
 
 // constexpr int compare(size_type pos1, size_type n1,
@@ -17,6 +15,7 @@
 #include <experimental/string_view>
 #include <cassert>
 
+#include "test_macros.h"
 #include "constexpr_char_traits.hpp"
 
 int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
@@ -25,13 +24,21 @@ template<typename CharT>
 void test1 ( std::experimental::basic_string_view<CharT> sv1, size_t pos1, size_t n1,
              const CharT *s2, size_t n2,
              int expected ) {
-
-    try
-    {
-        assert ( sign( sv1.compare(pos1, n1, s2, n2)) == sign(expected));
-        assert(pos1 <= sv1.size());
+    if (pos1 > sv1.size()) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+        try {
+            sv1.compare(pos1, n1, s2, n2);
+            assert(false);
+        } catch (const std::out_of_range&) {
+            return;
+        } catch (...) {
+            assert(false);
+        }
+#endif
+    } else {
+        assert(sign(sv1.compare(pos1, n1, s2, n2)) == sign(expected));
     }
-    catch (const std::out_of_range&) { assert(pos1 > sv1.size()); }
+
 }
 
 
@@ -40,7 +47,6 @@ void test ( const CharT *s1, size_t pos1
             const CharT *s2, size_t n2, 
             int expected ) {
     typedef std::experimental::basic_string_view<CharT> string_view_t;
-    
     string_view_t sv1 ( s1 );
     test1 (sv1, pos1, n1, s2, n2, expected);
 }
@@ -1319,7 +1325,7 @@ int main () {
     test(L"abcdefghijklmnopqrst", 10, 0, L"abcdefghij", 10, -10);
     }
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     {
     test(U"", 0, 0, U"abcde", 0, 0);
     test(U"", 0, 0, U"abcde", 1, -1);
@@ -1337,7 +1343,7 @@ int main () {
     }
 #endif
 
-#if _LIBCPP_STD_VER > 11
+#if TEST_STD_VER > 11
     {
     typedef std::experimental::basic_string_view<char, constexpr_char_traits<char>> SV;
     constexpr SV  sv1;

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_size_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_size_size.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_size_size.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.ops/compare.size_size_sv_size_size.pass.cpp Mon May 30 18:53:19 2016
@@ -7,8 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-
-// XFAIL: libcpp-no-exceptions
 // <string_view>
 
 // constexpr int compare(size_type pos1, size_type n1, basic_string_view str,
@@ -17,6 +15,7 @@
 #include <experimental/string_view>
 #include <cassert>
 
+#include "test_macros.h"
 #include "constexpr_char_traits.hpp"
 
 int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
@@ -26,13 +25,19 @@ void test1 ( std::experimental::basic_st
              std::experimental::basic_string_view<CharT> sv2, size_t pos2, size_t n2,
              int expected ) {
 
-    try
-    {
-        assert ( sign( sv1.compare(pos1, n1, sv2, pos2, n2)) == sign(expected));
-        assert(pos1 <= sv1.size());
-        assert(pos2 <= sv2.size());
+    if (pos1 > sv1.size() || pos2 > sv2.size()) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+        try {
+            sv1.compare(pos1, n1, sv2, pos2, n2);
+            assert(false);
+        } catch (const std::out_of_range&) {
+        } catch (...) {
+            assert(false);
+        }
+#endif
+    } else {
+        assert (sign( sv1.compare(pos1, n1, sv2, pos2, n2)) == sign(expected));
     }
-    catch (const std::out_of_range&) { assert(pos1 > sv1.size() || pos2 > sv2.size()); }
 }
 
 
@@ -5816,7 +5821,7 @@ int main () {
     test(L"ABCde", 2, 4, L"abcde", 2, 4, -1);
     }
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     {
     test(u"abcde", 5, 1, u"", 0, 0, 0);
     test(u"abcde", 2, 4, u"", 0, 0, 3);
@@ -5832,7 +5837,7 @@ int main () {
     }
 #endif
 
-#if _LIBCPP_STD_VER > 11
+#if TEST_STD_VER > 11
     {
     typedef std::experimental::basic_string_view<char, constexpr_char_traits<char>> SV;
     constexpr SV  sv1 { "abcde", 5 };

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.ops/copy.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.ops/copy.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.ops/copy.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.ops/copy.pass.cpp Mon May 30 18:53:19 2016
@@ -7,8 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-
-// XFAIL: libcpp-no-exceptions
 // <string_view>
 
 // size_type copy(charT* s, size_type n, size_type pos = 0) const;
@@ -23,21 +21,31 @@
 #include <experimental/string_view>
 #include <cassert>
 
+#include "test_macros.h"
+
 template<typename CharT>
 void test1 ( std::experimental::basic_string_view<CharT> sv, size_t n, size_t pos ) {
     const size_t rlen = std::min ( n, sv.size() - pos );
 
     CharT *dest1 = new CharT [rlen + 1];    dest1[rlen] = 0;
     CharT *dest2 = new CharT [rlen + 1];    dest2[rlen] = 0;
-    
-    try {
+
+    if (pos > sv.size()) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+        try {
+            sv.copy(dest1, n, pos);
+            assert(false);
+        } catch (const std::out_of_range&) {
+        } catch (...) {
+            assert(false);
+        }
+#endif
+    } else {
         sv.copy(dest1, n, pos);
         std::copy_n(sv.begin() + pos, rlen, dest2);
-
         for ( size_t i = 0; i <= rlen; ++i )
             assert ( dest1[i] == dest2[i] );
-        }
-    catch ( const std::out_of_range & ) { assert ( pos > sv.size()); }
+    }
     delete [] dest1;
     delete [] dest2;    
 }
@@ -79,7 +87,7 @@ int main () {
     test ( L"a" );
     test ( L"" );
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
     test ( u"ABCDE" );
     test ( u"a" );

Modified: libcxx/trunk/test/std/experimental/string.view/string.view.ops/substr.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/string.view/string.view.ops/substr.pass.cpp?rev=271237&r1=271236&r2=271237&view=diff
==============================================================================
--- libcxx/trunk/test/std/experimental/string.view/string.view.ops/substr.pass.cpp (original)
+++ libcxx/trunk/test/std/experimental/string.view/string.view.ops/substr.pass.cpp Mon May 30 18:53:19 2016
@@ -7,8 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-
-// XFAIL: libcpp-no-exceptions
 // <string_view>
 
 // constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
@@ -20,16 +18,28 @@
 #include <experimental/string_view>
 #include <cassert>
 
+#include "test_macros.h"
+
 template<typename CharT>
 void test1 ( std::experimental::basic_string_view<CharT> sv, size_t n, size_t pos ) {
-    try {
+    if (pos > sv.size()) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+        try {
+            std::experimental::basic_string_view<CharT> sv1 = sv.substr(pos, n);
+            assert(false);
+        } catch (const std::out_of_range&) {
+            return;
+        } catch (...) {
+            assert(false);
+        }
+#endif
+    } else {
         std::experimental::basic_string_view<CharT> sv1 = sv.substr(pos, n);
         const size_t rlen = std::min ( n, sv.size() - pos );
         assert ( sv1.size() == rlen );
         for ( size_t i = 0; i <= rlen; ++i )
             assert ( sv[pos+i] == sv1[i] );
-        }
-    catch ( const std::out_of_range & ) { assert ( pos > sv.size()); }
+    }
 }
 
 
@@ -68,7 +78,7 @@ int main () {
     test ( L"a" );
     test ( L"" );
 
-#if __cplusplus >= 201103L
+#if TEST_STD_VER >= 11
     test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
     test ( u"ABCDE" );
     test ( u"a" );
@@ -80,7 +90,7 @@ int main () {
     test ( U"" );
 #endif
     
-#if _LIBCPP_STD_VER > 11
+#if TEST_STD_VER > 11
     {
     constexpr std::experimental::string_view sv1 { "ABCDE", 5 };
 




More information about the cfe-commits mailing list