Expand build flags for Tint.

This CL extends the build options to Tint to make the various readers
and writers all optional.

Change-Id: I913e1830b1bb2243eff5deb4b8079ba592dd52e1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16801
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-18 14:08:48 +00:00 committed by dan sinclair
parent e4392c9ab5
commit 4b71b9ed2b
8 changed files with 265 additions and 168 deletions

View File

@ -27,15 +27,21 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
endif() endif()
option(TINT_BUILD_DOCS "Build documentation" ON) option(TINT_BUILD_DOCS "Build documentation" ON)
option(TINT_BUILD_SPV_PARSER "Build the SPIR-V input parser" OFF) option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" ON)
option(TINT_BUILD_FUZZERS "Build fuzzers" OFF) option(TINT_BUILD_WGSL_READER "Builde the WGSL input reader" ON)
option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
option(TINT_BUILD_FUZZERS "Build fuzzers" OFF)
option(TINT_ENABLE_MSAN "Enable memory sanitizer" OFF) option(TINT_ENABLE_MSAN "Enable memory sanitizer" OFF)
option(TINT_ENABLE_ASAN "Enable address sanitizer" OFF) option(TINT_ENABLE_ASAN "Enable address sanitizer" OFF)
option(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF) option(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}") message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
message(STATUS "Tint build SPIR-V parser: ${TINT_BUILD_SPV_PARSER}") message(STATUS "Tint build SPIR-V reader: ${TINT_BUILD_SPV_READER}")
message(STATUS "Tint build WGSL reader: ${TINT_BUILD_WGSL_READER}")
message(STATUS "Tint build SPIR-V writer: ${TINT_BUILD_SPV_WRITER}")
message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}") message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}")
message(STATUS "Tint build with ASAN: ${TINT_ENABLE_ASAN}") message(STATUS "Tint build with ASAN: ${TINT_ENABLE_ASAN}")
message(STATUS "Tint build with MSAN: ${TINT_ENABLE_MSAN}") message(STATUS "Tint build with MSAN: ${TINT_ENABLE_MSAN}")
@ -44,7 +50,7 @@ message(STATUS "Tint build with UBSAN: ${TINT_ENABLE_UBSAN}")
message(STATUS "Using python3") message(STATUS "Using python3")
find_package(PythonInterp 3 REQUIRED) find_package(PythonInterp 3 REQUIRED)
if (${TINT_BUILD_SPV_PARSER}) if (${TINT_BUILD_SPV_READER})
include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include") include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include")
endif() endif()
@ -72,7 +78,13 @@ function(tint_default_compile_options TARGET)
include_directories("${PROJECT_SOURCE_DIR}") include_directories("${PROJECT_SOURCE_DIR}")
target_compile_definitions(${TARGET} PRIVATE target_compile_definitions(${TARGET} PRIVATE
-DTINT_BUILD_SPV_PARSER=$<BOOL:${TINT_BUILD_SPV_PARSER}>) -DTINT_BUILD_SPV_READER=$<BOOL:${TINT_BUILD_SPV_READER}>)
target_compile_definitions(${TARGET} PRIVATE
-DTINT_BUILD_WGSL_READER=$<BOOL:${TINT_BUILD_WGSL_READER}>)
target_compile_definitions(${TARGET} PRIVATE
-DTINT_BUILD_SPV_WRITER=$<BOOL:${TINT_BUILD_SPV_WRITER}>)
target_compile_definitions(${TARGET} PRIVATE
-DTINT_BUILD_WGSL_WRITER=$<BOOL:${TINT_BUILD_WGSL_WRITER}>)
if (${COMPILER_IS_LIKE_GNU}) if (${COMPILER_IS_LIKE_GNU})
target_compile_options(${TARGET} PRIVATE target_compile_options(${TARGET} PRIVATE
@ -140,6 +152,10 @@ add_subdirectory(src)
add_subdirectory(samples) add_subdirectory(samples)
if (${TINT_BUILD_FUZZERS}) if (${TINT_BUILD_FUZZERS})
if (NOT ${TINT_BUILD_WGSL_READER)
message(ERROR, "Fuzzers require WGSL reader to be enabled")
endif()
add_subdirectory(fuzz) add_subdirectory(fuzz)
endif() endif()

View File

@ -11,7 +11,10 @@ This is not an officially supported Google product.
* Python, for fetching dependencies * Python, for fetching dependencies
## Build options ## Build options
* `TINT_BUILD_SPV_PARSER` : enable the SPIR-V input parser * `TINT_BUILD_SPV_READER` : enable the SPIR-V input reader (off by default)
* `TINT_BUILD_WGSL_READER` : enable the WGSL input reader (on by default)
* `TINT_BUILD_SPV_WRITER` : enable the SPIR-V output writer (on by default)
* `TINT_BUILD_WGSL_WRITER` : enable the WGSL output writer (on by default)
## Building ## Building
Tint uses Chromium dependency management so you need to [install depot_tools] and add it to your PATH. Tint uses Chromium dependency management so you need to [install depot_tools] and add it to your PATH.

View File

@ -18,6 +18,11 @@ set(TINT_SRCS
## Tint executable ## Tint executable
add_executable(tint ${TINT_SRCS}) add_executable(tint ${TINT_SRCS})
target_link_libraries(tint libtint SPIRV-Tools)
tint_default_compile_options(tint) tint_default_compile_options(tint)
target_link_libraries(tint libtint)
if(${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
target_link_libraries(tint SPIRV-Tools)
endif()

View File

@ -17,18 +17,27 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "spirv-tools/libspirv.hpp"
#include "src/reader/reader.h" #include "src/reader/reader.h"
#include "src/reader/wgsl/parser.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/validator.h" #include "src/validator.h"
#include "src/writer/spirv/generator.h"
#include "src/writer/wgsl/generator.h"
#include "src/writer/writer.h" #include "src/writer/writer.h"
#if TINT_BUILD_SPV_PARSER #if TINT_BUILD_SPV_READER
#include "src/reader/spirv/parser.h" #include "src/reader/spirv/parser.h"
#endif #endif // TINT_BUILD_SPV_READER
#if TINT_BUILD_WGSL_READER
#include "src/reader/wgsl/parser.h"
#endif // TINT_BUILD_WGSL_READER
#if TINT_BUILD_SPV_WRITER
#include "spirv-tools/libspirv.hpp"
#include "src/writer/spirv/generator.h"
#endif // TINT_BUILD_SPV_WRITER
#if TINT_BUILD_WGSL_WRITER
#include "src/writer/wgsl/generator.h"
#endif // TINT_BUILD_WGSL_WRITER
namespace { namespace {
@ -61,13 +70,22 @@ const char kUsage[] = R"(Usage: tint [options] SCRIPT [SCRIPTS...]
--dump-ast -- Dump the generated AST to stdout --dump-ast -- Dump the generated AST to stdout
-h -- This help text)"; -h -- This help text)";
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
Format parse_format(const std::string& fmt) { Format parse_format(const std::string& fmt) {
#pragma clang diagnostic pop
#if TINT_BUILD_SPV_WRITER
if (fmt == "spirv") if (fmt == "spirv")
return Format::kSpirv; return Format::kSpirv;
if (fmt == "spvasm") if (fmt == "spvasm")
return Format::kSpvAsm; return Format::kSpvAsm;
#endif // TINT_BUILD_SPV_WRITER
#if TINT_BUILD_WGSL_WRITER
if (fmt == "wgsl") if (fmt == "wgsl")
return Format::kWgsl; return Format::kWgsl;
#endif // TINT_BUILD_WGSL_WRITER
return Format::kNone; return Format::kNone;
} }
@ -119,8 +137,12 @@ bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
/// writes error messages to the standard error stream and returns false. /// writes error messages to the standard error stream and returns false.
/// Assumes the size of a |T| object is divisible by its required alignment. /// Assumes the size of a |T| object is divisible by its required alignment.
/// @returns true if we successfully read the file. /// @returns true if we successfully read the file.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-template"
template <typename T> template <typename T>
bool ReadFile(const std::string& input_file, std::vector<T>* buffer) { bool ReadFile(const std::string& input_file, std::vector<T>* buffer) {
#pragma Clang pop
if (!buffer) { if (!buffer) {
std::cerr << "The buffer pointer was null" << std::endl; std::cerr << "The buffer pointer was null" << std::endl;
return false; return false;
@ -168,6 +190,7 @@ bool ReadFile(const std::string& input_file, std::vector<T>* buffer) {
return true; return true;
} }
#if TINT_BUILD_SPV_WRITER
std::string Disassemble(const std::vector<uint32_t>& data) { std::string Disassemble(const std::vector<uint32_t>& data) {
std::string spv_errors; std::string spv_errors;
spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0; spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0;
@ -204,6 +227,7 @@ std::string Disassemble(const std::vector<uint32_t>& data) {
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES); SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
return result; return result;
} }
#endif // TINT_BUILD_SPV_WRITER
} // namespace } // namespace
@ -227,6 +251,7 @@ int main(int argc, const char** argv) {
} }
std::unique_ptr<tint::reader::Reader> reader; std::unique_ptr<tint::reader::Reader> reader;
#if TINT_BUILD_WGSL_READER
if (options.input_filename.size() > 5 && if (options.input_filename.size() > 5 &&
options.input_filename.substr(options.input_filename.size() - 5) == options.input_filename.substr(options.input_filename.size() - 5) ==
".wgsl") { ".wgsl") {
@ -237,7 +262,9 @@ int main(int argc, const char** argv) {
reader = std::make_unique<tint::reader::wgsl::Parser>( reader = std::make_unique<tint::reader::wgsl::Parser>(
std::string(data.begin(), data.end())); std::string(data.begin(), data.end()));
} }
#if TINT_BUILD_SPV_PARSER #endif // TINT_BUILD_WGSL_READER
#if TINT_BUILD_SPV_READER
if (options.input_filename.size() > 4 && if (options.input_filename.size() > 4 &&
options.input_filename.substr(options.input_filename.size() - 4) == options.input_filename.substr(options.input_filename.size() - 4) ==
".spv") { ".spv") {
@ -247,7 +274,8 @@ int main(int argc, const char** argv) {
} }
reader = std::make_unique<tint::reader::spirv::Parser>(data); reader = std::make_unique<tint::reader::spirv::Parser>(data);
} }
#endif #endif // TINT_BUILD_SPV_READER
if (!reader) { if (!reader) {
std::cerr << "Failed to create reader for input file: " std::cerr << "Failed to create reader for input file: "
<< options.input_filename << std::endl; << options.input_filename << std::endl;
@ -279,12 +307,21 @@ int main(int argc, const char** argv) {
} }
std::unique_ptr<tint::writer::Writer> writer; std::unique_ptr<tint::writer::Writer> writer;
#if TINT_BUILD_SPV_WRITER
if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) { if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) {
writer = writer =
std::make_unique<tint::writer::spirv::Generator>(std::move(module)); std::make_unique<tint::writer::spirv::Generator>(std::move(module));
} else if (options.format == Format::kWgsl) { }
#endif // TINT_BUILD_SPV_WRITER
#if TINT_BUILD_WGSL_WRITER
if (options.format == Format::kWgsl) {
writer = std::make_unique<tint::writer::wgsl::Generator>(std::move(module)); writer = std::make_unique<tint::writer::wgsl::Generator>(std::move(module));
} else { }
#endif // TINT_BUILD_WGSL_WRITER
if (!writer) {
std::cerr << "Unknown output format specified" << std::endl; std::cerr << "Unknown output format specified" << std::endl;
return 1; return 1;
} }
@ -294,18 +331,25 @@ int main(int argc, const char** argv) {
return 1; return 1;
} }
#if TINT_BUILD_SPV_WRITER
if (options.format == Format::kSpvAsm) { if (options.format == Format::kSpvAsm) {
auto w = static_cast<tint::writer::spirv::Generator*>(writer.get()); auto w = static_cast<tint::writer::spirv::Generator*>(writer.get());
auto str = Disassemble(w->result()); auto str = Disassemble(w->result());
// TODO(dsinclair): Write to file if output_file given // TODO(dsinclair): Write to file if output_file given
std::cout << str << std::endl; std::cout << str << std::endl;
} else if (options.format == Format::kSpirv) { }
if (options.format == Format::kSpirv) {
// auto w = static_cast<tint::writer::spirv::Generator*>(writer.get()); // auto w = static_cast<tint::writer::spirv::Generator*>(writer.get());
// TODO(dsincliair): Write to to file // TODO(dsincliair): Write to to file
} else if (options.format == Format::kWgsl) { }
#endif // TINT_BUILD_SPV_WRITER
#if TINT_BUILD_WGSL_WRITER
if (options.format == Format::kWgsl) {
auto w = static_cast<tint::writer::wgsl::Generator*>(writer.get()); auto w = static_cast<tint::writer::wgsl::Generator*>(writer.get());
std::cout << w->result() << std::endl; std::cout << w->result() << std::endl;
} }
#endif // TINT_BUILD_WGSL_WRITER
return 0; return 0;
} }

View File

@ -12,6 +12,30 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
function(tint_spvtools_compile_options TARGET)
# We'll use the optimizer for its nice SPIR-V in-memory representation
target_link_libraries(${TARGET} SPIRV-Tools-opt SPIRV-Tools)
# We'll be cheating: using internal interfaces to the SPIRV-Tools
# optimizer.
target_include_directories(${TARGET} PRIVATE
${spirv-tools_SOURCE_DIR}
${spirv-tools_BINARY_DIR}
)
if (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
# The SPIRV-Tools code is conditioned against C++ and an older version of Clang.
# Suppress warnings triggered in our current compilation environment.
# TODO(dneto): Fix the issues upstream.
target_compile_options(${TARGET} PRIVATE
-Wno-newline-eof
-Wno-sign-conversion
-Wno-old-style-cast
-Wno-weak-vtables
)
endif()
endfunction()
set(TINT_LIB_SRCS set(TINT_LIB_SRCS
ast/array_accessor_expression.cc ast/array_accessor_expression.cc
ast/array_accessor_expression.h ast/array_accessor_expression.h
@ -159,14 +183,6 @@ set(TINT_LIB_SRCS
ast/variable_statement.h ast/variable_statement.h
reader/reader.cc reader/reader.cc
reader/reader.h reader/reader.h
reader/wgsl/lexer.cc
reader/wgsl/lexer.h
reader/wgsl/parser.cc
reader/wgsl/parser.h
reader/wgsl/parser_impl.cc
reader/wgsl/parser_impl.h
reader/wgsl/token.cc
reader/wgsl/token.h
source.h source.h
type_determiner.cc type_determiner.cc
type_determiner.h type_determiner.h
@ -176,7 +192,35 @@ set(TINT_LIB_SRCS
validator.h validator.h
validator_impl.cc validator_impl.cc
validator_impl.h validator_impl.h
# TODO(dsinclair): The writers should all be optional writer/writer.cc
writer/writer.h
)
if(${TINT_BUILD_SPV_READER})
list(APPEND TINT_LIB_SRCS
reader/spirv/fail_stream.h
reader/spirv/parser.cc
reader/spirv/parser.h
reader/spirv/parser_impl.cc
reader/spirv/parser_impl.h
)
endif()
if(${TINT_BUILD_WGSL_READER})
list(APPEND TINT_LIB_SRCS
reader/wgsl/lexer.cc
reader/wgsl/lexer.h
reader/wgsl/parser.cc
reader/wgsl/parser.h
reader/wgsl/parser_impl.cc
reader/wgsl/parser_impl.h
reader/wgsl/token.cc
reader/wgsl/token.h
)
endif()
if(${TINT_BUILD_SPV_WRITER})
list(APPEND TINT_LIB_SRCS
writer/spirv/binary_writer.cc writer/spirv/binary_writer.cc
writer/spirv/binary_writer.h writer/spirv/binary_writer.h
writer/spirv/builder.cc writer/spirv/builder.cc
@ -187,21 +231,15 @@ set(TINT_LIB_SRCS
writer/spirv/instruction.h writer/spirv/instruction.h
writer/spirv/operand.cc writer/spirv/operand.cc
writer/spirv/operand.h writer/spirv/operand.h
)
endif()
if(${TINT_BUILD_WGSL_WRITER})
list(APPEND TINT_LIB_SRCS
writer/wgsl/generator.cc writer/wgsl/generator.cc
writer/wgsl/generator.h writer/wgsl/generator.h
writer/wgsl/generator_impl.cc writer/wgsl/generator_impl.cc
writer/wgsl/generator_impl.h writer/wgsl/generator_impl.h
writer/writer.cc
writer/writer.h
)
if(TINT_BUILD_SPV_PARSER)
list(APPEND TINT_LIB_SRCS
reader/spirv/fail_stream.h
reader/spirv/parser.cc
reader/spirv/parser.h
reader/spirv/parser_impl.cc
reader/spirv/parser_impl.h
) )
endif() endif()
@ -259,6 +297,32 @@ set(TINT_TEST_SRCS
ast/unless_statement_test.cc ast/unless_statement_test.cc
ast/variable_statement_test.cc ast/variable_statement_test.cc
ast/variable_test.cc ast/variable_test.cc
type_manager_test.cc
validator_impl_import_test.cc
)
## Tint library
add_library(libtint ${TINT_LIB_SRCS})
tint_default_compile_options(libtint)
set_target_properties(libtint PROPERTIES OUTPUT_NAME "tint")
if(${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
tint_spvtools_compile_options(libtint)
endif()
if(${TINT_BUILD_SPV_READER})
list(APPEND TINT_TEST_SRCS
reader/spirv/fail_stream_test.cc
reader/spirv/parser_impl_import_test.cc
reader/spirv/parser_impl_test.cc
reader/spirv/parser_test.cc
reader/spirv/spirv_tools_helpers_test.cc
reader/spirv/spirv_tools_helpers_test.h
)
endif()
if(${TINT_BUILD_WGSL_READER})
list(APPEND TINT_TEST_SRCS
reader/wgsl/lexer_test.cc reader/wgsl/lexer_test.cc
reader/wgsl/parser_test.cc reader/wgsl/parser_test.cc
reader/wgsl/parser_impl_additive_expression_test.cc reader/wgsl/parser_impl_additive_expression_test.cc
@ -325,58 +389,25 @@ set(TINT_TEST_SRCS
reader/wgsl/parser_impl_variable_stmt_test.cc reader/wgsl/parser_impl_variable_stmt_test.cc
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 )
validator_impl_import_test.cc endif()
if(${TINT_BUILD_SPV_WRITER})
list(APPEND TINT_TEST_SRCS
writer/spirv/binary_writer_test.cc writer/spirv/binary_writer_test.cc
writer/spirv/builder_test.cc writer/spirv/builder_test.cc
writer/spirv/instruction_test.cc writer/spirv/instruction_test.cc
writer/spirv/operand_test.cc writer/spirv/operand_test.cc
)
endif()
if(${TINT_BUILD_WGSL_WRITER})
list(APPEND TINT_TEST_SRCS
writer/wgsl/generator_impl_test.cc writer/wgsl/generator_impl_test.cc
) )
function(tint_spvtools_compile_options TARGET)
# We'll use the optimizer for its nice SPIR-V in-memory representation
target_link_libraries(libtint SPIRV-Tools-opt SPIRV-Tools)
# We'll be cheating: using internal interfaces to the SPIRV-Tools
# optimizer.
target_include_directories(libtint PRIVATE ${spirv-tools_SOURCE_DIR} ${spirv-tools_BINARY_DIR})
if (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
# The SPIRV-Tools code is conditioned against C++ and an older version of Clang.
# Suppress warnings triggered in our current compilation environment.
# TODO(dneto): Fix the issues upstream.
target_compile_options(${TARGET} PRIVATE
-Wno-newline-eof
-Wno-sign-conversion
-Wno-old-style-cast
-Wno-weak-vtables
)
endif() endif()
endfunction()
## Tint library
add_library(libtint ${TINT_LIB_SRCS})
tint_default_compile_options(libtint)
set_target_properties(libtint PROPERTIES OUTPUT_NAME "tint")
if(${TINT_BUILD_SPV_PARSER})
tint_spvtools_compile_options(libtint)
endif()
if(${TINT_BUILD_SPV_PARSER})
list (APPEND TINT_TEST_SRCS
reader/spirv/fail_stream_test.cc
reader/spirv/parser_impl_import_test.cc
reader/spirv/parser_impl_test.cc
reader/spirv/parser_test.cc
reader/spirv/spirv_tools_helpers_test.cc
reader/spirv/spirv_tools_helpers_test.h
)
endif()
add_executable(tint_unittests ${TINT_TEST_SRCS}) add_executable(tint_unittests ${TINT_TEST_SRCS})
if (${TINT_BUILD_SPV_PARSER})
tint_spvtools_compile_options(tint_unittests)
endif()
if(NOT MSVC) if(NOT MSVC)
target_compile_options(tint_unittests PRIVATE target_compile_options(tint_unittests PRIVATE
-Wno-global-constructors -Wno-global-constructors
@ -387,11 +418,11 @@ endif()
## Test executable ## Test executable
target_include_directories( target_include_directories(
tint_unittests PRIVATE ${gmock_SOURCE_DIR}/include) tint_unittests PRIVATE ${gmock_SOURCE_DIR}/include)
if(${TINT_BUILD_SPV_PARSER})
target_include_directories(tint_unittests
PRIVATE ${spirv-tools_SOURCE_DIR} ${spirv-tools_BINARY_DIR})
endif()
target_link_libraries(tint_unittests libtint gmock_main) target_link_libraries(tint_unittests libtint gmock_main)
tint_default_compile_options(tint_unittests) tint_default_compile_options(tint_unittests)
if(${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
tint_spvtools_compile_options(tint_unittests)
endif()
add_test(NAME tint_unittests COMMAND tint_unittests) add_test(NAME tint_unittests COMMAND tint_unittests)

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "src/reader/spv/parser_impl.h" #include "src/reader/spirv/parser_impl.h"
#include <cstring> #include <cstring>

View File

@ -15,42 +15,40 @@
#include <iostream> #include <iostream>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/reader/wgsl/parser.h" #include "src/ast/import.h"
#include "src/ast/module.h"
#include "src/validator_impl.h" #include "src/validator_impl.h"
namespace tint { namespace tint {
namespace { namespace {
ast::Module build_module(std::string data) {
auto reader = std::make_unique<tint::reader::wgsl::Parser>(
std::string(data.begin(), data.end()));
EXPECT_TRUE(reader->Parse()) << reader->error();
return reader->module();
}
using ValidatorImplTest = testing::Test; using ValidatorImplTest = testing::Test;
TEST_F(ValidatorImplTest, Import) { TEST_F(ValidatorImplTest, Import) {
std::string input = "import \"GLSL.std.450\" as glsl;"; ast::Module m;
auto module = build_module(input); m.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "glsl"));
tint::ValidatorImpl v; tint::ValidatorImpl v;
EXPECT_TRUE(v.CheckImports(module)); EXPECT_TRUE(v.CheckImports(m));
} }
TEST_F(ValidatorImplTest, Import_Fail_NotGLSL) { TEST_F(ValidatorImplTest, Import_Fail_NotGLSL) {
std::string input = "import \"not.GLSL\" as glsl;"; ast::Module m;
auto module = build_module(input); m.AddImport(std::make_unique<ast::Import>(Source{1, 1}, "not.GLSL", "glsl"));
tint::ValidatorImpl v; tint::ValidatorImpl v;
EXPECT_FALSE(v.CheckImports(module)); EXPECT_FALSE(v.CheckImports(m));
ASSERT_TRUE(v.has_error()); ASSERT_TRUE(v.has_error());
EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: not.GLSL"); EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: not.GLSL");
} }
TEST_F(ValidatorImplTest, Import_Fail_Typo) { TEST_F(ValidatorImplTest, Import_Fail_Typo) {
std::string input = "import \"GLSL.std.4501\" as glsl;"; ast::Module m;
auto module = build_module(input); m.AddImport(
std::make_unique<ast::Import>(Source{1, 1}, "GLSL.std.4501", "glsl"));
tint::ValidatorImpl v; tint::ValidatorImpl v;
EXPECT_FALSE(v.CheckImports(module)); EXPECT_FALSE(v.CheckImports(m));
ASSERT_TRUE(v.has_error()); ASSERT_TRUE(v.has_error());
EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: GLSL.std.4501"); EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: GLSL.std.4501");
} }

View File

@ -14,8 +14,8 @@
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest EXCLUDE_FROM_ALL) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest EXCLUDE_FROM_ALL)
# SPIRV-Tools is only linked into tint if we're using the SPIR-V parser. We if(${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
# always build it regardless so we can use the validator for testing purposes.
set(SPIRV-Headers_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/spirv-headers CACHE STRING "") set(SPIRV-Headers_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/spirv-headers CACHE STRING "")
set(SPIRV_SKIP_TESTS ON CACHE BOOL ON) set(SPIRV_SKIP_TESTS ON CACHE BOOL ON)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/spirv-tools) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/spirv-tools)
endif()