From bab2a4dd96706329ab7c3740844b3f155d8a9b2c Mon Sep 17 00:00:00 2001 From: Sarah Mashayekhi Date: Wed, 2 Sep 2020 19:58:06 +0000 Subject: [PATCH] [validation] Add disabled tests for validating switch statements This CL adds 6 unit tests for validating switch statements. v-switch01: switch statement selector expression must be of a scalar integer type v-switch02: switch statement must have exactly one default clause v-switch03: the case selector values must have the same type as the selector expression v-switch04: a literal value must not appear more than once in the case selectors for a switch statement v-switch05: a fallthrough statement must not appear as the last statement in last clause of a switch Bug: tint: 6 Change-Id: I0283afec22e56097b6f63c8455da23b87dfe26dc Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/27740 Commit-Queue: Sarah Mashayekhi Reviewed-by: dan sinclair --- BUILD.gn | 1 + src/CMakeLists.txt | 3 +- src/validator_control_block_test.cc | 300 ++++++++++++++++++++++++++++ 3 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 src/validator_control_block_test.cc diff --git a/BUILD.gn b/BUILD.gn index 7462befd29..0318b6251d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -742,6 +742,7 @@ source_set("tint_unittests_core_src") { "src/scope_stack_test.cc", "src/type_determiner_test.cc", "src/type_manager_test.cc", + "src/validator_control_block_test.cc", "src/validator_function_test.cc", "src/validator_test.cc", ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3d82ef66b9..0a2fa9bb41 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -352,8 +352,9 @@ set(TINT_TEST_SRCS scope_stack_test.cc type_determiner_test.cc type_manager_test.cc - validator_test.cc + validator_control_block_test.cc validator_function_test.cc + validator_test.cc ) ## Tint library diff --git a/src/validator_control_block_test.cc b/src/validator_control_block_test.cc new file mode 100644 index 0000000000..60f40e2bbb --- /dev/null +++ b/src/validator_control_block_test.cc @@ -0,0 +1,300 @@ +// 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/validator_impl.h" + +#include "gtest/gtest.h" +#include "spirv/unified1/GLSL.std.450.h" +#include "src/ast/case_statement.h" +#include "src/ast/fallthrough_statement.h" +#include "src/ast/return_statement.h" +#include "src/ast/scalar_constructor_expression.h" +#include "src/ast/sint_literal.h" +#include "src/ast/switch_statement.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/i32_type.h" +#include "src/ast/type/u32_type.h" +#include "src/ast/uint_literal.h" +#include "src/ast/variable.h" +#include "src/ast/variable_decl_statement.h" +#include "src/type_determiner.h" +#include "src/validator_test_helper.h" + +namespace tint { +namespace { + +class ValidateControlBlockTest : public ValidatorTestHelper, + public testing::Test {}; + +TEST_F(ValidateControlBlockTest, + DISABLED_SwitchSelectorExpressionNoneIntegerType_Fail) { + // var a : f32 = 3.14; + // switch (a) { + // default: {} + // } + ast::type::F32Type f32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &f32); + var->set_constructor(std::make_unique( + std::make_unique(&f32, 3.14f))); + + auto cond = std::make_unique("a"); + ast::CaseSelectorList default_csl; + auto block_default = std::make_unique(); + ast::CaseStatementList body; + body.push_back(std::make_unique( + std::move(default_csl), std::move(block_default))); + + auto block = std::make_unique(); + block->append(std::make_unique(std::move(var))); + block->append(std::make_unique( + Source{12, 34}, std::move(cond), std::move(body))); + + EXPECT_TRUE(td()->DetermineStatements(block.get())) << td()->error(); + EXPECT_FALSE(v()->ValidateStatements(block.get())); + EXPECT_EQ(v()->error(), + "12:34: v-switch01: switch statement selector expression must be " + "of a scalar integer type"); +} + +TEST_F(ValidateControlBlockTest, DISABLED_SwitchWithoutDefault_Fail) { + // var a : i32 = 2; + // switch (a) { + // case 1: {} + // } + ast::type::I32Type i32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &i32); + var->set_constructor(std::make_unique( + std::make_unique(&i32, 2))); + + auto cond = std::make_unique("a"); + ast::CaseSelectorList csl; + csl.push_back(std::make_unique(&i32, 1)); + ast::CaseStatementList body; + body.push_back(std::make_unique( + std::move(csl), std::make_unique())); + + auto block = std::make_unique(); + block->append(std::make_unique(std::move(var))); + block->append(std::make_unique( + Source{12, 34}, std::move(cond), std::move(body))); + + EXPECT_TRUE(td()->DetermineStatements(block.get())) << td()->error(); + EXPECT_FALSE(v()->ValidateStatements(block.get())); + EXPECT_EQ(v()->error(), + "12:34: v-switch02: switch statement must have exactly one default " + "clause"); +} + +TEST_F(ValidateControlBlockTest, DISABLED_SwitchWithTwoDefault_Fail) { + // var a : i32 = 2; + // switch (a) { + // default: {} + // case 1: {} + // default: {} + // } + ast::type::I32Type i32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &i32); + var->set_constructor(std::make_unique( + std::make_unique(&i32, 2))); + + ast::CaseStatementList switch_body; + auto cond = std::make_unique("a"); + + ast::CaseSelectorList default_csl_1; + auto block_default_1 = std::make_unique(); + switch_body.push_back(std::make_unique( + std::move(default_csl_1), std::move(block_default_1))); + + ast::CaseSelectorList csl_case_1; + csl_case_1.push_back(std::make_unique(&i32, 1)); + auto block_case_1 = std::make_unique(); + switch_body.push_back(std::make_unique( + std::move(csl_case_1), std::move(block_case_1))); + + ast::CaseSelectorList default_csl_2; + auto block_default_2 = std::make_unique(); + switch_body.push_back(std::make_unique( + Source{12, 34}, std::move(default_csl_2), std::move(block_default_2))); + + auto block = std::make_unique(); + block->append(std::make_unique(std::move(var))); + block->append(std::make_unique(std::move(cond), + std::move(switch_body))); + + EXPECT_TRUE(td()->DetermineStatements(block.get())) << td()->error(); + EXPECT_FALSE(v()->ValidateStatements(block.get())); + EXPECT_EQ(v()->error(), + "12:34: v-switch02: switch statement must have exactly one default " + "clause"); +} + +TEST_F(ValidateControlBlockTest, + DISABLED_SwitchConditionTypeMustMatchSelectorType_Fail) { + // var a : u32 = 2; + // switch (a) { + // case -1: {} + // default: {} + // } + ast::type::U32Type u32; + ast::type::I32Type i32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &u32); + var->set_constructor(std::make_unique( + std::make_unique(&u32, 2))); + + ast::CaseStatementList switch_body; + auto cond = std::make_unique("a"); + + ast::CaseSelectorList csl; + csl.push_back(std::make_unique(&i32, -1)); + switch_body.push_back(std::make_unique( + Source{12, 34}, std::move(csl), std::make_unique())); + + ast::CaseSelectorList default_csl; + auto block_default = std::make_unique(); + switch_body.push_back(std::make_unique( + std::move(default_csl), std::move(block_default))); + + auto block = std::make_unique(); + block->append(std::make_unique(std::move(var))); + block->append(std::make_unique(std::move(cond), + std::move(switch_body))); + + EXPECT_TRUE(td()->DetermineStatements(block.get())) << td()->error(); + EXPECT_FALSE(v()->ValidateStatements(block.get())); + EXPECT_EQ( + v()->error(), + "12:34: v-switch03: the case selector values must have the same type " + "as the selector expression."); +} + +TEST_F(ValidateControlBlockTest, + DISABLED_NonUniqueCaseSelectorLiteralValue_Fail) { + // var a : i32 = 2; + // switch (a) { + // case 10: {} + // case 0,1,2,10: {} + // default: {} + // } + ast::type::I32Type i32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &i32); + var->set_constructor(std::make_unique( + std::make_unique(&i32, 2))); + + ast::CaseStatementList switch_body; + auto cond = std::make_unique("a"); + + ast::CaseSelectorList csl_1; + csl_1.push_back(std::make_unique(&i32, 10)); + switch_body.push_back(std::make_unique( + std::move(csl_1), std::make_unique())); + + ast::CaseSelectorList csl_2; + csl_2.push_back(std::make_unique(&i32, 0)); + csl_2.push_back(std::make_unique(&i32, 1)); + csl_2.push_back(std::make_unique(&i32, 2)); + csl_2.push_back(std::make_unique(&i32, 10)); + switch_body.push_back(std::make_unique( + Source{12, 34}, std::move(csl_1), + std::make_unique())); + + ast::CaseSelectorList default_csl; + auto block_default = std::make_unique(); + switch_body.push_back(std::make_unique( + std::move(default_csl), std::move(block_default))); + + auto block = std::make_unique(); + block->append(std::make_unique(std::move(var))); + block->append(std::make_unique(std::move(cond), + std::move(switch_body))); + + EXPECT_TRUE(td()->DetermineStatements(block.get())) << td()->error(); + EXPECT_FALSE(v()->ValidateStatements(block.get())); + EXPECT_EQ( + v()->error(), + "12:34: v-switch04: a literal value must not appear more than once in " + "the case selectors for a switch statement"); +} + +TEST_F(ValidateControlBlockTest, + DISABLED_LastClauseLastStatementIsFallthrough_Fail) { + // var a : i32 = 2; + // switch (a) { + // default: { fallthrough; } + // } + ast::type::I32Type i32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &i32); + var->set_constructor(std::make_unique( + std::make_unique(&i32, 2))); + + auto cond = std::make_unique("a"); + ast::CaseSelectorList default_csl; + auto block_default = std::make_unique(); + block_default->append(std::make_unique()); + ast::CaseStatementList body; + body.push_back(std::make_unique( + Source{12, 34}, std::move(default_csl), std::move(block_default))); + + auto block = std::make_unique(); + block->append(std::make_unique(std::move(var))); + block->append( + std::make_unique(std::move(cond), std::move(body))); + + EXPECT_TRUE(td()->DetermineStatements(block.get())) << td()->error(); + EXPECT_FALSE(v()->ValidateStatements(block.get())); + EXPECT_EQ(v()->error(), + "12:34: v-switch05: a fallthrough statement must not appear as the " + "last statement in last clause of a switch"); +} + +TEST_F(ValidateControlBlockTest, SwitchCase_Pass) { + // var a : i32 = 2; + // switch (a) { + // default: {} + // case 5: {} + // } + ast::type::I32Type i32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &i32); + var->set_constructor(std::make_unique( + std::make_unique(&i32, 2))); + + auto cond = std::make_unique("a"); + ast::CaseSelectorList default_csl; + auto block_default = std::make_unique(); + ast::CaseStatementList body; + body.push_back(std::make_unique( + Source{12, 34}, std::move(default_csl), std::move(block_default))); + ast::CaseSelectorList case_csl; + case_csl.push_back(std::make_unique(&i32, 5)); + auto block_case = std::make_unique(); + body.push_back(std::make_unique(std::move(case_csl), + std::move(block_case))); + + auto block = std::make_unique(); + block->append(std::make_unique(std::move(var))); + block->append( + std::make_unique(std::move(cond), std::move(body))); + + EXPECT_TRUE(td()->DetermineStatements(block.get())) << td()->error(); + EXPECT_TRUE(v()->ValidateStatements(block.get())) << v()->error(); +} + +} // namespace +} // namespace tint