From f16250b8ac4068dbaffed8242c9aa6c87657f34a Mon Sep 17 00:00:00 2001 From: Sarah Mashayekhi Date: Wed, 12 Aug 2020 19:31:42 +0000 Subject: [PATCH] [validation] Validates functions return statement This CL adds a disabled test for validation rule v-0002: functions must end with a return statement Bug: Tint: 6 Change-Id: I127aa1bd7d236ff353fd89024b274c9f9b463f15 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26680 Reviewed-by: dan sinclair Commit-Queue: dan sinclair --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/validator_function_test.cc | 92 ++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/validator_function_test.cc diff --git a/BUILD.gn b/BUILD.gn index 28bf8394eb..3a2bdfcd11 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -739,6 +739,7 @@ source_set("tint_unittests_core_src") { "src/scope_stack_test.cc", "src/type_determiner_test.cc", "src/type_manager_test.cc", + "src/validator_function_test.cc", "src/validator_test.cc", ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9c2c3265d9..01d2ae6134 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -351,6 +351,7 @@ set(TINT_TEST_SRCS type_determiner_test.cc type_manager_test.cc validator_test.cc + validator_function_test.cc ) ## Tint library diff --git a/src/validator_function_test.cc b/src/validator_function_test.cc new file mode 100644 index 0000000000..e582defb0c --- /dev/null +++ b/src/validator_function_test.cc @@ -0,0 +1,92 @@ +// 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/scalar_constructor_expression.h" +#include "src/ast/sint_literal.h" +#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" + +namespace tint { +namespace { + +class TypeDeterminerHelper { + // TODO(sarahM0): move this form this test file, type_determiner_test.cc and + // validator_test.cc to its own file. + public: + TypeDeterminerHelper() + : td_(std::make_unique(&ctx_, &mod_)) {} + + TypeDeterminer* td() const { return td_.get(); } + ast::Module* mod() { return &mod_; } + + private: + Context ctx_; + ast::Module mod_; + std::unique_ptr td_; +}; + +class ValidateFunctionTest : public TypeDeterminerHelper, + public testing::Test {}; + +TEST_F(ValidateFunctionTest, DISABLED_FunctionEndWithoutReturnStatement_Fail) { + // fn func -> void { var a:i32 = 2; } + + 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::VariableList params; + ast::type::VoidType void_type; + auto func = std::make_unique(Source{12, 34}, "func", + std::move(params), &void_type); + auto body = std::make_unique(); + body->append(std::make_unique(std::move(var))); + func->set_body(std::move(body)); + mod()->AddFunction(std::move(func)); + + EXPECT_TRUE(td()->Determine()) << td()->error(); + tint::ValidatorImpl v; + EXPECT_FALSE(v.Validate(mod())); + EXPECT_EQ(v.error(), + "12:34: v-0002: 'function must end with a return statement 'func'"); +} + +TEST_F(ValidateFunctionTest, + DISABLED_FunctionEndWithoutReturnStatementEmptyBody_Fail) { + // fn func -> void {} + ast::type::VoidType void_type; + ast::VariableList params; + auto func = std::make_unique(Source{12, 34}, "func", + std::move(params), &void_type); + mod()->AddFunction(std::move(func)); + + EXPECT_TRUE(td()->Determine()) << td()->error(); + tint::ValidatorImpl v; + EXPECT_FALSE(v.Validate(mod())); + EXPECT_EQ(v.error(), + "12:34: v-0002: 'function must end with a return statement 'func'"); +} + +} // namespace +} // namespace tint