Louis Dionne 396145d0da [libc++] Fix error flags and exceptions propagated from input stream operations
Summary:
This is a re-application of r357533 and r357531. They had been reverted
because we thought the commits broke the LLDB data formatters, but it
turns out this was because only r357531 had been included in the CI
run.

Before this patch, we would only ever throw an exception if the badbit
was set on the stream. The Standard is currently very unclear on how
exceptions should be propagated and what error flags should be set by
the input stream operations. This commit changes libc++ to behave under
a different (but valid) interpretation of the Standard. This interpretation
of the Standard matches what other implementations are doing.

This effectively implements the wording in p1264r0. It hasn't been voted
into the Standard yet, however there is wide agreement that the fix is
correct and it's just a matter of time before the fix is standardized.

PR21586
PR15949
rdar://problem/15347558

Reviewers: mclow.lists, EricWF

Subscribers: christof, dexonsmith, cfe-commits

Differential Revision: https://reviews.llvm.org/D49863

llvm-svn: 357775
2019-04-05 16:33:37 +00:00

158 lines
3.9 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <string>
// template<class charT, class traits, class Allocator>
// basic_istream<charT,traits>&
// operator>>(basic_istream<charT,traits>& is,
// basic_string<charT,traits,Allocator>& str);
#include <string>
#include <sstream>
#include <cassert>
#include "min_allocator.h"
#include "test_macros.h"
int main(int, char**)
{
{
std::istringstream in("a bc defghij");
std::string s("initial text");
in >> s;
assert(in.good());
assert(s == "a");
assert(in.peek() == ' ');
in >> s;
assert(in.good());
assert(s == "bc");
assert(in.peek() == ' ');
in.width(3);
in >> s;
assert(in.good());
assert(s == "def");
assert(in.peek() == 'g');
in >> s;
assert(in.eof());
assert(s == "ghij");
in >> s;
assert(in.fail());
}
{
std::wistringstream in(L"a bc defghij");
std::wstring s(L"initial text");
in >> s;
assert(in.good());
assert(s == L"a");
assert(in.peek() == L' ');
in >> s;
assert(in.good());
assert(s == L"bc");
assert(in.peek() == L' ');
in.width(3);
in >> s;
assert(in.good());
assert(s == L"def");
assert(in.peek() == L'g');
in >> s;
assert(in.eof());
assert(s == L"ghij");
in >> s;
assert(in.fail());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
std::stringbuf sb;
std::istream is(&sb);
is.exceptions(std::ios::failbit);
bool threw = false;
try {
std::string s;
is >> s;
} catch (std::ios::failure const&) {
threw = true;
}
assert(!is.bad());
assert(is.fail());
assert(is.eof());
assert(threw);
}
{
std::stringbuf sb;
std::istream is(&sb);
is.exceptions(std::ios::eofbit);
bool threw = false;
try {
std::string s;
is >> s;
} catch (std::ios::failure const&) {
threw = true;
}
assert(!is.bad());
assert(is.fail());
assert(is.eof());
assert(threw);
}
#endif // TEST_HAS_NO_EXCEPTIONS
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
std::istringstream in("a bc defghij");
S s("initial text");
in >> s;
assert(in.good());
assert(s == "a");
assert(in.peek() == ' ');
in >> s;
assert(in.good());
assert(s == "bc");
assert(in.peek() == ' ');
in.width(3);
in >> s;
assert(in.good());
assert(s == "def");
assert(in.peek() == 'g');
in >> s;
assert(in.eof());
assert(s == "ghij");
in >> s;
assert(in.fail());
}
{
typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
std::wistringstream in(L"a bc defghij");
S s(L"initial text");
in >> s;
assert(in.good());
assert(s == L"a");
assert(in.peek() == L' ');
in >> s;
assert(in.good());
assert(s == L"bc");
assert(in.peek() == L' ');
in.width(3);
in >> s;
assert(in.good());
assert(s == L"def");
assert(in.peek() == L'g');
in >> s;
assert(in.eof());
assert(s == L"ghij");
in >> s;
assert(in.fail());
}
#endif
return 0;
}