1// Copyright (c) 2016 Google Inc. 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 TEST_OPT_PASS_UTILS_H_ 16#define TEST_OPT_PASS_UTILS_H_ 17 18#include <algorithm> 19#include <functional> 20#include <iterator> 21#include <string> 22#include <vector> 23 24#include "gtest/gtest.h" 25#include "include/spirv-tools/libspirv.h" 26#include "include/spirv-tools/libspirv.hpp" 27 28namespace spvtools { 29namespace opt { 30 31struct Message { 32 spv_message_level_t level; 33 const char* source_file; 34 uint32_t line_number; 35 uint32_t column_number; 36 const char* message; 37}; 38 39// Return a message consumer that can be used to check that the message produced 40// are the messages in |expexted_messages|, and in the same order. 41MessageConsumer GetTestMessageConsumer(std::vector<Message>& expected_messages); 42 43// In-place substring replacement. Finds the |find_str| in the |process_str| 44// and replaces the found substring with |replace_str|. Returns true if at 45// least one replacement is done successfully, returns false otherwise. The 46// replaced substring won't be processed again, which means: If the 47// |replace_str| has |find_str| as its substring, that newly replaced part of 48// |process_str| won't be processed again. 49bool FindAndReplace(std::string* process_str, const std::string find_str, 50 const std::string replace_str); 51 52// Returns true if the given string contains any debug opcode substring. 53bool ContainsDebugOpcode(const char* inst); 54 55// Returns the concatenated string from a vector of |strings|, with postfixing 56// each string with the given |delimiter|. if the |skip_dictator| returns true 57// for an original string, that string will be omitted. 58std::string SelectiveJoin(const std::vector<const char*>& strings, 59 const std::function<bool(const char*)>& skip_dictator, 60 char delimiter = '\n'); 61 62// Concatenates a vector of strings into one string. Each string is postfixed 63// with '\n'. 64std::string JoinAllInsts(const std::vector<const char*>& insts); 65 66// Concatenates a vector of strings into one string. Each string is postfixed 67// with '\n'. If a string contains opcode for debug instruction, that string 68// will be ignored. 69std::string JoinNonDebugInsts(const std::vector<const char*>& insts); 70 71// Returns a vector that contains the contents of |a| followed by the contents 72// of |b|. 73template <typename T> 74std::vector<T> Concat(const std::vector<T>& a, const std::vector<T>& b) { 75 std::vector<T> ret; 76 std::copy(a.begin(), a.end(), back_inserter(ret)); 77 std::copy(b.begin(), b.end(), back_inserter(ret)); 78 return ret; 79} 80 81} // namespace opt 82} // namespace spvtools 83 84#endif // TEST_OPT_PASS_UTILS_H_ 85