v-0001: Only allowed import is GLSL.std.450
Bug: tint:10
Change-Id: I566ff378c4cd72febc0a73434b5dfe1039ef2c42
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16420
Reviewed-by: Dan Sinclair <dsinclair@google.com>
Commit-Queue: Dan Sinclair <dsinclair@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 30b6e04..9d6cad9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -174,6 +174,8 @@
type_manager.h
validator.cc
validator.h
+ validator_impl.cc
+ validator_impl.h
# TODO(dsinclair): The writers should all be optional
writer/spv/generator.cc
writer/spv/generator.h
@@ -283,6 +285,7 @@
reader/wgsl/parser_impl_variable_storage_decoration_test.cc
reader/wgsl/token_test.cc
type_manager_test.cc
+ validator_impl_import_test.cc
)
## Tint library
diff --git a/src/ast/module.h b/src/ast/module.h
index e0d1a7e..cfe26e7 100644
--- a/src/ast/module.h
+++ b/src/ast/module.h
@@ -43,7 +43,9 @@
imports_.push_back(std::move(import));
}
/// @returns the imports for this module
- const std::vector<std::unique_ptr<Import>>& imports() { return imports_; }
+ const std::vector<std::unique_ptr<Import>>& imports() const {
+ return imports_;
+ }
/// Find the import of the given name
/// @param name The import name to search for
/// @returns the import with the given name if found, nullptr otherwise.
diff --git a/src/validator.cc b/src/validator.cc
index 1ee1c4c..d190fca 100644
--- a/src/validator.cc
+++ b/src/validator.cc
@@ -14,14 +14,21 @@
#include "src/validator.h"
+#include "src/validator_impl.h"
+
namespace tint {
-Validator::Validator() = default;
+Validator::Validator() : impl_(std::make_unique<tint::ValidatorImpl>()) {}
Validator::~Validator() = default;
-bool Validator::Validate(const ast::Module&) {
- return true;
+bool Validator::Validate(const ast::Module& module) {
+ bool ret = impl_->Validate(module);
+
+ if (impl_->has_error())
+ set_error(impl_->error());
+
+ return ret;
}
} // namespace tint
diff --git a/src/validator.h b/src/validator.h
index 32c64b7..90400e1 100644
--- a/src/validator.h
+++ b/src/validator.h
@@ -15,12 +15,16 @@
#ifndef SRC_VALIDATOR_H_
#define SRC_VALIDATOR_H_
+#include <memory>
#include <string>
#include "src/ast/module.h"
+#include "src/validator_impl.h"
namespace tint {
+class ValidatorImpl;
+
/// Determines if the module is complete and valid
class Validator {
public:
@@ -35,8 +39,14 @@
/// @returns error messages from the validator
const std::string& error() { return error_; }
+ /// @returns true if an error was encountered
+ bool has_error() const { return error_.size() > 0; }
+ /// Sets the error string
+ /// @param msg the error message
+ void set_error(const std::string& msg) { error_ = msg; }
private:
+ std::unique_ptr<ValidatorImpl> impl_;
std::string error_;
};
diff --git a/src/validator_impl.cc b/src/validator_impl.cc
new file mode 100644
index 0000000..57fadba
--- /dev/null
+++ b/src/validator_impl.cc
@@ -0,0 +1,44 @@
+// Copyright 2020 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/validator_impl.h"
+
+namespace tint {
+
+ValidatorImpl::ValidatorImpl() = default;
+
+ValidatorImpl::~ValidatorImpl() = default;
+
+void ValidatorImpl::set_error(const Source& src, const std::string& msg) {
+ error_ =
+ std::to_string(src.line) + ":" + std::to_string(src.column) + ": " + msg;
+}
+
+bool ValidatorImpl::Validate(const ast::Module& module) {
+ if (!CheckImports(module))
+ return false;
+ return true;
+}
+
+bool ValidatorImpl::CheckImports(const ast::Module& module) {
+ for (const auto& import : module.imports()) {
+ if (import->path() != "GLSL.std.450") {
+ set_error(import->source(), "v-0001: unknown import: " + import->path());
+ return false;
+ }
+ }
+ return true;
+}
+
+} // namespace tint
diff --git a/src/validator_impl.h b/src/validator_impl.h
new file mode 100644
index 0000000..414bf48
--- /dev/null
+++ b/src/validator_impl.h
@@ -0,0 +1,58 @@
+// Copyright 2020 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.
+
+#ifndef SRC_VALIDATOR_IMPL_H_
+#define SRC_VALIDATOR_IMPL_H_
+
+#include <string>
+
+#include "src/ast/module.h"
+
+namespace tint {
+
+/// Determines if the module is complete and valid
+class ValidatorImpl {
+ public:
+ /// Constructor
+ ValidatorImpl();
+ ~ValidatorImpl();
+
+ /// Runs the validator
+ /// @param module the module to validate
+ /// @returns true if the validation was successful
+ bool Validate(const ast::Module& module);
+
+ /// @returns error messages from the validator
+ const std::string& error() { return error_; }
+
+ /// @returns true if an error was encountered
+ bool has_error() const { return error_.size() > 0; }
+
+ /// Sets the error string
+ /// @param src the source causing the error
+ /// @param msg the error message
+ void set_error(const Source& src, const std::string& msg);
+
+ /// Validates v-0001: Only allowed import is "GLSL.std.450"
+ /// @param module the modele to check imports
+ /// @returns ture if input complies with v-0001 rule
+ bool CheckImports(const ast::Module& module);
+
+ private:
+ std::string error_;
+};
+
+} // namespace tint
+
+#endif // SRC_VALIDATOR_IMPL_H_
diff --git a/src/validator_impl_import_test.cc b/src/validator_impl_import_test.cc
new file mode 100644
index 0000000..5b17e06
--- /dev/null
+++ b/src/validator_impl_import_test.cc
@@ -0,0 +1,56 @@
+// Copyright 2020 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 <iostream>
+#include "gtest/gtest.h"
+#include "src/reader/wgsl/parser.h"
+#include "src/validator_impl.h"
+
+namespace tint {
+
+using ValidatorImplTest = testing::Test;
+
+ast::Module build_module(std::string data) {
+ auto reader = std::make_unique<tint::reader::wgsl::Parser>(
+ std::string(data.begin(), data.end()));
+ assert(reader->Parse());
+ return reader->module();
+}
+
+TEST_F(ValidatorImplTest, Import) {
+ std::string input = "import \"GLSL.std.450\" as glsl;";
+ auto module = build_module(input);
+ tint::ValidatorImpl v;
+ EXPECT_TRUE(v.CheckImports(module));
+}
+
+TEST_F(ValidatorImplTest, Import_Fail_NotGLSL) {
+ std::string input = "import \"not.GLSL\" as glsl;";
+ auto module = build_module(input);
+ tint::ValidatorImpl v;
+ EXPECT_FALSE(v.CheckImports(module));
+ ASSERT_TRUE(v.has_error());
+ EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: not.GLSL");
+}
+
+TEST_F(ValidatorImplTest, Import_Fail_Typo) {
+ std::string input = "import \"GLSL.std.4501\" as glsl;";
+ auto module = build_module(input);
+ tint::ValidatorImpl v;
+ EXPECT_FALSE(v.CheckImports(module));
+ ASSERT_TRUE(v.has_error());
+ EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: GLSL.std.4501");
+}
+
+} // namespace tint