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>
This commit is contained in:
parent
afc144ee95
commit
462dd675e1
|
@ -174,6 +174,8 @@ set(TINT_LIB_SRCS
|
||||||
type_manager.h
|
type_manager.h
|
||||||
validator.cc
|
validator.cc
|
||||||
validator.h
|
validator.h
|
||||||
|
validator_impl.cc
|
||||||
|
validator_impl.h
|
||||||
# TODO(dsinclair): The writers should all be optional
|
# TODO(dsinclair): The writers should all be optional
|
||||||
writer/spv/generator.cc
|
writer/spv/generator.cc
|
||||||
writer/spv/generator.h
|
writer/spv/generator.h
|
||||||
|
@ -283,6 +285,7 @@ set(TINT_TEST_SRCS
|
||||||
reader/wgsl/parser_impl_variable_storage_decoration_test.cc
|
reader/wgsl/parser_impl_variable_storage_decoration_test.cc
|
||||||
reader/wgsl/token_test.cc
|
reader/wgsl/token_test.cc
|
||||||
type_manager_test.cc
|
type_manager_test.cc
|
||||||
|
validator_impl_import_test.cc
|
||||||
)
|
)
|
||||||
|
|
||||||
## Tint library
|
## Tint library
|
||||||
|
|
|
@ -43,7 +43,9 @@ class Module {
|
||||||
imports_.push_back(std::move(import));
|
imports_.push_back(std::move(import));
|
||||||
}
|
}
|
||||||
/// @returns the imports for this module
|
/// @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
|
/// Find the import of the given name
|
||||||
/// @param name The import name to search for
|
/// @param name The import name to search for
|
||||||
/// @returns the import with the given name if found, nullptr otherwise.
|
/// @returns the import with the given name if found, nullptr otherwise.
|
||||||
|
|
|
@ -14,14 +14,21 @@
|
||||||
|
|
||||||
#include "src/validator.h"
|
#include "src/validator.h"
|
||||||
|
|
||||||
|
#include "src/validator_impl.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
|
|
||||||
Validator::Validator() = default;
|
Validator::Validator() : impl_(std::make_unique<tint::ValidatorImpl>()) {}
|
||||||
|
|
||||||
Validator::~Validator() = default;
|
Validator::~Validator() = default;
|
||||||
|
|
||||||
bool Validator::Validate(const ast::Module&) {
|
bool Validator::Validate(const ast::Module& module) {
|
||||||
return true;
|
bool ret = impl_->Validate(module);
|
||||||
|
|
||||||
|
if (impl_->has_error())
|
||||||
|
set_error(impl_->error());
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
|
|
@ -15,12 +15,16 @@
|
||||||
#ifndef SRC_VALIDATOR_H_
|
#ifndef SRC_VALIDATOR_H_
|
||||||
#define SRC_VALIDATOR_H_
|
#define SRC_VALIDATOR_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "src/ast/module.h"
|
#include "src/ast/module.h"
|
||||||
|
#include "src/validator_impl.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
|
|
||||||
|
class ValidatorImpl;
|
||||||
|
|
||||||
/// Determines if the module is complete and valid
|
/// Determines if the module is complete and valid
|
||||||
class Validator {
|
class Validator {
|
||||||
public:
|
public:
|
||||||
|
@ -35,8 +39,14 @@ class Validator {
|
||||||
|
|
||||||
/// @returns error messages from the validator
|
/// @returns error messages from the validator
|
||||||
const std::string& error() { return error_; }
|
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:
|
private:
|
||||||
|
std::unique_ptr<ValidatorImpl> impl_;
|
||||||
std::string error_;
|
std::string error_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -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_
|
|
@ -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
|
Loading…
Reference in New Issue