From 1e31f161db804b6c824962ca58c6779fabcc4e42 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 17 Mar 2020 14:59:06 +0000 Subject: [PATCH] Add Variable test for invalid initializer. This CL adds a test to the variable AST node to verify the initializer is valid. Bug: tint:11 Change-Id: I95553e8572124e8e2e861b451003e5c9bf6358f1 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16743 Reviewed-by: Sarah Mashayekhi --- src/ast/variable.cc | 3 +++ src/ast/variable_test.cc | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/ast/variable.cc b/src/ast/variable.cc index d9990b8b2c..9b029984e6 100644 --- a/src/ast/variable.cc +++ b/src/ast/variable.cc @@ -44,6 +44,9 @@ bool Variable::IsValid() const { if (type_ == nullptr) { return false; } + if (initializer_ && !initializer_->IsValid()) { + return false; + } return true; } diff --git a/src/ast/variable_test.cc b/src/ast/variable_test.cc index 8156d239f7..26dc5152d6 100644 --- a/src/ast/variable_test.cc +++ b/src/ast/variable_test.cc @@ -15,6 +15,7 @@ #include "src/ast/variable.h" #include "gtest/gtest.h" +#include "src/ast/identifier_expression.h" #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" @@ -69,6 +70,13 @@ TEST_F(VariableTest, IsValid) { EXPECT_TRUE(v.IsValid()); } +TEST_F(VariableTest, IsValid_WithInitializer) { + type::I32Type t; + Variable v{"my_var", StorageClass::kNone, &t}; + v.set_initializer(std::make_unique("ident")); + EXPECT_TRUE(v.IsValid()); +} + TEST_F(VariableTest, IsValid_MissinName) { type::I32Type t; Variable v{"", StorageClass::kNone, &t}; @@ -85,6 +93,13 @@ TEST_F(VariableTest, IsValid_MissingBoth) { EXPECT_FALSE(v.IsValid()); } +TEST_F(VariableTest, IsValid_InvalidInitializer) { + type::I32Type t; + Variable v{"my_var", StorageClass::kNone, &t}; + v.set_initializer(std::make_unique("")); + EXPECT_FALSE(v.IsValid()); +} + TEST_F(VariableTest, to_str) { type::F32Type t; Variable v{"my_var", StorageClass::kFunction, &t};