2020-08-12 19:31:42 +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"
|
2020-08-20 17:00:09 +00:00
|
|
|
#include "src/ast/call_statement.h"
|
2020-09-21 17:56:41 +00:00
|
|
|
#include "src/ast/pipeline_stage.h"
|
2020-08-13 19:17:49 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2020-08-12 19:31:42 +00:00
|
|
|
#include "src/ast/scalar_constructor_expression.h"
|
|
|
|
#include "src/ast/sint_literal.h"
|
2020-09-21 17:56:41 +00:00
|
|
|
#include "src/ast/stage_decoration.h"
|
2020-08-12 19:31:42 +00:00
|
|
|
#include "src/ast/type/f32_type.h"
|
|
|
|
#include "src/ast/type/i32_type.h"
|
|
|
|
#include "src/ast/type/void_type.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-08-12 19:31:42 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
2020-08-25 14:06:05 +00:00
|
|
|
class ValidateFunctionTest : public ValidatorTestHelper,
|
2020-08-12 19:31:42 +00:00
|
|
|
public testing::Test {};
|
|
|
|
|
2020-11-09 16:11:43 +00:00
|
|
|
TEST_F(ValidateFunctionTest, VoidFunctionEndWithoutReturnStatement_Pass) {
|
|
|
|
// [[stage(vertex)]]
|
2020-08-12 19:31:42 +00:00
|
|
|
// fn func -> void { var a:i32 = 2; }
|
|
|
|
ast::type::I32Type i32;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* var = create<ast::Variable>("a", ast::StorageClass::kNone, &i32);
|
2020-11-13 22:18:35 +00:00
|
|
|
var->set_constructor(create<ast::ScalarConstructorExpression>(
|
|
|
|
create<ast::SintLiteral>(&i32, 2)));
|
2020-08-12 19:31:42 +00:00
|
|
|
|
|
|
|
ast::VariableList params;
|
|
|
|
ast::type::VoidType void_type;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(std::move(var)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>(Source{Source::Location{12, 34}}, "func",
|
|
|
|
std::move(params), &void_type, std::move(body));
|
2020-11-13 22:18:35 +00:00
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
|
2020-08-12 19:31:42 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-11-09 16:11:43 +00:00
|
|
|
EXPECT_TRUE(v()->Validate(mod()));
|
2020-08-12 19:31:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:11:43 +00:00
|
|
|
TEST_F(ValidateFunctionTest,
|
|
|
|
VoidFunctionEndWithoutReturnStatementEmptyBody_Pass) {
|
|
|
|
// [[stage(vertex)]]
|
2020-08-12 19:31:42 +00:00
|
|
|
// fn func -> void {}
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
|
|
|
|
std::move(params), &void_type,
|
|
|
|
create<ast::BlockStatement>());
|
2020-11-13 22:18:35 +00:00
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
|
2020-11-09 16:11:43 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
|
|
|
EXPECT_TRUE(v()->Validate(mod()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidateFunctionTest, FunctionEndWithoutReturnStatement_Fail) {
|
|
|
|
// fn func -> int { var a:i32 = 2; }
|
|
|
|
|
|
|
|
ast::type::I32Type i32;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* var = create<ast::Variable>("a", ast::StorageClass::kNone, &i32);
|
2020-11-13 22:18:35 +00:00
|
|
|
var->set_constructor(create<ast::ScalarConstructorExpression>(
|
|
|
|
create<ast::SintLiteral>(&i32, 2)));
|
2020-11-09 16:11:43 +00:00
|
|
|
|
|
|
|
ast::VariableList params;
|
|
|
|
ast::type::VoidType void_type;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(std::move(var)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
|
|
|
|
std::move(params), &i32, std::move(body));
|
2020-11-09 16:11:43 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(
|
|
|
|
v()->error(),
|
|
|
|
"12:34: v-0002: non-void function must end with a return statement");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidateFunctionTest, FunctionEndWithoutReturnStatementEmptyBody_Fail) {
|
|
|
|
// fn func -> int {}
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::type::I32Type i32;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
|
|
|
|
std::move(params), &i32,
|
|
|
|
create<ast::BlockStatement>());
|
2020-08-12 19:31:42 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
2020-11-09 16:11:43 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
v()->error(),
|
|
|
|
"12:34: v-0002: non-void function must end with a return statement");
|
2020-08-12 19:31:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:46:07 +00:00
|
|
|
TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_Pass) {
|
2020-09-21 17:56:41 +00:00
|
|
|
// [[stage(vertex)]]
|
2020-08-13 19:17:49 +00:00
|
|
|
// fn func -> void { return; }
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::VariableList params;
|
2020-09-21 17:56:41 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>());
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func = create<ast::Function>("func", std::move(params), &void_type,
|
|
|
|
std::move(body));
|
2020-11-13 22:18:35 +00:00
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
|
2020-08-13 19:17:49 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
2020-09-21 17:56:41 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineFunctions(mod()->functions())) << td()->error();
|
2020-09-22 14:53:03 +00:00
|
|
|
EXPECT_TRUE(v()->ValidateFunctions(mod()->functions())) << v()->error();
|
2020-08-13 19:17:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:46:07 +00:00
|
|
|
TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_fail) {
|
2020-08-13 19:17:49 +00:00
|
|
|
// fn func -> void { return 2; }
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::type::I32Type i32;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
|
|
|
auto* return_expr = create<ast::ScalarConstructorExpression>(
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::SintLiteral>(&i32, 2));
|
|
|
|
|
|
|
|
body->append(create<ast::ReturnStatement>(Source{Source::Location{12, 34}},
|
|
|
|
std::move(return_expr)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func = create<ast::Function>("func", std::move(params), &void_type,
|
|
|
|
std::move(body));
|
2020-08-13 19:17:49 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
2020-08-13 19:17:49 +00:00
|
|
|
// TODO(sarahM0): replace 000y with a rule number
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-08-17 15:46:07 +00:00
|
|
|
"12:34: v-000y: return statement type must match its function "
|
|
|
|
"return type, returned '__i32', expected '__void'");
|
2020-08-13 19:17:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:46:07 +00:00
|
|
|
TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementTypeF32_fail) {
|
2020-08-13 19:17:49 +00:00
|
|
|
// fn func -> f32 { return 2; }
|
|
|
|
ast::type::I32Type i32;
|
|
|
|
ast::type::F32Type f32;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
|
|
|
auto* return_expr = create<ast::ScalarConstructorExpression>(
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::SintLiteral>(&i32, 2));
|
2020-08-13 19:17:49 +00:00
|
|
|
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>(Source{Source::Location{12, 34}},
|
|
|
|
std::move(return_expr)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>("func", std::move(params), &f32, std::move(body));
|
2020-08-13 19:17:49 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
2020-08-13 19:17:49 +00:00
|
|
|
// TODO(sarahM0): replace 000y with a rule number
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-08-17 15:46:07 +00:00
|
|
|
"12:34: v-000y: return statement type must match its function "
|
|
|
|
"return type, returned '__i32', expected '__f32'");
|
2020-08-13 19:17:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:32:38 +00:00
|
|
|
TEST_F(ValidateFunctionTest, FunctionNamesMustBeUnique_fail) {
|
|
|
|
// fn func -> i32 { return 2; }
|
|
|
|
// fn func -> i32 { return 2; }
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::type::I32Type i32;
|
|
|
|
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
|
|
|
auto* return_expr = create<ast::ScalarConstructorExpression>(
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::SintLiteral>(&i32, 2));
|
2020-08-17 15:32:38 +00:00
|
|
|
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>(std::move(return_expr)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>("func", std::move(params), &i32, std::move(body));
|
2020-08-17 15:32:38 +00:00
|
|
|
|
|
|
|
ast::VariableList params_copy;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body_copy = create<ast::BlockStatement>();
|
|
|
|
auto* return_expr_copy = create<ast::ScalarConstructorExpression>(
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::SintLiteral>(&i32, 2));
|
|
|
|
|
|
|
|
body_copy->append(create<ast::ReturnStatement>(std::move(return_expr_copy)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func_copy =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>(Source{Source::Location{12, 34}}, "func",
|
|
|
|
std::move(params_copy), &i32, std::move(body_copy));
|
2020-08-17 15:32:38 +00:00
|
|
|
|
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
mod()->AddFunction(std::move(func_copy));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(v()->error(),
|
|
|
|
"12:34: v-0016: function names must be unique 'func'");
|
2020-08-17 15:32:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 17:00:09 +00:00
|
|
|
TEST_F(ValidateFunctionTest, RecursionIsNotAllowed_Fail) {
|
|
|
|
// fn func() -> void {func(); return; }
|
|
|
|
ast::type::F32Type f32;
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::ExpressionList call_params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* call_expr = create<ast::CallExpression>(
|
2020-11-02 15:07:47 +00:00
|
|
|
Source{Source::Location{12, 34}},
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::IdentifierExpression>("func"), std::move(call_params));
|
2020-08-20 17:00:09 +00:00
|
|
|
ast::VariableList params0;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body0 = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body0->append(create<ast::CallStatement>(std::move(call_expr)));
|
|
|
|
body0->append(create<ast::ReturnStatement>());
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func0 =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>("func", std::move(params0), &f32, std::move(body0));
|
2020-08-20 17:00:09 +00:00
|
|
|
mod()->AddFunction(std::move(func0));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod())) << v()->error();
|
|
|
|
EXPECT_EQ(v()->error(), "12:34: v-0004: recursion is not allowed: 'func'");
|
2020-08-20 17:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidateFunctionTest, RecursionIsNotAllowedExpr_Fail) {
|
|
|
|
// fn func() -> i32 {var a: i32 = func(); return 2; }
|
|
|
|
ast::type::I32Type i32;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* var = create<ast::Variable>("a", ast::StorageClass::kNone, &i32);
|
2020-08-20 17:00:09 +00:00
|
|
|
ast::ExpressionList call_params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* call_expr = create<ast::CallExpression>(
|
2020-11-02 15:07:47 +00:00
|
|
|
Source{Source::Location{12, 34}},
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::IdentifierExpression>("func"), std::move(call_params));
|
2020-08-20 17:00:09 +00:00
|
|
|
var->set_constructor(std::move(call_expr));
|
|
|
|
ast::VariableList params0;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body0 = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body0->append(create<ast::VariableDeclStatement>(std::move(var)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* return_expr = create<ast::ScalarConstructorExpression>(
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::SintLiteral>(&i32, 2));
|
|
|
|
|
|
|
|
body0->append(create<ast::ReturnStatement>(std::move(return_expr)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func0 =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>("func", std::move(params0), &i32, std::move(body0));
|
2020-08-20 17:00:09 +00:00
|
|
|
mod()->AddFunction(std::move(func0));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod())) << v()->error();
|
|
|
|
EXPECT_EQ(v()->error(), "12:34: v-0004: recursion is not allowed: 'func'");
|
2020-08-20 17:00:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 17:56:41 +00:00
|
|
|
TEST_F(ValidateFunctionTest, Function_WithPipelineStage_NotVoid_Fail) {
|
|
|
|
// [[stage(vertex)]]
|
2020-08-20 21:51:39 +00:00
|
|
|
// fn vtx_main() -> i32 { return 0; }
|
|
|
|
ast::type::I32Type i32;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* return_expr = create<ast::ScalarConstructorExpression>(
|
2020-11-13 22:18:35 +00:00
|
|
|
create<ast::SintLiteral>(&i32, 0));
|
2020-08-20 21:51:39 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>(std::move(return_expr)));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>(Source{Source::Location{12, 34}}, "vtx_main",
|
|
|
|
std::move(params), &i32, std::move(body));
|
2020-11-13 22:18:35 +00:00
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
|
2020-08-20 21:51:39 +00:00
|
|
|
|
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(v()->error(),
|
2020-08-20 21:51:39 +00:00
|
|
|
"12:34: v-0024: Entry point function must return void: 'vtx_main'");
|
|
|
|
}
|
2020-08-24 15:00:46 +00:00
|
|
|
|
2020-09-21 17:56:41 +00:00
|
|
|
TEST_F(ValidateFunctionTest, Function_WithPipelineStage_WithParams_Fail) {
|
|
|
|
// [[stage(vertex)]]
|
2020-08-24 15:00:46 +00:00
|
|
|
// fn vtx_func(a : i32) -> void { return; }
|
|
|
|
ast::type::I32Type i32;
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::VariableList params;
|
2020-11-13 22:18:35 +00:00
|
|
|
params.push_back(create<ast::Variable>("a", ast::StorageClass::kNone, &i32));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>());
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>(Source{Source::Location{12, 34}}, "vtx_func",
|
|
|
|
std::move(params), &void_type, std::move(body));
|
2020-11-13 22:18:35 +00:00
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
|
2020-08-24 15:00:46 +00:00
|
|
|
|
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(v()->error(),
|
2020-08-24 15:00:46 +00:00
|
|
|
"12:34: v-0023: Entry point function must accept no parameters: "
|
|
|
|
"'vtx_func'");
|
|
|
|
}
|
2020-08-25 16:06:24 +00:00
|
|
|
|
2020-11-11 14:21:05 +00:00
|
|
|
TEST_F(ValidateFunctionTest, PipelineStage_MustBeUnique_Fail) {
|
2020-09-21 17:56:41 +00:00
|
|
|
// [[stage(fragment)]]
|
2020-11-11 14:21:05 +00:00
|
|
|
// [[stage(vertex)]]
|
2020-09-21 17:56:41 +00:00
|
|
|
// fn main() -> void { return; }
|
2020-08-25 16:06:24 +00:00
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>());
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func =
|
2020-11-14 01:13:04 +00:00
|
|
|
create<ast::Function>(Source{Source::Location{12, 34}}, "main",
|
|
|
|
std::move(params), &void_type, std::move(body));
|
2020-11-13 22:18:35 +00:00
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
|
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kFragment, Source{}));
|
2020-08-25 16:06:24 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-11-11 14:21:05 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(
|
|
|
|
v()->error(),
|
|
|
|
"12:34: v-0020: only one stage decoration permitted per entry point");
|
2020-08-25 16:06:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 17:56:41 +00:00
|
|
|
TEST_F(ValidateFunctionTest, OnePipelineStageFunctionMustBePresent_Pass) {
|
|
|
|
// [[stage(vertex)]]
|
2020-08-27 18:05:00 +00:00
|
|
|
// fn vtx_func() -> void { return; }
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>());
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func = create<ast::Function>("vtx_func", std::move(params), &void_type,
|
|
|
|
std::move(body));
|
2020-11-13 22:18:35 +00:00
|
|
|
func->add_decoration(
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
|
2020-08-27 18:05:00 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
|
|
|
EXPECT_TRUE(v()->Validate(mod())) << v()->error();
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:56:41 +00:00
|
|
|
TEST_F(ValidateFunctionTest, OnePipelineStageFunctionMustBePresent_Fail) {
|
2020-08-27 18:05:00 +00:00
|
|
|
// fn vtx_func() -> void { return; }
|
|
|
|
ast::type::VoidType void_type;
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::ReturnStatement>());
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* func = create<ast::Function>("vtx_func", std::move(params), &void_type,
|
|
|
|
std::move(body));
|
2020-08-27 18:05:00 +00:00
|
|
|
mod()->AddFunction(std::move(func));
|
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(v()->error(),
|
|
|
|
"0:0: v-0003: At least one of vertex, fragment or compute shader "
|
|
|
|
"must be present");
|
|
|
|
}
|
|
|
|
|
2020-08-12 19:31:42 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace tint
|