blob: 4f52e0985f9b958e9514e5188c173180a6eae305 [file] [log] [blame]
dan sinclair9981b632020-03-25 19:16:36 +00001// Copyright 2020 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_READER_SPIRV_PARSER_IMPL_TEST_HELPER_H_
16#define SRC_READER_SPIRV_PARSER_IMPL_TEST_HELPER_H_
17
18#include <memory>
19#include <string>
dan sinclairf26d9a82020-03-26 15:31:36 +000020#include <vector>
dan sinclair9981b632020-03-25 19:16:36 +000021
22#include "gtest/gtest.h"
David Neto753cdc72020-04-07 19:56:02 +000023#include "source/opt/ir_context.h"
dan sinclair9981b632020-03-25 19:16:36 +000024#include "src/context.h"
25#include "src/reader/spirv/parser_impl.h"
26
27namespace tint {
28namespace reader {
29namespace spirv {
30
dan sinclairf26d9a82020-03-26 15:31:36 +000031/// SPIR-V Parser test class
dan sinclair9981b632020-03-25 19:16:36 +000032class SpvParserTest : public testing::Test {
33 public:
34 SpvParserTest() = default;
35 ~SpvParserTest() = default;
36
dan sinclairf26d9a82020-03-26 15:31:36 +000037 /// Sets up the test helper
David Netoa8cd18e2020-03-27 00:47:16 +000038 void SetUp() { ctx_.Reset(); }
dan sinclair9981b632020-03-25 19:16:36 +000039
dan sinclairf26d9a82020-03-26 15:31:36 +000040 /// Tears down the test helper
dan sinclair136fa112020-03-27 01:50:13 +000041 void TearDown() { impl_ = nullptr; }
dan sinclair9981b632020-03-25 19:16:36 +000042
dan sinclairf26d9a82020-03-26 15:31:36 +000043 /// Retrieves the parser from the helper
David Neto28867492020-03-27 00:49:03 +000044 /// @param input the SPIR-V binary to parse
45 /// @returns a parser for the given binary
dan sinclair9981b632020-03-25 19:16:36 +000046 ParserImpl* parser(const std::vector<uint32_t>& input) {
David Netoa8cd18e2020-03-27 00:47:16 +000047 impl_ = std::make_unique<ParserImpl>(&ctx_, input);
dan sinclair9981b632020-03-25 19:16:36 +000048 return impl_.get();
49 }
50
David Neto753cdc72020-04-07 19:56:02 +000051 /// Gets the internal representation of the function with the given ID.
52 /// Assumes ParserImpl::BuildInternalRepresentation has been run and
53 /// succeeded.
54 /// @returns the internal representation of the function
55 spvtools::opt::Function* spirv_function(uint32_t id) {
56 return impl_->ir_context()->GetFunction(id);
57 }
58
dan sinclair9981b632020-03-25 19:16:36 +000059 private:
60 std::unique_ptr<ParserImpl> impl_;
61 Context ctx_;
dan sinclair9981b632020-03-25 19:16:36 +000062};
63
64} // namespace spirv
65} // namespace reader
66} // namespace tint
67
dan sinclairf26d9a82020-03-26 15:31:36 +000068#endif // SRC_READER_SPIRV_PARSER_IMPL_TEST_HELPER_H_