[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 <dsinclair@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 28bf839..3a2bdfc 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -739,6 +739,7 @@
"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 9c2c326..01d2ae6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -351,6 +351,7 @@
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 0000000..e582def
--- /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<TypeDeterminer>(&ctx_, &mod_)) {}
+
+ TypeDeterminer* td() const { return td_.get(); }
+ ast::Module* mod() { return &mod_; }
+
+ private:
+ Context ctx_;
+ ast::Module mod_;
+ std::unique_ptr<TypeDeterminer> 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<ast::Variable>("a", ast::StorageClass::kNone, &i32);
+ var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::SintLiteral>(&i32, 2)));
+
+ ast::VariableList params;
+ ast::type::VoidType void_type;
+ auto func = std::make_unique<ast::Function>(Source{12, 34}, "func",
+ std::move(params), &void_type);
+ auto body = std::make_unique<ast::BlockStatement>();
+ body->append(std::make_unique<ast::VariableDeclStatement>(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<ast::Function>(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