[tint] Remove diagnostic from IR encode/decode files.
The IR encode/decode files uses diagnostics for tracking if there were
any errors, and a single instance of styled text. This CL switches to a
string stream and just looks for if the stream is empty.
Bug: 383726508
Change-Id: I0b9000c459d7c6c8e70bf6eed611c99d3608d40c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/230754
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/lang/core/ir/binary/decode.cc b/src/tint/lang/core/ir/binary/decode.cc
index 4374d56..9b0a9e0 100644
--- a/src/tint/lang/core/ir/binary/decode.cc
+++ b/src/tint/lang/core/ir/binary/decode.cc
@@ -81,10 +81,10 @@
Vector<ir::BreakIf*, 32> break_ifs_{};
Vector<ir::Continue*, 32> continues_{};
- diag::List diags_{};
+ std::stringstream err_{};
Hashset<std::string, 4> struct_names_{};
- diag::Result<Module> Decode() {
+ Result<Module> Decode() {
{
const size_t n = static_cast<size_t>(mod_in_.types().size());
types_.Reserve(n);
@@ -133,9 +133,10 @@
PopulateBlock(blocks_[i], mod_in_.blocks()[static_cast<int>(i)]);
}
- if (diags_.ContainsErrors()) {
+ auto err = err_.str();
+ if (!err.empty()) {
// Note: Its not safe to call InferControlInstruction() with a broken IR.
- return diag::Failure{std::move(diags_)};
+ return Failure{err};
}
if (CheckBlocks()) {
@@ -159,14 +160,14 @@
}
}
- if (diags_.ContainsErrors()) {
- return diag::Failure{std::move(diags_)};
+ err = err_.str();
+ if (!err.empty()) {
+ return Failure{err};
}
return std::move(mod_out_);
}
- /// Adds a new error to the diagnostics and returns a reference to it
- diag::Diagnostic& Error() { return diags_.AddError(Source{}); }
+ std::stringstream& Error() { return err_; }
/// Errors if @p number is not finite.
/// @returns @p number if finite, otherwise 0.
@@ -777,7 +778,7 @@
}
if (!struct_names_.Add(struct_name)) {
- Error() << "duplicate struct name: " << style::Type(struct_name);
+ Error() << "duplicate struct name: " << struct_name;
return mod_out_.Types().invalid();
}
@@ -1807,18 +1808,18 @@
} // namespace
-diag::Result<Module> Decode(Slice<const std::byte> encoded) {
+Result<Module> Decode(Slice<const std::byte> encoded) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
pb::Module mod_in;
if (!mod_in.ParseFromArray(encoded.data, static_cast<int>(encoded.len))) {
- return diag::Failure{"failed to deserialize protobuf"};
+ return Failure{"failed to deserialize protobuf"};
}
return Decode(mod_in);
}
-diag::Result<Module> Decode(const pb::Module& mod_in) {
+Result<Module> Decode(const pb::Module& mod_in) {
return Decoder{mod_in}.Decode();
}
diff --git a/src/tint/lang/core/ir/binary/decode.h b/src/tint/lang/core/ir/binary/decode.h
index fd25683..a3448a3 100644
--- a/src/tint/lang/core/ir/binary/decode.h
+++ b/src/tint/lang/core/ir/binary/decode.h
@@ -28,7 +28,8 @@
#ifndef SRC_TINT_LANG_CORE_IR_BINARY_DECODE_H_
#define SRC_TINT_LANG_CORE_IR_BINARY_DECODE_H_
-#include "src/tint/utils/diagnostic/diagnostic.h"
+#include "src/tint/utils/containers/slice.h"
+#include "src/tint/utils/result/result.h"
// Forward declarations
namespace tint::core::ir {
@@ -41,10 +42,10 @@
namespace tint::core::ir::binary {
/// @returns the decoded Module from the serialized protobuf.
-diag::Result<Module> Decode(Slice<const std::byte> encoded);
+Result<Module> Decode(Slice<const std::byte> encoded);
/// @returns the decoded Module from the protobuf.
-diag::Result<Module> Decode(const pb::Module& module);
+Result<Module> Decode(const pb::Module& module);
} // namespace tint::core::ir::binary
diff --git a/src/tint/lang/core/ir/binary/encode.cc b/src/tint/lang/core/ir/binary/encode.cc
index 945b032..7e7d146 100644
--- a/src/tint/lang/core/ir/binary/encode.cc
+++ b/src/tint/lang/core/ir/binary/encode.cc
@@ -27,6 +27,7 @@
#include "src/tint/lang/core/ir/binary/encode.h"
+#include <sstream>
#include <string>
#include <utility>
@@ -104,9 +105,9 @@
Hashmap<const core::ir::Value*, uint32_t, 32> values_{};
Hashmap<const core::constant::Value*, uint32_t, 32> constant_values_{};
- diag::List diags_{};
+ std::stringstream err_{};
- diag::Result<SuccessType> Encode() {
+ Result<SuccessType> Encode() {
// Encode all user-declared structures first. This is to ensure that the IR disassembly
// (which prints structure types first) does not reorder after encoding and decoding.
for (auto* ty : mod_in_.Types()) {
@@ -125,14 +126,14 @@
}
mod_out_.set_root_block(Block(mod_in_.root_block));
- if (diags_.ContainsErrors()) {
- return diag::Failure{std::move(diags_)};
+ auto err = err_.str();
+ if (!err.empty()) {
+ return Failure{err};
}
return Success;
}
- /// Adds a new error to the diagnostics and returns a reference to it
- diag::Diagnostic& Error() { return diags_.AddError(Source{}); }
+ std::stringstream& Error() { return err_; }
////////////////////////////////////////////////////////////////////////////
// Functions
@@ -497,7 +498,7 @@
array_out.set_count(c->value);
if (c->value >= internal_limits::kMaxArrayElementCount) {
Error() << "array count (" << c->value << ") must be less than "
- << internal_limits::kMaxArrayElementCount;
+ << internal_limits::kMaxArrayElementCount << "\n";
}
},
[&](const core::type::RuntimeArrayCount*) { array_out.set_count(0); },
@@ -737,7 +738,7 @@
splat_out.set_type(Type(splat_in->type));
if (DAWN_UNLIKELY(splat_in->count > internal_limits::kMaxArrayConstructorElements)) {
Error() << "array constructor has excessive number of elements (>"
- << internal_limits::kMaxArrayConstructorElements << ")";
+ << internal_limits::kMaxArrayConstructorElements << ")\n";
}
splat_out.set_elements(ConstantValue(splat_in->el));
splat_out.set_count(static_cast<uint32_t>(splat_in->count));
@@ -1322,7 +1323,7 @@
} // namespace
-diag::Result<std::unique_ptr<pb::Module>> EncodeToProto(const Module& mod_in) {
+Result<std::unique_ptr<pb::Module>> EncodeToProto(const Module& mod_in) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
pb::Module mod_out;
@@ -1334,7 +1335,7 @@
return std::make_unique<pb::Module>(mod_out);
}
-diag::Result<Vector<std::byte, 0>> EncodeToBinary(const Module& mod_in) {
+Result<Vector<std::byte, 0>> EncodeToBinary(const Module& mod_in) {
auto mod_out = EncodeToProto(mod_in);
if (mod_out != Success) {
return mod_out.Failure();
@@ -1345,7 +1346,7 @@
buffer.Resize(len);
if (len > 0) {
if (!mod_out.Get()->SerializeToArray(&buffer[0], static_cast<int>(len))) {
- return diag::Failure{"failed to serialize protobuf"};
+ return Failure{"failed to serialize protobuf"};
}
}
return buffer;
diff --git a/src/tint/lang/core/ir/binary/encode.h b/src/tint/lang/core/ir/binary/encode.h
index 529e3ae..46d7458 100644
--- a/src/tint/lang/core/ir/binary/encode.h
+++ b/src/tint/lang/core/ir/binary/encode.h
@@ -31,7 +31,7 @@
#include <memory>
#include "src/tint/utils/containers/vector.h"
-#include "src/tint/utils/diagnostic/diagnostic.h"
+#include "src/tint/utils/result/result.h"
// Forward declarations
namespace tint::core::ir {
@@ -45,10 +45,10 @@
namespace tint::core::ir::binary {
// Encode the module into a proto representation.
-diag::Result<std::unique_ptr<pb::Module>> EncodeToProto(const Module& module);
+Result<std::unique_ptr<pb::Module>> EncodeToProto(const Module& module);
// Encode the module into a binary representation.
-diag::Result<Vector<std::byte, 0>> EncodeToBinary(const Module& module);
+Result<Vector<std::byte, 0>> EncodeToBinary(const Module& module);
} // namespace tint::core::ir::binary
diff --git a/src/tint/lang/core/ir/binary/roundtrip_test.cc b/src/tint/lang/core/ir/binary/roundtrip_test.cc
index 68f5527..606eae4 100644
--- a/src/tint/lang/core/ir/binary/roundtrip_test.cc
+++ b/src/tint/lang/core/ir/binary/roundtrip_test.cc
@@ -52,11 +52,11 @@
auto pre = Disassembler(this->mod).Plain();
auto encoded = EncodeToBinary(this->mod);
if (encoded != Success) {
- return {pre, encoded.Failure().reason.Str()};
+ return {pre, encoded.Failure().reason};
}
auto decoded = Decode(encoded->Slice());
if (decoded != Success) {
- return {pre, decoded.Failure().reason.Str()};
+ return {pre, decoded.Failure().reason};
}
auto post = Disassembler(decoded.Get()).Plain();
return {pre, post};