diff --git a/src/ast/module.cc b/src/ast/module.cc index e5c5015d71..dfca88a479 100644 --- a/src/ast/module.cc +++ b/src/ast/module.cc @@ -29,8 +29,29 @@ const Import* Module::FindImportByName(const std::string& name) { bool Module::IsValid() const { for (const auto& import : imports_) { - if (!import->IsValid()) + if (import == nullptr || !import->IsValid()) { return false; + } + } + for (const auto& var : global_variables_) { + if (var == nullptr || !var->IsValid()) { + return false; + } + } + for (const auto& ep : entry_points_) { + if (ep == nullptr || !ep->IsValid()) { + return false; + } + } + for (const auto& alias : alias_types_) { + if (alias == nullptr) { + return false; + } + } + for (const auto& func : functions_) { + if (func == nullptr || !func->IsValid()) { + return false; + } } return true; } diff --git a/src/ast/module_test.cc b/src/ast/module_test.cc index 2cf16267b0..64fd31b2f7 100644 --- a/src/ast/module_test.cc +++ b/src/ast/module_test.cc @@ -17,6 +17,11 @@ #include #include "gtest/gtest.h" +#include "src/ast/entry_point.h" +#include "src/ast/function.h" +#include "src/ast/import.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/variable.h" namespace tint { namespace ast { @@ -62,17 +67,104 @@ TEST_F(ModuleTest, IsValid_Empty) { EXPECT_TRUE(m.IsValid()); } -TEST_F(ModuleTest, IsValid_InvalidImport) { - Module m; - m.AddImport(std::make_unique()); - EXPECT_FALSE(m.IsValid()); -} - -TEST_F(ModuleTest, IsValid_ValidImport) { +TEST_F(ModuleTest, IsValid_Import) { Module m; m.AddImport(std::make_unique("GLSL.std.430", "std::glsl")); EXPECT_TRUE(m.IsValid()); } +TEST_F(ModuleTest, IsValid_Null_Import) { + Module m; + m.AddImport(nullptr); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Invalid_Import) { + Module m; + m.AddImport(std::make_unique()); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_GlobalVariable) { + type::F32Type f32; + auto var = std::make_unique("var", StorageClass::kInput, &f32); + + Module m; + m.AddGlobalVariable(std::move(var)); + EXPECT_TRUE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Null_GlobalVariable) { + Module m; + m.AddGlobalVariable(nullptr); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) { + auto var = std::make_unique("var", StorageClass::kInput, nullptr); + + Module m; + m.AddGlobalVariable(std::move(var)); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_EntryPoint) { + Module m; + m.AddEntryPoint( + std::make_unique(PipelineStage::kVertex, "main", "vtx_main")); + EXPECT_TRUE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Null_EntryPoint) { + Module m; + m.AddEntryPoint(nullptr); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Invalid_EntryPoint) { + Module m; + m.AddEntryPoint(std::make_unique()); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Alias) { + type::F32Type f32; + type::AliasType alias("alias", &f32); + + Module m; + m.AddAliasType(&alias); + EXPECT_TRUE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Null_Alias) { + Module m; + m.AddAliasType(nullptr); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Function) { + type::F32Type f32; + auto func = std::make_unique( + "main", std::vector>(), &f32); + + Module m; + m.AddFunction(std::move(func)); + EXPECT_TRUE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Null_Function) { + Module m; + m.AddFunction(nullptr); + EXPECT_FALSE(m.IsValid()); +} + +TEST_F(ModuleTest, IsValid_Invalid_Function) { + auto func = std::make_unique(); + + Module m; + m.AddFunction(std::move(func)); + EXPECT_FALSE(m.IsValid()); +} + } // namespace ast } // namespace tint