Convert most remaining usages to utils::StringStream.

This CL converts most of the remaining Tint usages (leaving out the
fuzzer code and some float_to_string code).

Bug: tint:1686
Change-Id: I4d5cef176c15479250861903870ec5bec0f95b5e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/122002
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2023-02-28 22:22:58 +00:00 committed by Dawn LUCI CQ
parent 7c21fe5d92
commit dee884c925
31 changed files with 87 additions and 40 deletions

View File

@ -293,7 +293,10 @@ libtint_source_set("libtint_demangler_src") {
"demangler.cc", "demangler.cc",
"demangler.h", "demangler.h",
] ]
deps = [ ":libtint_program_src" ] deps = [
":libtint_base_src",
":libtint_program_src",
]
} }
libtint_source_set("libtint_initializer_src") { libtint_source_set("libtint_initializer_src") {

View File

@ -14,6 +14,8 @@
#include "src/tint/ast/test_helper.h" #include "src/tint/ast/test_helper.h"
#include "src/tint/utils/string_stream.h"
namespace tint::ast { namespace tint::ast {
namespace { namespace {
@ -42,7 +44,7 @@ TEST_F(FloatLiteralExpressionTest, SuffixH) {
TEST_F(FloatLiteralExpressionTest, SuffixStringStream) { TEST_F(FloatLiteralExpressionTest, SuffixStringStream) {
auto to_str = [](FloatLiteralExpression::Suffix suffix) { auto to_str = [](FloatLiteralExpression::Suffix suffix) {
std::stringstream ss; utils::StringStream ss;
ss << suffix; ss << suffix;
return ss.str(); return ss.str();
}; };

View File

@ -14,6 +14,8 @@
#include "src/tint/ast/test_helper.h" #include "src/tint/ast/test_helper.h"
#include "src/tint/utils/string_stream.h"
namespace tint::ast { namespace tint::ast {
namespace { namespace {
@ -42,7 +44,7 @@ TEST_F(IntLiteralExpressionTest, SuffixU) {
TEST_F(IntLiteralExpressionTest, SuffixStringStream) { TEST_F(IntLiteralExpressionTest, SuffixStringStream) {
auto to_str = [](IntLiteralExpression::Suffix suffix) { auto to_str = [](IntLiteralExpression::Suffix suffix) {
std::stringstream ss; utils::StringStream ss;
ss << suffix; ss << suffix;
return ss.str(); return ss.str();
}; };

View File

@ -19,6 +19,8 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "src/tint/utils/string_stream.h"
namespace tint::bench { namespace tint::bench {
namespace { namespace {
@ -44,7 +46,7 @@ std::variant<std::vector<T>, Error> ReadFile(const std::string& input_file) {
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
const auto file_size = static_cast<size_t>(ftell(file)); const auto file_size = static_cast<size_t>(ftell(file));
if (0 != (file_size % sizeof(T))) { if (0 != (file_size % sizeof(T))) {
std::stringstream err; utils::StringStream err;
err << "File " << input_file err << "File " << input_file
<< " does not contain an integral number of objects: " << file_size << " does not contain an integral number of objects: " << file_size
<< " bytes in the file, require " << sizeof(T) << " bytes per object"; << " bytes in the file, require " << sizeof(T) << " bytes per object";

View File

@ -37,6 +37,7 @@
#include "src/tint/cmd/helper.h" #include "src/tint/cmd/helper.h"
#include "src/tint/utils/io/command.h" #include "src/tint/utils/io/command.h"
#include "src/tint/utils/string.h" #include "src/tint/utils/string.h"
#include "src/tint/utils/string_stream.h"
#include "src/tint/utils/transform.h" #include "src/tint/utils/transform.h"
#include "src/tint/val/val.h" #include "src/tint/val/val.h"
#include "tint/tint.h" #include "tint/tint.h"
@ -245,7 +246,7 @@ Format infer_format(const std::string& filename) {
std::vector<std::string> split_on_char(std::string list, char c) { std::vector<std::string> split_on_char(std::string list, char c) {
std::vector<std::string> res; std::vector<std::string> res;
std::stringstream str(list); std::istringstream str(list);
while (str.good()) { while (str.good()) {
std::string substr; std::string substr;
getline(str, substr, c); getline(str, substr, c);
@ -1034,7 +1035,7 @@ int main(int argc, const char** argv) {
}}, }},
}; };
auto transform_names = [&] { auto transform_names = [&] {
std::stringstream names; tint::utils::StringStream names;
for (auto& t : transforms) { for (auto& t : transforms) {
names << " " << t.name << std::endl; names << " " << t.name << std::endl;
} }

View File

@ -21,6 +21,7 @@
#include "src/tint/diagnostic/formatter.h" #include "src/tint/diagnostic/formatter.h"
#include "src/tint/diagnostic/printer.h" #include "src/tint/diagnostic/printer.h"
#include "src/tint/utils/compiler_macros.h" #include "src/tint/utils/compiler_macros.h"
#include "src/tint/utils/string_stream.h"
namespace tint { namespace tint {
@ -71,7 +72,7 @@ class InternalCompilerError {
const size_t line_; const size_t line_;
diag::System system_; diag::System system_;
diag::List& diagnostics_; diag::List& diagnostics_;
std::stringstream msg_; utils::StringStream msg_;
}; };
} // namespace tint } // namespace tint

View File

@ -15,6 +15,7 @@
#include "src/tint/demangler.h" #include "src/tint/demangler.h"
#include "src/tint/program.h" #include "src/tint/program.h"
#include "src/tint/utils/string_stream.h"
namespace tint { namespace tint {
namespace { namespace {
@ -29,7 +30,7 @@ Demangler::Demangler() = default;
Demangler::~Demangler() = default; Demangler::~Demangler() = default;
std::string Demangler::Demangle(const SymbolTable& symbols, const std::string& str) const { std::string Demangler::Demangle(const SymbolTable& symbols, const std::string& str) const {
std::stringstream out; utils::StringStream out;
size_t pos = 0; size_t pos = 0;
for (;;) { for (;;) {

View File

@ -20,6 +20,7 @@
#include "src/tint/diagnostic/diagnostic.h" #include "src/tint/diagnostic/diagnostic.h"
#include "src/tint/diagnostic/printer.h" #include "src/tint/diagnostic/printer.h"
#include "src/tint/utils/string_stream.h"
namespace tint::diag { namespace tint::diag {
namespace { namespace {
@ -41,7 +42,7 @@ const char* to_str(Severity severity) {
} }
std::string to_str(const Source::Location& location) { std::string to_str(const Source::Location& location) {
std::stringstream ss; utils::StringStream ss;
if (location.line > 0) { if (location.line > 0) {
ss << location.line; ss << location.line;
if (location.column > 0) { if (location.column > 0) {
@ -75,7 +76,7 @@ struct Formatter::State {
auto str = stream.str(); auto str = stream.str();
if (str.length() > 0) { if (str.length() > 0) {
printer->write(str, style); printer->write(str, style);
std::stringstream reset; utils::StringStream reset;
stream.swap(reset); stream.swap(reset);
} }
} }
@ -95,12 +96,12 @@ struct Formatter::State {
/// repeat queues the character c to be written to the printer n times. /// repeat queues the character c to be written to the printer n times.
/// @param c the character to print `n` times /// @param c the character to print `n` times
/// @param n the number of times to print character `c` /// @param n the number of times to print character `c`
void repeat(char c, size_t n) { std::fill_n(std::ostream_iterator<char>(stream), n, c); } void repeat(char c, size_t n) { stream.repeat(c, n); }
private: private:
Printer* printer; Printer* printer;
diag::Style style; diag::Style style;
std::stringstream stream; utils::StringStream stream;
}; };
Formatter::Formatter() {} Formatter::Formatter() {}

View File

@ -19,6 +19,8 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "src/tint/utils/string_stream.h"
namespace tint::diag { namespace tint::diag {
class List; class List;
@ -73,7 +75,7 @@ class StringPrinter : public Printer {
void write(const std::string& str, const Style&) override; void write(const std::string& str, const Style&) override;
private: private:
std::stringstream stream; utils::StringStream stream;
}; };
} // namespace tint::diag } // namespace tint::diag

View File

@ -19,6 +19,7 @@
#include "src/tint/program_builder.h" #include "src/tint/program_builder.h"
#include "src/tint/utils/compiler_macros.h" #include "src/tint/utils/compiler_macros.h"
#include "src/tint/utils/string_stream.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -254,7 +255,7 @@ TEST_P(NumberF16Test, QuantizeF16) {
float input_value = GetParam().input_value; float input_value = GetParam().input_value;
float quantized_value = GetParam().quantized_value; float quantized_value = GetParam().quantized_value;
std::stringstream ss; utils::StringStream ss;
ss << "input value = " << input_value << ", expected quantized value = " << quantized_value; ss << "input value = " << input_value << ", expected quantized value = " << quantized_value;
SCOPED_TRACE(ss.str()); SCOPED_TRACE(ss.str());
@ -269,7 +270,7 @@ TEST_P(NumberF16Test, BitsRepresentation) {
float input_value = GetParam().input_value; float input_value = GetParam().input_value;
uint16_t representation = GetParam().f16_bit_pattern; uint16_t representation = GetParam().f16_bit_pattern;
std::stringstream ss; utils::StringStream ss;
ss << "input value = " << input_value ss << "input value = " << input_value
<< ", expected binary16 bits representation = " << std::hex << std::showbase << ", expected binary16 bits representation = " << std::hex << std::showbase
<< representation; << representation;
@ -282,7 +283,7 @@ TEST_P(NumberF16Test, FromBits) {
float input_value = GetParam().quantized_value; float input_value = GetParam().quantized_value;
uint16_t representation = GetParam().f16_bit_pattern; uint16_t representation = GetParam().f16_bit_pattern;
std::stringstream ss; utils::StringStream ss;
ss << "binary16 bits representation = " << std::hex << std::showbase << representation ss << "binary16 bits representation = " << std::hex << std::showbase << representation
<< " expected value = " << input_value; << " expected value = " << input_value;
SCOPED_TRACE(ss.str()); SCOPED_TRACE(ss.str());

View File

@ -36,6 +36,7 @@
#include "src/tint/utils/block_allocator.h" #include "src/tint/utils/block_allocator.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/map.h" #include "src/tint/utils/map.h"
#include "src/tint/utils/string_stream.h"
using namespace tint::number_suffixes; // NOLINT using namespace tint::number_suffixes; // NOLINT
@ -695,7 +696,7 @@ DecomposeMemoryAccess::Intrinsic::Intrinsic(ProgramID pid,
: Base(pid, nid), op(o), type(ty), address_space(as), buffer(buf) {} : Base(pid, nid), op(o), type(ty), address_space(as), buffer(buf) {}
DecomposeMemoryAccess::Intrinsic::~Intrinsic() = default; DecomposeMemoryAccess::Intrinsic::~Intrinsic() = default;
std::string DecomposeMemoryAccess::Intrinsic::InternalName() const { std::string DecomposeMemoryAccess::Intrinsic::InternalName() const {
std::stringstream ss; utils::StringStream ss;
switch (op) { switch (op) {
case Op::kLoad: case Op::kLoad:
ss << "intrinsic_load_"; ss << "intrinsic_load_";

View File

@ -32,6 +32,7 @@
#include "src/tint/type/abstract_int.h" #include "src/tint/type/abstract_int.h"
#include "src/tint/utils/reverse.h" #include "src/tint/utils/reverse.h"
#include "src/tint/utils/scoped_assignment.h" #include "src/tint/utils/scoped_assignment.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::transform::DirectVariableAccess); TINT_INSTANTIATE_TYPEINFO(tint::transform::DirectVariableAccess);
TINT_INSTANTIATE_TYPEINFO(tint::transform::DirectVariableAccess::Config); TINT_INSTANTIATE_TYPEINFO(tint::transform::DirectVariableAccess::Config);
@ -687,7 +688,7 @@ struct DirectVariableAccess::State {
// Build an appropriate variant function name. // Build an appropriate variant function name.
// This is derived from the original function name and the pointer parameter // This is derived from the original function name and the pointer parameter
// chains. // chains.
std::stringstream ss; utils::StringStream ss;
ss << ctx.src->Symbols().NameFor(target->Declaration()->name->symbol); ss << ctx.src->Symbols().NameFor(target->Declaration()->name->symbol);
for (auto* param : target->Parameters()) { for (auto* param : target->Parameters()) {
if (auto indices = target_signature.Find(param)) { if (auto indices = target_signature.Find(param)) {
@ -1080,7 +1081,7 @@ struct DirectVariableAccess::State {
/// @returns a name describing the given shape /// @returns a name describing the given shape
std::string AccessShapeName(const AccessShape& shape) { std::string AccessShapeName(const AccessShape& shape) {
std::stringstream ss; utils::StringStream ss;
if (IsPrivateOrFunction(shape.root.address_space)) { if (IsPrivateOrFunction(shape.root.address_space)) {
ss << "F"; ss << "F";

View File

@ -26,6 +26,7 @@
#include "src/tint/utils/compiler_macros.h" #include "src/tint/utils/compiler_macros.h"
#include "src/tint/utils/map.h" #include "src/tint/utils/map.h"
#include "src/tint/utils/math.h" #include "src/tint/utils/math.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::transform::VertexPulling); TINT_INSTANTIATE_TYPEINFO(tint::transform::VertexPulling);
TINT_INSTANTIATE_TYPEINFO(tint::transform::VertexPulling::Config); TINT_INSTANTIATE_TYPEINFO(tint::transform::VertexPulling::Config);
@ -59,7 +60,7 @@ enum class VertexDataType {
/// @param out the std::ostream to write to /// @param out the std::ostream to write to
/// @param format the VertexFormat to write /// @param format the VertexFormat to write
/// @returns out so calls can be chained /// @returns out so calls can be chained
std::ostream& operator<<(std::ostream& out, VertexFormat format) { utils::StringStream& operator<<(utils::StringStream& out, VertexFormat format) {
switch (format) { switch (format) {
case VertexFormat::kUint8x2: case VertexFormat::kUint8x2:
return out << "uint8x2"; return out << "uint8x2";
@ -379,7 +380,7 @@ struct VertexPulling::State {
// Base types must match between the vertex stream and the WGSL variable // Base types must match between the vertex stream and the WGSL variable
if (!IsTypeCompatible(var_dt, fmt_dt)) { if (!IsTypeCompatible(var_dt, fmt_dt)) {
std::stringstream err; utils::StringStream err;
err << "VertexAttributeDescriptor for location " err << "VertexAttributeDescriptor for location "
<< std::to_string(attribute_desc.shader_location) << " has format " << std::to_string(attribute_desc.shader_location) << " has format "
<< attribute_desc.format << " but shader expects " << attribute_desc.format << " but shader expects "

View File

@ -21,6 +21,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/texture_dimension.h" #include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Array); TINT_INSTANTIATE_TYPEINFO(tint::type::Array);
@ -81,7 +82,7 @@ bool Array::Equals(const UniqueNode& other) const {
} }
std::string Array::FriendlyName(const SymbolTable& symbols) const { std::string Array::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
if (!IsStrideImplicit()) { if (!IsStrideImplicit()) {
out << "@stride(" << stride_ << ") "; out << "@stride(" << stride_ << ") ";
} }

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/reference.h" #include "src/tint/type/reference.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Atomic); TINT_INSTANTIATE_TYPEINFO(tint::type::Atomic);
@ -42,7 +43,7 @@ bool Atomic::Equals(const type::UniqueNode& other) const {
} }
std::string Atomic::FriendlyName(const SymbolTable& symbols) const { std::string Atomic::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
out << "atomic<" << subtype_->FriendlyName(symbols) << ">"; out << "atomic<" << subtype_->FriendlyName(symbols) << ">";
return out.str(); return out.str();
} }

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/texture_dimension.h" #include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::DepthMultisampledTexture); TINT_INSTANTIATE_TYPEINFO(tint::type::DepthMultisampledTexture);
@ -46,7 +47,7 @@ bool DepthMultisampledTexture::Equals(const UniqueNode& other) const {
} }
std::string DepthMultisampledTexture::FriendlyName(const SymbolTable&) const { std::string DepthMultisampledTexture::FriendlyName(const SymbolTable&) const {
std::ostringstream out; utils::StringStream out;
out << "texture_depth_multisampled_" << dim(); out << "texture_depth_multisampled_" << dim();
return out.str(); return out.str();
} }

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/texture_dimension.h" #include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::DepthTexture); TINT_INSTANTIATE_TYPEINFO(tint::type::DepthTexture);
@ -47,7 +48,7 @@ bool DepthTexture::Equals(const UniqueNode& other) const {
} }
std::string DepthTexture::FriendlyName(const SymbolTable&) const { std::string DepthTexture::FriendlyName(const SymbolTable&) const {
std::ostringstream out; utils::StringStream out;
out << "texture_depth_" << dim(); out << "texture_depth_" << dim();
return out.str(); return out.str();
} }

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/vector.h" #include "src/tint/type/vector.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Matrix); TINT_INSTANTIATE_TYPEINFO(tint::type::Matrix);
@ -51,7 +52,7 @@ bool Matrix::Equals(const UniqueNode& other) const {
} }
std::string Matrix::FriendlyName(const SymbolTable& symbols) const { std::string Matrix::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
out << "mat" << columns_ << "x" << rows_ << "<" << subtype_->FriendlyName(symbols) << ">"; out << "mat" << columns_ << "x" << rows_ << "<" << subtype_->FriendlyName(symbols) << ">";
return out.str(); return out.str();
} }

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/texture_dimension.h" #include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::MultisampledTexture); TINT_INSTANTIATE_TYPEINFO(tint::type::MultisampledTexture);
@ -40,7 +41,7 @@ bool MultisampledTexture::Equals(const UniqueNode& other) const {
} }
std::string MultisampledTexture::FriendlyName(const SymbolTable& symbols) const { std::string MultisampledTexture::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
out << "texture_multisampled_" << dim() << "<" << type_->FriendlyName(symbols) << ">"; out << "texture_multisampled_" << dim() << "<" << type_->FriendlyName(symbols) << ">";
return out.str(); return out.str();
} }

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/reference.h" #include "src/tint/type/reference.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Pointer); TINT_INSTANTIATE_TYPEINFO(tint::type::Pointer);
@ -43,7 +44,7 @@ bool Pointer::Equals(const UniqueNode& other) const {
} }
std::string Pointer::FriendlyName(const SymbolTable& symbols) const { std::string Pointer::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
out << "ptr<"; out << "ptr<";
if (address_space_ != builtin::AddressSpace::kUndefined) { if (address_space_ != builtin::AddressSpace::kUndefined) {
out << address_space_ << ", "; out << address_space_ << ", ";

View File

@ -18,6 +18,7 @@
#include "src/tint/diagnostic/diagnostic.h" #include "src/tint/diagnostic/diagnostic.h"
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Reference); TINT_INSTANTIATE_TYPEINFO(tint::type::Reference);
@ -44,7 +45,7 @@ bool Reference::Equals(const UniqueNode& other) const {
} }
std::string Reference::FriendlyName(const SymbolTable& symbols) const { std::string Reference::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
out << "ref<"; out << "ref<";
if (address_space_ != builtin::AddressSpace::kUndefined) { if (address_space_ != builtin::AddressSpace::kUndefined) {
out << address_space_ << ", "; out << address_space_ << ", ";

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/texture_dimension.h" #include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::SampledTexture); TINT_INSTANTIATE_TYPEINFO(tint::type::SampledTexture);
@ -39,7 +40,7 @@ bool SampledTexture::Equals(const UniqueNode& other) const {
} }
std::string SampledTexture::FriendlyName(const SymbolTable& symbols) const { std::string SampledTexture::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
out << "texture_" << dim() << "<" << type_->FriendlyName(symbols) << ">"; out << "texture_" << dim() << "<" << type_->FriendlyName(symbols) << ">";
return out.str(); return out.str();
} }

View File

@ -19,6 +19,7 @@
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/type/u32.h" #include "src/tint/type/u32.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::StorageTexture); TINT_INSTANTIATE_TYPEINFO(tint::type::StorageTexture);
@ -43,7 +44,7 @@ bool StorageTexture::Equals(const UniqueNode& other) const {
} }
std::string StorageTexture::FriendlyName(const SymbolTable&) const { std::string StorageTexture::FriendlyName(const SymbolTable&) const {
std::ostringstream out; utils::StringStream out;
out << "texture_storage_" << dim() << "<" << texel_format_ << ", " << access_ << ">"; out << "texture_storage_" << dim() << "<" << texel_format_ << ", " << access_ << ">";
return out.str(); return out.str();
} }

View File

@ -22,6 +22,7 @@
#include "src/tint/symbol_table.h" #include "src/tint/symbol_table.h"
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Struct); TINT_INSTANTIATE_TYPEINFO(tint::type::Struct);
TINT_INSTANTIATE_TYPEINFO(tint::type::StructMember); TINT_INSTANTIATE_TYPEINFO(tint::type::StructMember);
@ -96,7 +97,7 @@ std::string Struct::FriendlyName(const SymbolTable& symbols) const {
} }
std::string Struct::Layout(const tint::SymbolTable& symbols) const { std::string Struct::Layout(const tint::SymbolTable& symbols) const {
std::stringstream ss; utils::StringStream ss;
auto member_name_of = [&](const StructMember* sm) { return symbols.NameFor(sm->Name()); }; auto member_name_of = [&](const StructMember* sm) { return symbols.NameFor(sm->Name()); };

View File

@ -18,6 +18,7 @@
#include "src/tint/diagnostic/diagnostic.h" #include "src/tint/diagnostic/diagnostic.h"
#include "src/tint/type/manager.h" #include "src/tint/type/manager.h"
#include "src/tint/utils/hash.h" #include "src/tint/utils/hash.h"
#include "src/tint/utils/string_stream.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Vector); TINT_INSTANTIATE_TYPEINFO(tint::type::Vector);
@ -47,7 +48,7 @@ bool Vector::Equals(const UniqueNode& other) const {
} }
std::string Vector::FriendlyName(const SymbolTable& symbols) const { std::string Vector::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out; utils::StringStream out;
if (packed_) { if (packed_) {
out << "__packed_"; out << "__packed_";
} }

View File

@ -18,6 +18,7 @@
#include <vector> #include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "src/tint/utils/string_stream.h"
namespace tint::utils { namespace tint::utils {
namespace { namespace {
@ -232,7 +233,7 @@ TEST(EnumSetTest, Loop) {
} }
TEST(EnumSetTest, Ostream) { TEST(EnumSetTest, Ostream) {
std::stringstream ss; utils::StringStream ss;
ss << EnumSet<E>(E::A, E::C); ss << EnumSet<E>(E::A, E::C);
EXPECT_EQ(ss.str(), "{A, C}"); EXPECT_EQ(ss.str(), "{A, C}");
} }

View File

@ -21,6 +21,7 @@
#include <string> #include <string>
#include "src/tint/utils/defer.h" #include "src/tint/utils/defer.h"
#include "src/tint/utils/string_stream.h"
namespace tint::utils { namespace tint::utils {
@ -197,7 +198,7 @@ Command::Output Command::Exec(std::initializer_list<std::string> arguments) cons
si.hStdError = stderr_pipe.write; si.hStdError = stderr_pipe.write;
si.hStdInput = stdin_pipe.read; si.hStdInput = stdin_pipe.read;
std::stringstream args; utils::StringStream args;
args << path_; args << path_;
for (auto& arg : arguments) { for (auto& arg : arguments) {
if (!arg.empty()) { if (!arg.empty()) {

View File

@ -18,6 +18,8 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "src/tint/utils/string_stream.h"
namespace tint::utils { namespace tint::utils {
/// TmpFile constructs a temporary file that can be written to, and is /// TmpFile constructs a temporary file that can be written to, and is
@ -55,7 +57,7 @@ class TmpFile {
/// @return a reference to this TmpFile /// @return a reference to this TmpFile
template <typename T> template <typename T>
inline TmpFile& operator<<(T&& data) { inline TmpFile& operator<<(T&& data) {
std::stringstream ss; utils::StringStream ss;
ss << data; ss << data;
std::string str = ss.str(); std::string str = ss.str();
Append(str.data(), str.size()); Append(str.data(), str.size());

View File

@ -43,7 +43,7 @@ namespace tint::utils {
/// @returns value printed as a string via the std::ostream `<<` operator /// @returns value printed as a string via the std::ostream `<<` operator
template <typename T> template <typename T>
std::string ToString(const T& value) { std::string ToString(const T& value) {
std::stringstream s; utils::StringStream s;
s << value; s << value;
return s.str(); return s.str();
} }
@ -52,7 +52,7 @@ std::string ToString(const T& value) {
/// @returns value printed as a string via the std::ostream `<<` operator /// @returns value printed as a string via the std::ostream `<<` operator
template <typename... TYs> template <typename... TYs>
std::string ToString(const std::variant<TYs...>& value) { std::string ToString(const std::variant<TYs...>& value) {
std::stringstream s; utils::StringStream s;
s << std::visit([&](auto& v) { return ToString(v); }, value); s << std::visit([&](auto& v) { return ToString(v); }, value);
return s.str(); return s.str();
} }

View File

@ -16,9 +16,11 @@
#define SRC_TINT_UTILS_STRING_STREAM_H_ #define SRC_TINT_UTILS_STRING_STREAM_H_
#include <functional> #include <functional>
#include <iterator>
#include <limits> #include <limits>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <utility>
namespace tint::utils { namespace tint::utils {
@ -89,6 +91,15 @@ class StringStream {
return *this; return *this;
} }
/// Swaps streams
/// @param other stream to swap too
void swap(StringStream& other) { sstream_.swap(other.sstream_); }
/// repeat queues the character c to be written to the printer n times.
/// @param c the character to print `n` times
/// @param n the number of times to print character `c`
void repeat(char c, size_t n) { std::fill_n(std::ostream_iterator<char>(sstream_), n, c); }
/// The callback to emit a `endl` to the stream /// The callback to emit a `endl` to the stream
using StdEndl = std::ostream& (*)(std::ostream&); using StdEndl = std::ostream& (*)(std::ostream&);

View File

@ -20,6 +20,7 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "src/tint/utils/bitcast.h" #include "src/tint/utils/bitcast.h"
#include "src/tint/utils/string_stream.h"
namespace tint::utils { namespace tint::utils {
namespace { namespace {
@ -1788,7 +1789,7 @@ TEST(TintVectorTest, Equality) {
} }
TEST(TintVectorTest, ostream) { TEST(TintVectorTest, ostream) {
std::stringstream ss; utils::StringStream ss;
ss << Vector{1, 2, 3}; ss << Vector{1, 2, 3};
EXPECT_EQ(ss.str(), "[1, 2, 3]"); EXPECT_EQ(ss.str(), "[1, 2, 3]");
} }
@ -2065,7 +2066,7 @@ TEST(TintVectorRefTest, BeginEnd) {
} }
TEST(TintVectorRefTest, ostream) { TEST(TintVectorRefTest, ostream) {
std::stringstream ss; utils::StringStream ss;
Vector vec{1, 2, 3}; Vector vec{1, 2, 3};
const VectorRef<int> vec_ref(vec); const VectorRef<int> vec_ref(vec);
ss << vec_ref; ss << vec_ref;