pass_utils.h 3.6 KB

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