Fix all tests so TypeDetermination runs without errors

If TypeDetermination fails, then semantic info may be missing.
We don't want to have to guard against missing semantic nodes in each writer.

Bug: tint:390
Change-Id: I3124b514ce88f83bd3f75747c4ec6c960282f3c6
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40141
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-02-03 23:42:26 +00:00 committed by Commit Bot service account
parent ea9c86c296
commit df629c5404
7 changed files with 61 additions and 17 deletions

View File

@ -37,7 +37,10 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_Loop) {
auto* body = create<ast::BlockStatement>(ast::StatementList{ auto* body = create<ast::BlockStatement>(ast::StatementList{
create<ast::DiscardStatement>(), create<ast::DiscardStatement>(),
}); });
auto* l = create<ast::LoopStatement>(body, nullptr); auto* continuing = create<ast::BlockStatement>(ast::StatementList{});
auto* l = create<ast::LoopStatement>(body, continuing);
WrapInFunction(l);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -59,6 +62,8 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopWithContinuing) {
}); });
auto* l = create<ast::LoopStatement>(body, continuing); auto* l = create<ast::LoopStatement>(body, continuing);
WrapInFunction(l);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); gen.increment_indent();
@ -79,6 +84,9 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopWithContinuing) {
} }
TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopNestedWithContinuing) { TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopNestedWithContinuing) {
Global("lhs", ast::StorageClass::kNone, ty.f32());
Global("rhs", ast::StorageClass::kNone, ty.f32());
auto* body = create<ast::BlockStatement>(ast::StatementList{ auto* body = create<ast::BlockStatement>(ast::StatementList{
create<ast::DiscardStatement>(), create<ast::DiscardStatement>(),
}); });
@ -99,6 +107,7 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopNestedWithContinuing) {
}); });
auto* outer = create<ast::LoopStatement>(body, continuing); auto* outer = create<ast::LoopStatement>(body, continuing);
WrapInFunction(outer);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -151,6 +160,8 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopWithVarUsedInContinuing) {
// } // }
// } // }
Global("rhs", ast::StorageClass::kNone, ty.f32());
auto* var = Var("lhs", ast::StorageClass::kFunction, ty.f32(), Expr(2.4f), auto* var = Var("lhs", ast::StorageClass::kFunction, ty.f32(), Expr(2.4f),
ast::VariableDecorationList{}); ast::VariableDecorationList{});
@ -167,6 +178,7 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopWithVarUsedInContinuing) {
create<ast::AssignmentStatement>(lhs, rhs), create<ast::AssignmentStatement>(lhs, rhs),
}); });
auto* outer = create<ast::LoopStatement>(body, continuing); auto* outer = create<ast::LoopStatement>(body, continuing);
WrapInFunction(outer);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();

View File

