diff --git a/src/ast/struct.cc b/src/ast/struct.cc index 1cdf866a52..e643e2b9e0 100644 --- a/src/ast/struct.cc +++ b/src/ast/struct.cc @@ -17,6 +17,8 @@ namespace tint { namespace ast { +Struct::Struct() : Node() {} + Struct::Struct(StructDecoration decoration, std::vector> members) : Node(), decoration_(decoration), members_(std::move(members)) {} @@ -29,6 +31,11 @@ Struct::Struct(const Source& source, Struct::~Struct() = default; bool Struct::IsValid() const { + for (const auto& mem : members_) { + if (mem == nullptr || !mem->IsValid()) { + return false; + } + } return true; } diff --git a/src/ast/struct.h b/src/ast/struct.h index a3d2e12ef9..b7b157ca8f 100644 --- a/src/ast/struct.h +++ b/src/ast/struct.h @@ -32,7 +32,7 @@ namespace ast { class Struct : public Node { public: /// Create a new empty struct statement - Struct() = default; + Struct(); /// Create a new struct statement /// @param decoration The struct decorations /// @param members The struct members diff --git a/src/ast/struct_member.cc b/src/ast/struct_member.cc index 4ad5230e6a..7642ec2447 100644 --- a/src/ast/struct_member.cc +++ b/src/ast/struct_member.cc @@ -36,7 +36,7 @@ StructMember::StructMember( StructMember::~StructMember() = default; bool StructMember::IsValid() const { - if (name_.length() == 0) { + if (name_.empty()) { return false; } if (type_ == nullptr) { diff --git a/src/ast/struct_test.cc b/src/ast/struct_test.cc index 9802270f6e..35d608d096 100644 --- a/src/ast/struct_test.cc +++ b/src/ast/struct_test.cc @@ -59,6 +59,27 @@ TEST_F(StructTest, IsValid) { EXPECT_TRUE(s.IsValid()); } +TEST_F(StructTest, IsValid_Null_StructMember) { + type::I32Type i32; + std::vector> members; + members.push_back(std::make_unique( + "a", &i32, std::vector>())); + members.push_back(nullptr); + + Struct s{StructDecoration::kNone, std::move(members)}; + EXPECT_FALSE(s.IsValid()); +} + +TEST_F(StructTest, IsValid_Invalid_StructMember) { + type::I32Type i32; + std::vector> members; + members.push_back(std::make_unique( + "", &i32, std::vector>())); + + Struct s{StructDecoration::kNone, std::move(members)}; + EXPECT_FALSE(s.IsValid()); +} + TEST_F(StructTest, ToStr) { type::I32Type i32; Source source{27, 4}; @@ -69,10 +90,10 @@ TEST_F(StructTest, ToStr) { Struct s{source, StructDecoration::kNone, std::move(members)}; std::ostringstream out; - s.to_str(out, 0); - EXPECT_EQ(out.str(), R"(Struct{ - StructMember{a: __i32} -} + s.to_str(out, 2); + EXPECT_EQ(out.str(), R"( Struct{ + StructMember{a: __i32} + } )"); }