[validation] add const variables shouldn't have a storage class

This CL adds a rule to separate global variables and global consts in regards to storage class:
v-0022: Global variables must have a storage class
v-global01: const variables shouldn't have a storage class

Bug: tint: 225
Change-Id: I53d84afd771c78d91eaf47568e954986abf38e58
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/28141
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: David Neto <dneto@google.com>
This commit is contained in:
Sarah Mashayekhi 2020-09-03 17:20:07 +00:00 committed by Commit Bot service account
parent 75c492a0cc
commit b693b4c10d
2 changed files with 33 additions and 1 deletions

View File

@ -62,11 +62,17 @@ bool ValidatorImpl::ValidateGlobalVariables(
"v-0011: redeclared global identifier '" + var->name() + "'"); "v-0011: redeclared global identifier '" + var->name() + "'");
return false; return false;
} }
if (var->storage_class() == ast::StorageClass::kNone) { if (!var->is_const() && var->storage_class() == ast::StorageClass::kNone) {
set_error(var->source(), set_error(var->source(),
"v-0022: global variables must have a storage class"); "v-0022: global variables must have a storage class");
return false; return false;
} }
if (var->is_const() &&
!(var->storage_class() == ast::StorageClass::kNone)) {
set_error(var->source(),
"v-global01: global constants shouldn't have a storage class");
return false;
}
variable_stack_.set_global(var->name(), var.get()); variable_stack_.set_global(var->name(), var.get());
} }
return true; return true;

View File

@ -280,6 +280,32 @@ TEST_F(ValidatorTest, GlobalVariableNoStorageClass_Fail) {
EXPECT_EQ(v()->error(), EXPECT_EQ(v()->error(),
"12:34: v-0022: global variables must have a storage class"); "12:34: v-0022: global variables must have a storage class");
} }
TEST_F(ValidatorTest, GlobalConstantWithStorageClass_Fail) {
// const<in> gloabl_var: f32;
ast::type::F32Type f32;
auto global_var = std::make_unique<ast::Variable>(
Source{12, 34}, "global_var", ast::StorageClass::kInput, &f32);
global_var->set_is_const(true);
mod()->AddGlobalVariable(std::move(global_var));
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_FALSE(v()->Validate(mod()));
EXPECT_EQ(
v()->error(),
"12:34: v-global01: global constants shouldn't have a storage class");
}
TEST_F(ValidatorTest, GlobalConstNoStorageClass_Pass) {
// const gloabl_var: f32;
ast::type::F32Type f32;
auto global_var = std::make_unique<ast::Variable>(
Source{12, 34}, "global_var", ast::StorageClass::kNone, &f32);
global_var->set_is_const(true);
mod()->AddGlobalVariable(std::move(global_var));
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_FALSE(v()->Validate(mod())) << v()->error();
}
TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) { TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
// var global_var: f32 = 2.1; // var global_var: f32 = 2.1;