@ -33,7 +33,7 @@ namespace {
using HlslGeneratorImplTest_VariableDecl = TestHelper; using HlslGeneratorImplTest_VariableDecl = TestHelper;
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement) {
auto* var = Var("a", ast::StorageClass::kNone, ty.f32()); auto* var = Global("a", ast::StorageClass::kNone, ty.f32());
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
@ -59,7 +59,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) {
} }
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
auto* var = Var("a", ast::StorageClass::kNone, ty.array<f32, 5>()); auto* var = Global("a", ast::StorageClass::kNone, ty.array<f32, 5>());
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
@ -73,7 +73,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
TEST_F(HlslGeneratorImplTest_VariableDecl, TEST_F(HlslGeneratorImplTest_VariableDecl,
Emit_VariableDeclStatement_Function) { Emit_VariableDeclStatement_Function) {
auto* var = Var("a", ast::StorageClass::kFunction, ty.f32()); auto* var = Global("a", ast::StorageClass::kFunction, ty.f32());
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
@ -86,7 +86,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
} }
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
auto* var = Var("a", ast::StorageClass::kPrivate, ty.f32()); auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32());
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
@ -100,8 +100,9 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
TEST_F(HlslGeneratorImplTest_VariableDecl, TEST_F(HlslGeneratorImplTest_VariableDecl,
Emit_VariableDeclStatement_Initializer_Private) { Emit_VariableDeclStatement_Initializer_Private) {
auto* var = Var("a", ast::StorageClass::kNone, ty.f32(), Expr("initializer"), Global("initializer", ast::StorageClass::kNone, ty.f32());
ast::VariableDecorationList{}); auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32(),
Expr("initializer"), ast::VariableDecorationList{});
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
@ -114,10 +115,11 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
TEST_F(HlslGeneratorImplTest_VariableDecl, TEST_F(HlslGeneratorImplTest_VariableDecl,
Emit_VariableDeclStatement_Initializer_ZeroVec) { Emit_VariableDeclStatement_Initializer_ZeroVec) {
auto* var = Var("a", ast::StorageClass::kNone, ty.vec3<f32>(), vec3<f32>(), auto* var = Var("a", ast::StorageClass::kFunction, ty.vec3<f32>(),
ast::VariableDecorationList{}); vec3<f32>(), ast::VariableDecorationList{});
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -128,10 +130,11 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
TEST_F(HlslGeneratorImplTest_VariableDecl, TEST_F(HlslGeneratorImplTest_VariableDecl,
Emit_VariableDeclStatement_Initializer_ZeroMat) { Emit_VariableDeclStatement_Initializer_ZeroMat) {
auto* var = Var("a", ast::StorageClass::kNone, ty.mat2x3<f32>(), auto* var = Var("a", ast::StorageClass::kFunction, ty.mat2x3<f32>(),
mat2x3<f32>(), ast::VariableDecorationList{}); mat2x3<f32>(), ast::VariableDecorationList{});
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();

View File

@ -21,6 +21,7 @@
#include <utility> #include <utility>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/diagnostic/formatter.h"
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/generator_impl.h"
@ -44,7 +45,15 @@ class TestHelperBase : public BODY, public ProgramBuilder {
if (gen_) { if (gen_) {
return *gen_; return *gen_;
} }
[&]() {
ASSERT_TRUE(IsValid()) << "Builder program is not valid\n"
<< diag::Formatter().format(Diagnostics());
}();
program = std::make_unique<Program>(std::move(*this)); program = std::make_unique<Program>(std::move(*this));
[&]() {
ASSERT_TRUE(program->IsValid())
<< diag::Formatter().format(program->Diagnostics());
}();
gen_ = std::make_unique<GeneratorImpl>(program.get()); gen_ = std::make_unique<GeneratorImpl>(program.get());
return *gen_; return *gen_;
} }

View File

@ -39,7 +39,8 @@ TEST_F(MslGeneratorImplTest, Emit_Loop) {
auto* body = create<ast::BlockStatement>(ast::StatementList{ auto* body = create<ast::BlockStatement>(ast::StatementList{
create<ast::DiscardStatement>(), create<ast::DiscardStatement>(),
}); });
auto* l = create<ast::LoopStatement>(body, nullptr); auto* continuing = create<ast::BlockStatement>(ast::StatementList{});
auto* l = create<ast::LoopStatement>(body, continuing);
WrapInFunction(l); WrapInFunction(l);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -83,6 +84,9 @@ TEST_F(MslGeneratorImplTest, Emit_LoopWithContinuing) {
} }
TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) { TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
Global("lhs", ast::StorageClass::kNone, ty.f32());
Global("rhs", ast::StorageClass::kNone, ty.f32());
auto* body = create<ast::BlockStatement>(ast::StatementList{ auto* body = create<ast::BlockStatement>(ast::StatementList{
create<ast::DiscardStatement>(), create<ast::DiscardStatement>(),
}); });
@ -153,6 +157,8 @@ TEST_F(MslGeneratorImplTest, Emit_LoopWithVarUsedInContinuing) {
// } // }
// } // }
Global("rhs", ast::StorageClass::kNone, ty.f32());
auto* var = Var("lhs", ast::StorageClass::kFunction, ty.f32(), Expr(2.4f), auto* var = Var("lhs", ast::StorageClass::kFunction, ty.f32(), Expr(2.4f),
ast::VariableDecorationList{}); ast::VariableDecorationList{});

View File

@ -29,6 +29,12 @@ namespace {
using MslGeneratorImplTest = TestHelper; using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor) { TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor) {
Global("str", ast::StorageClass::kPrivate,
ty.struct_("my_str", create<ast::Struct>(
ast::StructMemberList{
Member("mem", ty.f32()),
},
ast::StructDecorationList{})));
auto* expr = MemberAccessor("str", "mem"); auto* expr = MemberAccessor("str", "mem");
WrapInFunction(expr); WrapInFunction(expr);

View File

@ -129,9 +129,8 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Matrix) {
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
auto* var = Var("a", ast::StorageClass::kPrivate, ty.f32()); auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32());
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -142,10 +141,10 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
auto* var = Var("a", ast::StorageClass::kNone, ty.f32(), Expr("initializer"), Global("initializer", ast::StorageClass::kNone, ty.f32());
ast::VariableDecorationList{}); auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32(),
Expr("initializer"), ast::VariableDecorationList{});
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -157,7 +156,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
auto* zero_vec = vec3<f32>(); auto* zero_vec = vec3<f32>();
auto* var = Var("a", ast::StorageClass::kNone, ty.vec3<f32>(), zero_vec, auto* var = Var("a", ast::StorageClass::kFunction, ty.vec3<f32>(), zero_vec,
ast::VariableDecorationList{}); ast::VariableDecorationList{});
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt); WrapInFunction(stmt);

View File

@ -20,6 +20,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/diagnostic/formatter.h"
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/msl/generator_impl.h" #include "src/writer/msl/generator_impl.h"
@ -43,7 +44,15 @@ class TestHelperBase : public BASE, public ProgramBuilder {
if (gen_) { if (gen_) {
return *gen_; return *gen_;
} }
[&]() {
ASSERT_TRUE(IsValid()) << "Builder program is not valid\n"
<< diag::Formatter().format(Diagnostics());
}();
program = std::make_unique<Program>(std::move(*this)); program = std::make_unique<Program>(std::move(*this));
[&]() {
ASSERT_TRUE(program->IsValid())
<< diag::Formatter().format(program->Diagnostics());
}();
gen_ = std::make_unique<GeneratorImpl>(program.get()); gen_ = std::make_unique<GeneratorImpl>(program.get());
return *gen_; return *gen_;
} }