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