dawn-cmake/src/resolver/call_validation_test.cc

316 lines
9.6 KiB
C++

// Copyright 2021 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/resolver/resolver.h"
#include "gmock/gmock.h"
#include "src/ast/call_statement.h"
#include "src/resolver/resolver_test_helper.h"
namespace tint {
namespace resolver {
namespace {
using ResolverCallValidationTest = ResolverTest;
TEST_F(ResolverCallValidationTest, Recursive_Invalid) {
// fn main() {main(); }
SetSource(Source::Location{12, 34});
auto* call_expr = Call("main");
ast::VariableList params0;
Func("main", params0, ty.void_(),
ast::StatementList{
create<ast::CallStatement>(call_expr),
},
ast::DecorationList{
Stage(ast::PipelineStage::kVertex),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: recursion is not permitted. 'main' attempted to call "
"itself.");
}
TEST_F(ResolverCallValidationTest, Undeclared_Invalid) {
// fn main() {func(); return; }
// fn func() { return; }
SetSource(Source::Location{12, 34});
auto* call_expr = Call("func");
ast::VariableList params0;
Func("main", params0, ty.f32(),
ast::StatementList{
create<ast::CallStatement>(call_expr),
Return(),
},
ast::DecorationList{});
Func("func", params0, ty.f32(),
ast::StatementList{
Return(),
},
ast::DecorationList{});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: unable to find called function: func");
}
TEST_F(ResolverCallValidationTest, TooFewArgs) {
Func("foo", {Param(Sym(), ty.i32()), Param(Sym(), ty.f32())}, ty.void_(),
{Return()});
auto* call = Call(Source{{12, 34}}, "foo", 1);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
"12:34 error: too few arguments in call to 'foo', expected 2, got 1");
}
TEST_F(ResolverCallValidationTest, TooManyArgs) {
Func("foo", {Param(Sym(), ty.i32()), Param(Sym(), ty.f32())}, ty.void_(),
{Return()});
auto* call = Call(Source{{12, 34}}, "foo", 1, 1.0f, 1.0f);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
"12:34 error: too many arguments in call to 'foo', expected 2, got 3");
}
TEST_F(ResolverCallValidationTest, MismatchedArgs) {
Func("foo", {Param(Sym(), ty.i32()), Param(Sym(), ty.f32())}, ty.void_(),
{Return()});
auto* call = Call("foo", Expr(Source{{12, 34}}, true), 1.0f);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: type mismatch for argument 1 in call to 'foo', "
"expected 'i32', got 'bool'");
}
TEST_F(ResolverCallValidationTest, UnusedRetval) {
// fn main() {func(); return; }
// fn func() { return; }
Func("func", {}, ty.f32(), {Return(Expr(1.0f))}, {});
Func("main", {}, ty.f32(),
ast::StatementList{
create<ast::CallStatement>(Source{{12, 34}}, Call("func")),
Return(),
},
{});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: result of called function was not used. If this was "
"intentional wrap the function call in ignore()");
}
TEST_F(ResolverCallValidationTest, PointerArgument_VariableIdentExpr) {
// fn foo(p: ptr<function, i32>) {}
// fn main() {
// var z: i32 = 1;
// foo(&z);
// }
auto* param = Param("p", ty.pointer<i32>(ast::StorageClass::kFunction));
Func("foo", {param}, ty.void_(), {});
Func("main", {}, ty.void_(),
ast::StatementList{
Decl(Var("z", ty.i32(), Expr(1))),
create<ast::CallStatement>(
Call("foo", AddressOf(Source{{12, 34}}, Expr("z")))),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverCallValidationTest, PointerArgument_ConstIdentExpr) {
// fn foo(p: ptr<function, i32>) {}
// fn main() {
// let z: i32 = 1;
// foo(&z);
// }
auto* param = Param("p", ty.pointer<i32>(ast::StorageClass::kFunction));
Func("foo", {param}, ty.void_(), {});
Func("main", {}, ty.void_(),
ast::StatementList{
Decl(Const("z", ty.i32(), Expr(1))),
create<ast::CallStatement>(
Call("foo", AddressOf(Expr(Source{{12, 34}}, "z")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: cannot take the address of expression");
}
TEST_F(ResolverCallValidationTest, PointerArgument_NotIdentExprVar) {
// struct S { m: i32; };
// fn foo(p: ptr<function, i32>) {}
// fn main() {
// var v: S;
// foo(&v.m);
// }
auto* S = Structure("S", {Member("m", ty.i32())});
auto* param = Param("p", ty.pointer<i32>(ast::StorageClass::kFunction));
Func("foo", {param}, ty.void_(), {});
Func("main", {}, ty.void_(),
ast::StatementList{
Decl(Var("v", ty.Of(S))),
create<ast::CallStatement>(Call(
"foo", AddressOf(Source{{12, 34}}, MemberAccessor("v", "m")))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: expected an address-of expression of a variable "
"identifier expression or a function parameter");
}
TEST_F(ResolverCallValidationTest, PointerArgument_AddressOfMemberAccessor) {
// struct S { m: i32; };
// fn foo(p: ptr<function, i32>) {}
// fn main() {
// let v: S = S();
// foo(&v.m);
// }
auto* S = Structure("S", {Member("m", ty.i32())});
auto* param = Param("p", ty.pointer<i32>(ast::StorageClass::kFunction));
Func("foo", {param}, ty.void_(), {});
Func("main", {}, ty.void_(),
ast::StatementList{
Decl(Const("v", ty.Of(S), Construct(ty.Of(S)))),
create<ast::CallStatement>(Call(
"foo",
AddressOf(Expr(Source{{12, 34}}, MemberAccessor("v", "m"))))),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: cannot take the address of expression");
}
TEST_F(ResolverCallValidationTest, PointerArgument_FunctionParam) {
// fn foo(p: ptr<function, i32>) {}
// fn bar(p: ptr<function, i32>) {
// foo(p);
// }
Func("foo", {Param("p", ty.pointer<i32>(ast::StorageClass::kFunction))},
ty.void_(), {});
Func("bar", {Param("p", ty.pointer<i32>(ast::StorageClass::kFunction))},
ty.void_(),
ast::StatementList{create<ast::CallStatement>(Call("foo", Expr("p")))});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverCallValidationTest, PointerArgument_FunctionParamWithMain) {
// fn foo(p: ptr<function, i32>) {}
// fn bar(p: ptr<function, i32>) {
// foo(p);
// }
// [[stage(fragment)]]
// fn main() {
// var v: i32;
// bar(&v);
// }
Func("foo", {Param("p", ty.pointer<i32>(ast::StorageClass::kFunction))},
ty.void_(), {});
Func("bar", {Param("p", ty.pointer<i32>(ast::StorageClass::kFunction))},
ty.void_(),
ast::StatementList{create<ast::CallStatement>(Call("foo", Expr("p")))});
Func("main", ast::VariableList{}, ty.void_(),
{
Decl(Var("v", ty.i32(), Expr(1))),
create<ast::CallStatement>(Call("foo", AddressOf(Expr("v")))),
},
{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverCallValidationTest, LetPointer) {
// fn x(p : ptr<function, i32>) -> i32 {}
// [[stage(fragment)]]
// fn main() {
// var v: i32;
// let p: ptr<function, i32> = &v;
// var c: i32 = x(p);
// }
Func("x", {Param("p", ty.pointer<i32>(ast::StorageClass::kFunction))},
ty.void_(), {});
auto* v = Var("v", ty.i32());
auto* p = Const("p", ty.pointer(ty.i32(), ast::StorageClass::kFunction),
AddressOf(v));
auto* c = Var("c", ty.i32(), ast::StorageClass::kNone,
Call("x", Expr(Source{{12, 34}}, p)));
Func("main", ast::VariableList{}, ty.void_(),
{
Decl(v),
Decl(p),
Decl(c),
},
{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: expected an address-of expression of a variable "
"identifier expression or a function parameter");
}
TEST_F(ResolverCallValidationTest, LetPointerPrivate) {
// let p: ptr<private, i32> = &v;
// fn foo(p : ptr<private, i32>) -> i32 {}
// var v: i32;
// [[stage(fragment)]]
// fn main() {
// var c: i32 = foo(p);
// }
Func("foo", {Param("p", ty.pointer<i32>(ast::StorageClass::kPrivate))},
ty.void_(), {});
auto* v = Global("v", ty.i32(), ast::StorageClass::kPrivate);
auto* p = Const("p", ty.pointer(ty.i32(), ast::StorageClass::kPrivate),
AddressOf(v));
auto* c = Var("c", ty.i32(), ast::StorageClass::kNone,
Call("foo", Expr(Source{{12, 34}}, p)));
Func("main", ast::VariableList{}, ty.void_(),
{
Decl(p),
Decl(c),
},
{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: expected an address-of expression of a variable "
"identifier expression or a function parameter");
}
} // namespace
} // namespace resolver
} // namespace tint