diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f4ef8b337c..d58e85e038 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -222,6 +222,7 @@ set(TINT_TEST_SRCS ast/kill_statement_test.cc ast/location_decoration_test.cc ast/loop_statement_test.cc + ast/member_accessor_expression_test.cc ast/module_test.cc ast/set_decoration_test.cc ast/struct_member_test.cc diff --git a/src/ast/loop_statement.cc b/src/ast/loop_statement.cc index e99c87cc5e..97efc069d0 100644 --- a/src/ast/loop_statement.cc +++ b/src/ast/loop_statement.cc @@ -54,10 +54,10 @@ void LoopStatement::to_str(std::ostream& out, size_t indent) const { if (continuing_.size() > 0) { make_indent(out, indent + 2); out << "continuing {" << std::endl; - + for (const auto& stmt : continuing_) stmt->to_str(out, indent + 4); - + make_indent(out, indent + 2); out << "}" << std::endl; } diff --git a/src/ast/loop_statement_test.cc b/src/ast/loop_statement_test.cc index cb7f8cbcd6..d49636fca6 100644 --- a/src/ast/loop_statement_test.cc +++ b/src/ast/loop_statement_test.cc @@ -17,8 +17,8 @@ #include #include "gtest/gtest.h" -#include "src/ast/kill_statement.h" #include "src/ast/if_statement.h" +#include "src/ast/kill_statement.h" #include "src/ast/nop_statement.h" namespace tint { diff --git a/src/ast/member_accessor_expression.cc b/src/ast/member_accessor_expression.cc index 24f43ba29f..180b06b9e4 100644 --- a/src/ast/member_accessor_expression.cc +++ b/src/ast/member_accessor_expression.cc @@ -17,6 +17,8 @@ namespace tint { namespace ast { +MemberAccessorExpression::MemberAccessorExpression() = default; + MemberAccessorExpression::MemberAccessorExpression( std::unique_ptr structure, std::unique_ptr member) @@ -33,7 +35,13 @@ MemberAccessorExpression::MemberAccessorExpression( MemberAccessorExpression::~MemberAccessorExpression() = default; bool MemberAccessorExpression::IsValid() const { - return struct_ != nullptr && member_ != nullptr; + if (struct_ == nullptr || !struct_->IsValid()) { + return false; + } + if (member_ == nullptr || !member_->IsValid()) { + return false; + } + return true; } void MemberAccessorExpression::to_str(std::ostream& out, size_t indent) const { diff --git a/src/ast/member_accessor_expression.h b/src/ast/member_accessor_expression.h index ff24bad242..996e3ae127 100644 --- a/src/ast/member_accessor_expression.h +++ b/src/ast/member_accessor_expression.h @@ -29,6 +29,8 @@ namespace ast { /// A member accessor expression class MemberAccessorExpression : public Expression { public: + /// Constructor + MemberAccessorExpression(); /// Constructor /// @param structure the structure /// @param member the member diff --git a/src/ast/member_accessor_expression_test.cc b/src/ast/member_accessor_expression_test.cc new file mode 100644 index 0000000000..57e30be342 --- /dev/null +++ b/src/ast/member_accessor_expression_test.cc @@ -0,0 +1,109 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/ast/member_accessor_expression.h" + +#include + +#include "gtest/gtest.h" +#include "src/ast/identifier_expression.h" + +namespace tint { +namespace ast { + +using MemberAccessorExpressionTest = testing::Test; + +TEST_F(MemberAccessorExpressionTest, Creation) { + auto str = std::make_unique("structure"); + auto mem = std::make_unique("member"); + + auto str_ptr = str.get(); + auto mem_ptr = mem.get(); + + MemberAccessorExpression stmt(std::move(str), std::move(mem)); + EXPECT_EQ(stmt.structure(), str_ptr); + EXPECT_EQ(stmt.member(), mem_ptr); +} + +TEST_F(MemberAccessorExpressionTest, Creation_WithSource) { + auto str = std::make_unique("structure"); + auto mem = std::make_unique("member"); + + MemberAccessorExpression stmt(Source{20, 2}, std::move(str), std::move(mem)); + auto src = stmt.source(); + EXPECT_EQ(src.line, 20); + EXPECT_EQ(src.column, 2); +} + +TEST_F(MemberAccessorExpressionTest, IsMemberAccessor) { + MemberAccessorExpression stmt; + EXPECT_TRUE(stmt.IsMemberAccessor()); +} + +TEST_F(MemberAccessorExpressionTest, IsValid) { + auto str = std::make_unique("structure"); + auto mem = std::make_unique("member"); + + MemberAccessorExpression stmt(std::move(str), std::move(mem)); + EXPECT_TRUE(stmt.IsValid()); +} + +TEST_F(MemberAccessorExpressionTest, IsValid_NullStruct) { + auto mem = std::make_unique("member"); + + MemberAccessorExpression stmt; + stmt.set_member(std::move(mem)); + EXPECT_FALSE(stmt.IsValid()); +} + +TEST_F(MemberAccessorExpressionTest, IsValid_InvalidStruct) { + auto str = std::make_unique(""); + auto mem = std::make_unique("member"); + + MemberAccessorExpression stmt(std::move(str), std::move(mem)); + EXPECT_FALSE(stmt.IsValid()); +} + +TEST_F(MemberAccessorExpressionTest, IsValid_NullMember) { + auto str = std::make_unique("structure"); + + MemberAccessorExpression stmt; + stmt.set_structure(std::move(str)); + EXPECT_FALSE(stmt.IsValid()); +} + +TEST_F(MemberAccessorExpressionTest, IsValid_InvalidMember) { + auto str = std::make_unique("structure"); + auto mem = std::make_unique(""); + + MemberAccessorExpression stmt(std::move(str), std::move(mem)); + EXPECT_FALSE(stmt.IsValid()); +} + +TEST_F(MemberAccessorExpressionTest, ToStr) { + auto str = std::make_unique("structure"); + auto mem = std::make_unique("member"); + + MemberAccessorExpression stmt(std::move(str), std::move(mem)); + std::ostringstream out; + stmt.to_str(out, 2); + EXPECT_EQ(out.str(), R"( MemberAccessor{ + Identifier{structure} + Identifier{member} + } +)"); +} + +} // namespace ast +} // namespace tint