From 1a6493b4e08c7789138b8087a44bcec53b9cd3fa Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 1 Feb 2018 03:55:27 +0000 Subject: [PATCH] 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. llvm-svn: 323945 --- libcxx/include/ios | 3 ++ libcxx/include/streambuf | 3 ++ .../fstreams/filebuf/traits_mismatch.fail.cpp | 24 +++++++++++++++ .../fstreams/traits_mismatch.fail.cpp | 25 ++++++++++++++++ .../input.streams/traits_mismatch.fail.cpp | 29 +++++++++++++++++++ .../output.streams/traits_mismatch.fail.cpp | 29 +++++++++++++++++++ .../string.streams/traits_mismatch.fail.cpp | 26 +++++++++++++++++ 7 files changed, 139 insertions(+) create mode 100644 libcxx/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp create mode 100644 libcxx/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp create mode 100644 libcxx/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp create mode 100644 libcxx/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp create mode 100644 libcxx/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp diff --git a/libcxx/include/ios b/libcxx/include/ios index 61d00b90ee04..0bffa571e92b 100644 --- a/libcxx/include/ios +++ b/libcxx/include/ios @@ -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. diff --git a/libcxx/include/streambuf b/libcxx/include/streambuf index ea64f5780459..e409d2106070 100644 --- a/libcxx/include/streambuf +++ b/libcxx/include/streambuf @@ -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: diff --git a/libcxx/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp b/libcxx/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp new file mode 100644 index 000000000000..84045cf3c94f --- /dev/null +++ b/libcxx/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.fail.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// template> +// class basic_filebuf; +// +// The char type of the stream and the char_type of the traits have to match + +#include + +int main() +{ + std::basic_filebuf > f; +// expected-error-re@streambuf:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}} +} + diff --git a/libcxx/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp b/libcxx/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp new file mode 100644 index 000000000000..dcb7b1f64480 --- /dev/null +++ b/libcxx/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.fail.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// template > +// class basic_fstream + +// The char type of the stream and the char_type of the traits have to match + +#include + +int main() +{ + std::basic_fstream > f; +// expected-error-re@ios:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}} +// expected-error-re@streambuf:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}} +} + diff --git a/libcxx/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp b/libcxx/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp new file mode 100644 index 000000000000..a459ec4fb71b --- /dev/null +++ b/libcxx/test/libcxx/input.output/iostream.format/input.streams/traits_mismatch.fail.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// template > +// class basic_istream; + +// The char type of the stream and the char_type of the traits have to match + +#include +#include +#include + +struct test_istream + : public std::basic_istream > {}; + + +int main() +{ +// expected-error-re@ios:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}} +} + diff --git a/libcxx/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp b/libcxx/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp new file mode 100644 index 000000000000..fab0dd436577 --- /dev/null +++ b/libcxx/test/libcxx/input.output/iostream.format/output.streams/traits_mismatch.fail.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// template > +// class basic_ostream; + +// The char type of the stream and the char_type of the traits have to match + +#include +#include +#include + +struct test_ostream + : public std::basic_ostream > {}; + + +int main() +{ +// expected-error-re@ios:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}} +} + diff --git a/libcxx/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp b/libcxx/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp new file mode 100644 index 000000000000..f048cae0c565 --- /dev/null +++ b/libcxx/test/libcxx/input.output/string.streams/traits_mismatch.fail.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// template, +// class Allocator = allocator> +// class basic_stringbuf; +// +// The char type of the stream and the char_type of the traits have to match + +#include + +int main() +{ + std::basic_stringbuf > sb; +// expected-error-re@streambuf:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}} +// expected-error-re@string:* {{static_assert failed{{.*}} "traits_type::char_type must be the same type as CharT"}} +} +