dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 1 | // 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 sinclair | f26d9a8 | 2020-03-26 15:31:36 +0000 | [diff] [blame] | 20 | #include <vector> |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 21 | |
| 22 | #include "gtest/gtest.h" |
David Neto | 753cdc7 | 2020-04-07 19:56:02 +0000 | [diff] [blame^] | 23 | #include "source/opt/ir_context.h" |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 24 | #include "src/context.h" |
| 25 | #include "src/reader/spirv/parser_impl.h" |
| 26 | |
| 27 | namespace tint { |
| 28 | namespace reader { |
| 29 | namespace spirv { |
| 30 | |
dan sinclair | f26d9a8 | 2020-03-26 15:31:36 +0000 | [diff] [blame] | 31 | /// SPIR-V Parser test class |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 32 | class SpvParserTest : public testing::Test { |
| 33 | public: |
| 34 | SpvParserTest() = default; |
| 35 | ~SpvParserTest() = default; |
| 36 | |
dan sinclair | f26d9a8 | 2020-03-26 15:31:36 +0000 | [diff] [blame] | 37 | /// Sets up the test helper |
David Neto | a8cd18e | 2020-03-27 00:47:16 +0000 | [diff] [blame] | 38 | void SetUp() { ctx_.Reset(); } |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 39 | |
dan sinclair | f26d9a8 | 2020-03-26 15:31:36 +0000 | [diff] [blame] | 40 | /// Tears down the test helper |
dan sinclair | 136fa11 | 2020-03-27 01:50:13 +0000 | [diff] [blame] | 41 | void TearDown() { impl_ = nullptr; } |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 42 | |
dan sinclair | f26d9a8 | 2020-03-26 15:31:36 +0000 | [diff] [blame] | 43 | /// Retrieves the parser from the helper |
David Neto | 2886749 | 2020-03-27 00:49:03 +0000 | [diff] [blame] | 44 | /// @param input the SPIR-V binary to parse |
| 45 | /// @returns a parser for the given binary |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 46 | ParserImpl* parser(const std::vector<uint32_t>& input) { |
David Neto | a8cd18e | 2020-03-27 00:47:16 +0000 | [diff] [blame] | 47 | impl_ = std::make_unique<ParserImpl>(&ctx_, input); |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 48 | return impl_.get(); |
| 49 | } |
| 50 | |
David Neto | 753cdc7 | 2020-04-07 19:56:02 +0000 | [diff] [blame^] | 51 | /// 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 sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 59 | private: |
| 60 | std::unique_ptr<ParserImpl> impl_; |
| 61 | Context ctx_; |
dan sinclair | 9981b63 | 2020-03-25 19:16:36 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | } // namespace spirv |
| 65 | } // namespace reader |
| 66 | } // namespace tint |
| 67 | |
dan sinclair | f26d9a8 | 2020-03-26 15:31:36 +0000 | [diff] [blame] | 68 | #endif // SRC_READER_SPIRV_PARSER_IMPL_TEST_HELPER_H_ |