Add RawStringOstream for slightly simpler streaming to strings (#4817)

This adds a RawStringOstream. Versus TestRawOstream, which is
consolidated over to RawStringOstream, it uses a string for storage
instead of a vector, mainly to support move-to-string semantics. Versus
llvm::raw_string_ostream, it owns the string and supports pwrite (which
is needed for driver and its fd_ostream compatibility requirement).

This converts most uses of llvm::raw_string_ostream, leaving behind a
few in InstNamer that explicitly cannot own the string, such as:

```
     llvm::raw_string_ostream(name)
          << "_" << tree.tokens().GetColumnNumber(token);
```

I have this as its own library so that it can use CHECK.

Yes this doesn't save much code, but it's code we repeatedly write.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Jon Ross-Perkins
2025-01-18 01:11:44 +00:00
committed by GitHub
co-authored by Geoff Romer
parent ef6e035e7d
commit 4c4c4a4d2c
47 changed files with 318 additions and 275 deletions
+3 -4
View File
@@ -9,7 +9,7 @@
#include <forward_list>
#include "testing/base/test_raw_ostream.h"
#include "common/raw_string_ostream.h"
#include "toolchain/base/shared_value_stores.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/diagnostics/mocks.h"
@@ -23,7 +23,6 @@
namespace Carbon::Parse {
namespace {
using ::Carbon::Testing::TestRawOstream;
using ::testing::ElementsAre;
using ::testing::Pair;
@@ -90,7 +89,7 @@ TEST_F(TreeTest, PrintPostorderAsYAML) {
auto [tokens, tree_and_subtrees] =
compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
EXPECT_FALSE(tree_and_subtrees.tree().has_errors());
TestRawOstream print_stream;
RawStringOstream print_stream;
tree_and_subtrees.tree().Print(print_stream);
auto file = Yaml::Sequence(ElementsAre(
@@ -119,7 +118,7 @@ TEST_F(TreeTest, PrintPreorderAsYAML) {
auto [tokens, tree_and_subtrees] =
compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
EXPECT_FALSE(tree_and_subtrees.tree().has_errors());
TestRawOstream print_stream;
RawStringOstream print_stream;
tree_and_subtrees.PrintPreorder(print_stream);
auto param_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(