diff options
author | Daniel Dunbar <daniel@zuster.org> | 2012-09-14 23:15:56 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2012-09-14 23:15:56 +0000 |
commit | 9affb245a5c601524267661afa64ccd78a4b11b2 (patch) | |
tree | 5f1fec4e41091c9477a465d0f0c598a85d952ca2 | |
parent | 25cc99fa44bedf9ae771d33a5bc317758d9551f8 (diff) | |
download | bcm5719-llvm-9affb245a5c601524267661afa64ccd78a4b11b2.tar.gz bcm5719-llvm-9affb245a5c601524267661afa64ccd78a4b11b2.zip |
formatted_raw_ostream: Fix a serious bug in tell().
- The current_pos function is supposed to return all the written bytes, not the
current position of the underlying stream.
- This caused tell() to be broken whenever the underlying stream had buffered
content.
llvm-svn: 163948
-rw-r--r-- | llvm/include/llvm/Support/FormattedStream.h | 7 | ||||
-rw-r--r-- | llvm/unittests/Support/CMakeLists.txt | 3 | ||||
-rw-r--r-- | llvm/unittests/Support/formatted_raw_ostream_test.cpp | 33 |
3 files changed, 39 insertions, 4 deletions
diff --git a/llvm/include/llvm/Support/FormattedStream.h b/llvm/include/llvm/Support/FormattedStream.h index 58a18851687..61a219f676e 100644 --- a/llvm/include/llvm/Support/FormattedStream.h +++ b/llvm/include/llvm/Support/FormattedStream.h @@ -60,9 +60,10 @@ namespace llvm /// current_pos - Return the current position within the stream, /// not counting the bytes currently in the buffer. virtual uint64_t current_pos() const { - // This has the same effect as calling TheStream.current_pos(), - // but that interface is private. - return TheStream->tell() - TheStream->GetNumBytesInBuffer(); + // Our current position in the stream is all the contents which have been + // written to the underlying stream (*not* the current position of the + // underlying stream). + return TheStream->tell(); } /// ComputeColumn - Examine the given output buffer and figure out which diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index 3b9bf843703..06c5343cd65 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -18,10 +18,11 @@ add_llvm_unittest(SupportTests ManagedStatic.cpp MathExtrasTest.cpp Path.cpp - raw_ostream_test.cpp RegexTest.cpp SwapByteOrderTest.cpp TimeValue.cpp ValueHandleTest.cpp YAMLParserTest.cpp + formatted_raw_ostream.cpp + raw_ostream_test.cpp ) diff --git a/llvm/unittests/Support/formatted_raw_ostream_test.cpp b/llvm/unittests/Support/formatted_raw_ostream_test.cpp new file mode 100644 index 00000000000..4725cedc211 --- /dev/null +++ b/llvm/unittests/Support/formatted_raw_ostream_test.cpp @@ -0,0 +1,33 @@ +//===- llvm/unittest/Support/formatted_raw_ostream_test.cpp ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/FormattedStream.h" + +using namespace llvm; + +namespace { + +TEST(formatted_raw_ostreamTest, Test_Tell) { + // Check offset when underlying stream has buffer contents. + SmallString<128> A; + raw_svector_ostream B(A); + formatted_raw_ostream C(B); + char tmp[100] = ""; + + for (unsigned i = 0; i != 3; ++i) { + C.write(tmp, 100); + + EXPECT_EQ(100*(i+1), (unsigned) C.tell()); + } +} + +} |