Delete Validator except public API

Now that all validation has been moved to the Resolver, we can delete
the Validator. This change removes everything except validator.h with
the public no-op API. We can remove this once we remove dependencies on
this public API in Dawn.

Bug: tint:642
Change-Id: I644cd900615509c9cdd57c375c6217a50126e36c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47023
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano 2021-04-07 16:32:01 +00:00 committed by Commit Bot service account
parent bebb1cb8f1
commit 8f42be3f60
9 changed files with 12 additions and 463 deletions

View File

@ -461,10 +461,7 @@ source_set("libtint_core_src") {
"utils/hash.h",
"utils/math.h",
"utils/unique_vector.h",
"validator/validator.cc",
"validator/validator.h",
"validator/validator_impl.cc",
"validator/validator_impl.h",
"writer/append_vector.cc",
"writer/append_vector.h",
"writer/float_to_string.cc",

View File

@ -276,10 +276,7 @@ set(TINT_LIB_SRCS
utils/hash.h
utils/math.h
utils/unique_vector.h
validator/validator.cc
validator/validator.h
validator/validator_impl.cc
validator/validator_impl.h
writer/append_vector.cc
writer/append_vector.h
writer/float_to_string.cc
@ -526,8 +523,6 @@ if(${TINT_BUILD_TESTS})
utils/tmpfile_test.cc
utils/tmpfile.h
utils/unique_vector_test.cc
validator/validator_test_helper.cc
validator/validator_test_helper.h
writer/append_vector_test.cc
writer/float_to_string_test.cc
)

View File

@ -1,32 +0,0 @@
// 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/validator.h"
#include "src/validator/validator_impl.h"
namespace tint {
Validator::Validator() = default;
Validator::~Validator() = default;
bool Validator::Validate(const Program* program) {
ValidatorImpl impl(program);
bool ret = impl.Validate();
diags_ = impl.diagnostics();
return ret;
}
} // namespace tint

View File

@ -17,29 +17,30 @@
#include <string>
#include "src/program.h"
#include "src/diagnostic/diagnostic.h"
namespace tint {
class Program;
// TODO(amaiorano): This class is deprecated. Delete after removing its usage in
// Dawn.
/// Determines if the program is complete and valid
class Validator {
public:
/// Constructor
Validator();
~Validator();
/// Runs the validator
/// @param program the program to validate
/// @returns true if the validation was successful
bool Validate(const Program* program);
bool Validate(const Program* program) {
(void)program;
return true;
}
/// @returns error messages from the validator
std::string error() {
diag::Formatter formatter{{false, false, false, false}};
return formatter.format(diags_);
}
std::string error() { return {}; }
/// @returns true if an error was encountered
bool has_error() const { return diags_.contains_errors(); }
bool has_error() const { return false; }
/// @returns the full list of diagnostic messages.
const diag::List& diagnostics() const { return diags_; }

View File

@ -1,186 +0,0 @@
// 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/validator_impl.h"
#include <utility>
#include "src/ast/call_statement.h"
#include "src/ast/constant_id_decoration.h"
#include "src/ast/fallthrough_statement.h"
#include "src/ast/sint_literal.h"
#include "src/ast/stage_decoration.h"
#include "src/ast/stride_decoration.h"
#include "src/ast/struct_block_decoration.h"
#include "src/ast/struct_member_align_decoration.h"
#include "src/ast/struct_member_offset_decoration.h"
#include "src/ast/struct_member_size_decoration.h"
#include "src/ast/uint_literal.h"
#include "src/ast/workgroup_decoration.h"
#include "src/semantic/call.h"
#include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/alias_type.h"
#include "src/type/array_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
#include "src/type/void_type.h"
namespace tint {
ValidatorImpl::ValidatorImpl(const Program* program) : program_(program) {}
ValidatorImpl::~ValidatorImpl() = default;
void ValidatorImpl::add_error(const Source& src,
const char* code,
const std::string& msg) {
diag::Diagnostic diag;
diag.severity = diag::Severity::Error;
diag.source = src;
diag.message = msg;
diag.code = code;
diags_.add(std::move(diag));
}
void ValidatorImpl::add_error(const Source& src, const std::string& msg) {
diags_.add_error(msg, src);
}
bool ValidatorImpl::Validate() {
if (!program_->IsValid()) {
// If we're attempting to validate an invalid program, fail with the
// program's diagnostics.
diags_.add(program_->Diagnostics());
return false;
}
// Validate global declarations in the order they appear in the module.
for (auto* decl : program_->AST().GlobalDeclarations()) {
if (decl->Is<type::Type>()) {
// Validated by Resolver (Struct types only)
return true;
} else if (auto* func = decl->As<ast::Function>()) {
current_function_ = func;
if (!ValidateFunction(func)) {
return false;
}
current_function_ = nullptr;
} else if (auto* var = decl->As<ast::Variable>()) {
if (!ValidateGlobalVariable(var)) {
return false;
}
} else {
TINT_UNREACHABLE(diags_);
return false;
}
}
if (!ValidateEntryPoint(program_->AST().Functions())) {
return false;
}
return true;
}
bool ValidatorImpl::ValidateGlobalVariable(const ast::Variable* var) {
variable_stack_.set_global(var->symbol(), var);
return true;
}
bool ValidatorImpl::ValidateEntryPoint(const ast::FunctionList&) {
return true;
}
bool ValidatorImpl::ValidateFunction(const ast::Function* func) {
// TODO(amaiorano): Remove ValidateFunction once we've moved all the statement
// validation to Resovler
variable_stack_.push_scope();
for (auto* param : func->params()) {
variable_stack_.set(param->symbol(), param);
}
if (!ValidateStatements(func->body())) {
return false;
}
variable_stack_.pop_scope();
return true;
}
bool ValidatorImpl::ValidateStatements(const ast::BlockStatement* block) {
if (!block) {
return false;
}
bool is_valid = true;
variable_stack_.push_scope();
for (auto* stmt : *block) {
if (!ValidateStatement(stmt)) {
is_valid = false;
break;
}
}
variable_stack_.pop_scope();
return is_valid;
}
bool ValidatorImpl::ValidateDeclStatement(
const ast::VariableDeclStatement* decl) {
auto symbol = decl->variable()->symbol();
variable_stack_.set(symbol, decl->variable());
return true;
}
bool ValidatorImpl::ValidateStatement(const ast::Statement* stmt) {
if (!stmt) {
return false;
}
if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
return ValidateDeclStatement(v);
}
if (auto* s = stmt->As<ast::SwitchStatement>()) {
return ValidateSwitch(s);
}
if (auto* c = stmt->As<ast::CaseStatement>()) {
return ValidateCase(c);
}
if (auto* b = stmt->As<ast::BlockStatement>()) {
return ValidateStatements(b);
}
return true;
}
bool ValidatorImpl::ValidateSwitch(const ast::SwitchStatement* s) {
// TODO(amaiorano): Switch validation has moved to Resolver, but we need this
// logic to validate case statements for now. Remove once ValidateStatement()
// can be removed.
for (auto* case_stmt : s->body()) {
if (!ValidateStatement(case_stmt)) {
return false;
}
}
return true;
}
bool ValidatorImpl::ValidateCase(const ast::CaseStatement* c) {
if (!ValidateStatement(c->body())) {
return false;
}
return true;
}
} // namespace tint

View File

@ -1,120 +0,0 @@
// 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_VALIDATOR_IMPL_H_
#define SRC_VALIDATOR_VALIDATOR_IMPL_H_
#include <string>
#include <unordered_set>
#include "src/ast/assignment_statement.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/module.h"
#include "src/ast/return_statement.h"
#include "src/ast/switch_statement.h"
#include "src/ast/variable_decl_statement.h"
#include "src/program.h"
#include "src/scope_stack.h"
namespace tint {
/// Determines if the program is complete and valid
class ValidatorImpl {
public:
/// Constructor
/// @param program the program to validate
explicit ValidatorImpl(const Program* program);
~ValidatorImpl();
/// Runs the validator
/// @returns true if the validation was successful
bool Validate();
/// @returns the program being validated
const Program* program() { return program_; }
/// @returns the diagnostic messages
const diag::List& diagnostics() const { return diags_; }
/// @returns the diagnostic messages
diag::List& diagnostics() { return diags_; }
/// @returns error messages from the validator
std::string error() {
diag::Formatter formatter{{false, false, false, false}};
return formatter.format(diags_);
}
/// @returns true if an error was encountered
bool has_error() const { return diags_.contains_errors(); }
/// Appends an error at `src` with the code `code` and message `msg`
/// @param src the source causing the error
/// @param code the validation error code
/// @param msg the error message
void add_error(const Source& src, const char* code, const std::string& msg);
/// Appends an error at `src` with the message `msg`
/// @param src the source causing the error
/// @param msg the error message
void add_error(const Source& src, const std::string& msg);
/// Validates a global variable
/// @param var the global variable to check
/// @returns true if the validation was successful
bool ValidateGlobalVariable(const ast::Variable* var);
/// Validates a function
/// @param func the function to check
/// @returns true if the validation was successful
bool ValidateFunction(const ast::Function* func);
/// Validates a block of statements
/// @param block the statements to check
/// @returns true if the validation was successful
bool ValidateStatements(const ast::BlockStatement* block);
/// Validates a statement
/// @param stmt the statement to check
/// @returns true if the validation was successful
bool ValidateStatement(const ast::Statement* stmt);
/// Validates declaration name uniqueness
/// @param decl is the new declaration to be added
/// @returns true if no previous declaration with the `decl` 's name
/// exist in the variable stack
bool ValidateDeclStatement(const ast::VariableDeclStatement* decl);
/// Validates switch statements
/// @param s the switch statement to check
/// @returns true if the valdiation was successful
bool ValidateSwitch(const ast::SwitchStatement* s);
/// Validates case statements
/// @param c the case statement to check
/// @returns true if the valdiation was successful
bool ValidateCase(const ast::CaseStatement* c);
/// Validates entry points
/// @param funcs the functions to check
/// @returns true if the valdiation was successful
bool ValidateEntryPoint(const ast::FunctionList& funcs);
/// Testing method to inserting a given variable into the current scope.
/// @param var the variable to register
void RegisterVariableForTesting(ast::Variable* var) {
variable_stack_.set(var->symbol(), var);
}
private:
const Program* program_;
diag::List diags_;
ScopeStack<const ast::Variable*> variable_stack_;
ast::Function* current_function_ = nullptr;
};
} // namespace tint
#endif // SRC_VALIDATOR_VALIDATOR_IMPL_H_

View File

@ -1,23 +0,0 @@
// 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/validator_test_helper.h"
namespace tint {
ValidatorTestHelper::ValidatorTestHelper() = default;
ValidatorTestHelper::~ValidatorTestHelper() = default;
} // namespace tint

View File

@ -1,81 +0,0 @@
// 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_VALIDATOR_TEST_HELPER_H_
#define SRC_VALIDATOR_VALIDATOR_TEST_HELPER_H_
#include <memory>
#include <utility>
#include <vector>
#include "gtest/gtest.h"
#include "src/program_builder.h"
#include "src/semantic/expression.h"
#include "src/validator/validator_impl.h"
namespace tint {
/// A helper for testing validation
class ValidatorTestHelper : public ProgramBuilder {
public:
/// Constructor
ValidatorTestHelper();
~ValidatorTestHelper() override;
/// Builds and returns a validator from the program.
/// @note The validator is only built once. Multiple calls to Build() will
/// return the same ValidatorImpl without rebuilding.
/// @return the built validator
ValidatorImpl& Build() {
if (val_) {
return *val_;
}
program_ = std::make_unique<Program>(std::move(*this));
[&]() {
ASSERT_TRUE(program_->IsValid())
<< diag::Formatter().format(program_->Diagnostics());
}();
val_ = std::make_unique<ValidatorImpl>(program_.get());
for (auto* var : vars_for_testing_) {
val_->RegisterVariableForTesting(var);
}
return *val_;
}
/// Inserts a variable into the current scope.
/// @param var the variable to register.
void RegisterVariable(ast::Variable* var) {
AST().AddGlobalVariable(var);
vars_for_testing_.emplace_back(var);
}
/// Helper for returning the resolved semantic type of the expression `expr`
/// from the built program.
/// @param expr the AST expression
/// @return the resolved semantic type for the expression, or nullptr if the
/// expression has no resolved type.
type::Type* TypeOf(ast::Expression* expr) const {
auto* sem = program_->Sem().Get(expr);
return sem ? sem->Type() : nullptr;
}
private:
std::unique_ptr<Program> program_;
std::unique_ptr<ValidatorImpl> val_;
std::vector<ast::Variable*> vars_for_testing_;
};
} // namespace tint
#endif // SRC_VALIDATOR_VALIDATOR_TEST_HELPER_H_

View File

@ -226,8 +226,6 @@ source_set("tint_unittests_core_src") {
"../src/utils/tmpfile.h",
"../src/utils/tmpfile_test.cc",
"../src/utils/unique_vector_test.cc",
"../src/validator/validator_test_helper.cc",
"../src/validator/validator_test_helper.h",
"../src/writer/append_vector_test.cc",
"../src/writer/float_to_string_test.cc",
]