[libcxx] r323945 - Add static_asserts to basic_ios and basic_stream_buf to ensure that that the traits match the character type. This is a requirement on the user - now we get consistent failures at compile time instead of incomprehensible error messages or runtime failures. This is also LWG#2994 - not yet adopted.

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 31 19:55:27 PST 2018


Author: marshall
Date: Wed Jan 31 19:55:27 2018
New Revision: 323945

URL: http://llvm.org/viewvc/llvm-project?rev=323945&view=rev
Log:
Add static_asserts to basic_ios and basic_stream_buf to ensure that that the traits match the character type. This is a requirement on the user - now we get consistent failures at compile time instead of incomprehensible error messages or runtime failures. This is also LWG#2994 - not yet adopted.

Added:
    libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/filebuf/
    libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp
    libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp
    libcxx/trunk/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp
    libcxx/trunk/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp
    libcxx/trunk/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp
Modified:
    libcxx/trunk/include/ios
    libcxx/trunk/include/streambuf

Modified: libcxx/trunk/include/ios
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/ios?rev=323945&r1=323944&r2=323945&view=diff
==============================================================================
--- libcxx/trunk/include/ios (original)
+++ libcxx/trunk/include/ios Wed Jan 31 19:55:27 2018
@@ -592,6 +592,9 @@ public:
     typedef typename traits_type::pos_type pos_type;
     typedef typename traits_type::off_type off_type;
 
+    static_assert((is_same<_CharT, typename traits_type::char_type>::value),
+                  "traits_type::char_type must be the same type as CharT");
+
   // __true_value will generate undefined references when linking unless
   // we give it internal linkage.
 

Modified: libcxx/trunk/include/streambuf
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/streambuf?rev=323945&r1=323944&r2=323945&view=diff
==============================================================================
--- libcxx/trunk/include/streambuf (original)
+++ libcxx/trunk/include/streambuf Wed Jan 31 19:55:27 2018
@@ -132,6 +132,9 @@ public:
     typedef typename traits_type::pos_type pos_type;
     typedef typename traits_type::off_type off_type;
 
+    static_assert((is_same<_CharT, typename traits_type::char_type>::value),
+                  "traits_type::char_type must be the same type as CharT");
+
     virtual ~basic_streambuf();
 
     // 27.6.2.2.1 locales:

Added: libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp?rev=323945&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp Wed Jan 31 19:55:27 2018
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <fstream>
+
+// template<class charT, class traits = char_traits<charT>>
+//   class basic_filebuf;
+//
+// The char type of the stream and the char_type of the traits have to match
+
+#include <fstream>
+
+int main()
+{
+	std::basic_filebuf<char, std::char_traits<wchar_t> > f;
+//  expected-error-re at streambuf:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}}
+}
+

Added: libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp?rev=323945&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp Wed Jan 31 19:55:27 2018
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <fstream>
+
+// template <class charT, class traits = char_traits<charT> >
+// class basic_fstream
+
+// The char type of the stream and the char_type of the traits have to match
+
+#include <fstream>
+
+int main()
+{
+	std::basic_fstream<char, std::char_traits<wchar_t> > f;
+//  expected-error-re at ios:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}}
+//  expected-error-re at streambuf:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}}
+}
+

Added: libcxx/trunk/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp?rev=323945&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp Wed Jan 31 19:55:27 2018
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <istream>
+
+// template <class charT, class traits = char_traits<charT> >
+// class basic_istream;
+
+// The char type of the stream and the char_type of the traits have to match
+
+#include <istream>
+#include <type_traits>
+#include <cassert>
+
+struct test_istream
+    : public std::basic_istream<char, std::char_traits<wchar_t> > {};
+
+
+int main()
+{
+//  expected-error-re at ios:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}}
+}
+

Added: libcxx/trunk/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp?rev=323945&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp Wed Jan 31 19:55:27 2018
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <ostream>
+
+// template <class charT, class traits = char_traits<charT> >
+// class basic_ostream;
+
+// The char type of the stream and the char_type of the traits have to match
+
+#include <ostream>
+#include <type_traits>
+#include <cassert>
+
+struct test_ostream
+    : public std::basic_ostream<char, std::char_traits<wchar_t> > {};
+
+
+int main()
+{
+//  expected-error-re at ios:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}}
+}
+

Added: libcxx/trunk/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp?rev=323945&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp Wed Jan 31 19:55:27 2018
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <sstream>
+
+//  template<class charT, class traits = char_traits<charT>,
+//           class Allocator = allocator<charT>>
+//    class basic_stringbuf;
+//
+// The char type of the stream and the char_type of the traits have to match
+
+#include <sstream>
+
+int main()
+{
+	std::basic_stringbuf<char, std::char_traits<wchar_t> > sb;
+//  expected-error-re at streambuf:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}}
+//  expected-error-re at string:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}}
+}
+




More information about the cfe-commits mailing list