2020-09-02 19:58:06 +00:00
|
|
|
// 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 "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"
|
2020-09-10 14:37:17 +00:00
|
|
|
#include "src/ast/type/alias_type.h"
|
2020-09-02 19:58:06 +00:00
|
|
|
#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"
|
2020-11-06 17:31:15 +00:00
|
|
|
#include "src/validator/validator_impl.h"
|
|
|
|
#include "src/validator/validator_test_helper.h"
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ValidateControlBlockTest : public ValidatorTestHelper,
|
|
|
|
public testing::Test {};
|
|
|
|
|
2020-09-10 14:37:17 +00:00
|
|
|
TEST_F(ValidateControlBlockTest, SwitchSelectorExpressionNoneIntegerType_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : f32 = 3.14;
|
|
|
|
// switch (a) {
|
|
|
|
// default: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &f32, 3.14f)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{Source::Location{12, 34}}, mod()->RegisterSymbol("a"), "a");
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-12-12 11:58:44 +00:00
|
|
|
body.push_back(
|
|
|
|
create<ast::CaseStatement>(Source{}, default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, body),
|
|
|
|
});
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-02 19:58:06 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0025: switch statement selector expression must be "
|
2020-09-02 19:58:06 +00:00
|
|
|
"of a scalar integer type");
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:17 +00:00
|
|
|
TEST_F(ValidateControlBlockTest, SwitchWithoutDefault_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// case 1: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseSelectorList csl;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl.push_back(create<ast::SintLiteral>(Source{}, &i32, 1));
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-12-12 11:58:44 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(
|
2020-12-14 20:25:27 +00:00
|
|
|
Source{}, csl,
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, cond, body),
|
|
|
|
});
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-02 19:58:06 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0008: switch statement must have exactly one default "
|
2020-09-02 19:58:06 +00:00
|
|
|
"clause");
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:17 +00:00
|
|
|
TEST_F(ValidateControlBlockTest, SwitchWithTwoDefault_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// default: {}
|
|
|
|
// case 1: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl_1;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default_1 =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-11-16 16:41:47 +00:00
|
|
|
switch_body.push_back(
|
2020-12-12 11:58:44 +00:00
|
|
|
create<ast::CaseStatement>(Source{}, default_csl_1, block_default_1));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_case_1;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl_case_1.push_back(create<ast::SintLiteral>(Source{}, &i32, 1));
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_case_1 =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(
|
|
|
|
create<ast::CaseStatement>(Source{}, csl_case_1, block_case_1));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl_2;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default_2 =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-11-16 16:41:47 +00:00
|
|
|
switch_body.push_back(
|
2020-12-12 11:58:44 +00:00
|
|
|
create<ast::CaseStatement>(Source{}, default_csl_2, block_default_2));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, cond, switch_body),
|
|
|
|
});
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-10 14:37:17 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0008: switch statement must have exactly one default "
|
2020-09-10 14:37:17 +00:00
|
|
|
"clause");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidateControlBlockTest,
|
|
|
|
SwitchConditionTypeMustMatchSelectorType2_Fail) {
|
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// case 1: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::U32 u32;
|
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl.push_back(create<ast::UintLiteral>(Source{}, &u32, 1));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl,
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{})));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(
|
|
|
|
create<ast::CaseStatement>(Source{}, default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, switch_body),
|
|
|
|
});
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-02 19:58:06 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0026: the case selector values must have the same "
|
2020-09-10 14:37:17 +00:00
|
|
|
"type as the selector expression.");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidateControlBlockTest,
|
2020-09-10 14:37:17 +00:00
|
|
|
SwitchConditionTypeMustMatchSelectorType_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : u32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// case -1: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::U32 u32;
|
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&u32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::UintLiteral>(Source{}, &u32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl.push_back(create<ast::SintLiteral>(Source{}, &i32, -1));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl,
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(
|
|
|
|
create<ast::CaseStatement>(Source{}, default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, switch_body),
|
|
|
|
});
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-10 14:37:17 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0026: the case selector values must have the same "
|
2020-09-10 14:37:17 +00:00
|
|
|
"type as the selector expression.");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:17 +00:00
|
|
|
TEST_F(ValidateControlBlockTest, NonUniqueCaseSelectorValueUint_Fail) {
|
|
|
|
// var a : u32 = 3;
|
|
|
|
// switch (a) {
|
|
|
|
// case 0: {}
|
|
|
|
// case 2, 2: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::U32 u32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&u32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::UintLiteral>(Source{}, &u32, 3)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_1;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl_1.push_back(create<ast::UintLiteral>(Source{}, &u32, 0));
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
2020-12-14 20:25:27 +00:00
|
|
|
Source{}, csl_1,
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{})));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_2;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl_2.push_back(create<ast::UintLiteral>(Source{}, &u32, 2));
|
|
|
|
csl_2.push_back(create<ast::UintLiteral>(Source{}, &u32, 2));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl_2,
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{})));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(
|
|
|
|
create<ast::CaseStatement>(Source{}, default_csl, block_default));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, switch_body),
|
|
|
|
});
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-10 14:37:17 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0027: a literal value must not appear more than once "
|
2020-09-10 14:37:17 +00:00
|
|
|
"in the case selectors for a switch statement: '2'");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidateControlBlockTest, NonUniqueCaseSelectorValueSint_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// case 10: {}
|
|
|
|
// case 0,1,2,10: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_1;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl_1.push_back(create<ast::SintLiteral>(Source{}, &i32, 10));
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
2020-12-14 20:25:27 +00:00
|
|
|
Source{}, csl_1,
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_2;
|
2020-12-12 01:35:43 +00:00
|
|
|
csl_2.push_back(create<ast::SintLiteral>(Source{}, &i32, 0));
|
|
|
|
csl_2.push_back(create<ast::SintLiteral>(Source{}, &i32, 1));
|
|
|
|
csl_2.push_back(create<ast::SintLiteral>(Source{}, &i32, 2));
|
|
|
|
csl_2.push_back(create<ast::SintLiteral>(Source{}, &i32, 10));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl_2,
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(
|
|
|
|
create<ast::CaseStatement>(Source{}, default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, switch_body),
|
|
|
|
});
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-30 20:12:50 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0027: a literal value must not appear more than once in "
|
2020-09-30 20:12:50 +00:00
|
|
|
"the case selectors for a switch statement: '10'");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:17 +00:00
|
|
|
TEST_F(ValidateControlBlockTest, LastClauseLastStatementIsFallthrough_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// default: { fallthrough; }
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(
|
|
|
|
Source{},
|
|
|
|
ast::StatementList{
|
|
|
|
create<ast::FallthroughStatement>(Source{Source::Location{12, 34}}),
|
|
|
|
});
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-12-12 11:58:44 +00:00
|
|
|
body.push_back(
|
|
|
|
create<ast::CaseStatement>(Source{}, default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, body),
|
|
|
|
});
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(block));
|
2020-09-02 19:58:06 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0028: a fallthrough statement must not appear as the "
|
2020-09-02 19:58:06 +00:00
|
|
|
"last statement in last clause of a switch");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidateControlBlockTest, SwitchCase_Pass) {
|
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// default: {}
|
|
|
|
// case 5: {}
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-11-13 22:18:35 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(Source{Source::Location{12, 34}},
|
2020-11-16 16:41:47 +00:00
|
|
|
default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseSelectorList case_csl;
|
2020-12-12 01:35:43 +00:00
|
|
|
case_csl.push_back(create<ast::SintLiteral>(Source{}, &i32, 5));
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_case =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-12-12 11:58:44 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(Source{}, case_csl, block_case));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, body),
|
|
|
|
});
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_TRUE(v()->ValidateStatements(block)) << v()->error();
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:17 +00:00
|
|
|
TEST_F(ValidateControlBlockTest, SwitchCaseAlias_Pass) {
|
|
|
|
// type MyInt = u32;
|
2020-09-21 17:56:41 +00:00
|
|
|
// var v: MyInt;
|
|
|
|
// switch(v){
|
|
|
|
// default: {}
|
2020-09-10 14:37:17 +00:00
|
|
|
// }
|
2020-09-21 17:56:41 +00:00
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::U32 u32;
|
2020-12-11 19:35:03 +00:00
|
|
|
ast::type::Alias my_int{mod()->RegisterSymbol("MyInt"), "MyInt", &u32};
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&my_int, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{},
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &u32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-12 01:38:13 +00:00
|
|
|
auto* cond = create<ast::IdentifierExpression>(
|
|
|
|
Source{}, mod()->RegisterSymbol("a"), "a");
|
2020-09-10 14:37:17 +00:00
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default =
|
|
|
|
create<ast::BlockStatement>(Source{}, ast::StatementList{});
|
2020-09-10 14:37:17 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-11-13 22:18:35 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(Source{Source::Location{12, 34}},
|
2020-11-16 16:41:47 +00:00
|
|
|
default_csl, block_default));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(
|
|
|
|
Source{}, ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(Source{}, var),
|
|
|
|
create<ast::SwitchStatement>(Source{}, cond, body),
|
|
|
|
});
|
2020-10-19 15:31:47 +00:00
|
|
|
mod()->AddConstructedType(&my_int);
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();
|
|
|
|
EXPECT_TRUE(v()->ValidateStatements(block)) << v()->error();
|
2020-09-10 14:37:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:58:06 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace tint
|