ast_test_matchers_internal.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/ast/ast_test_matchers_internal.h"
  5. #include "llvm/ADT/StringExtras.h"
  6. #include "llvm/Support/raw_ostream.h"
  7. namespace Carbon::TestingInternal {
  8. AstNodeMatcherBase::~AstNodeMatcherBase() = default;
  9. auto BlockContentsMatcher::MatchAndExplainImpl(
  10. Nonnull<const AstNode*> node, ::testing::MatchResultListener* out) const
  11. -> bool {
  12. const auto* block = llvm::dyn_cast<Block>(node);
  13. if (block == nullptr) {
  14. *out << "is not a Block";
  15. return false;
  16. }
  17. *out << "is a Block whose statements collection ";
  18. return matcher_.MatchAndExplain(block->statements(), out);
  19. }
  20. auto MatchesIntLiteralMatcher::MatchAndExplainImpl(
  21. const AstNode* node, ::testing::MatchResultListener* listener) const
  22. -> bool {
  23. const auto* literal = llvm::dyn_cast<IntLiteral>(node);
  24. if (literal == nullptr) {
  25. *listener << "is not an IntLiteral";
  26. return false;
  27. }
  28. bool matched = literal->value() == value_;
  29. *listener << "is " << (matched ? "" : "not ") << "a literal " << value_;
  30. return matched;
  31. }
  32. auto BinaryOperatorExpressionMatcher::MatchAndExplainImpl(
  33. Nonnull<const AstNode*> node, ::testing::MatchResultListener* out) const
  34. -> bool {
  35. const auto* op = llvm::dyn_cast<OperatorExpression>(node);
  36. if (op == nullptr) {
  37. *out << "which is not a OperatorExpression";
  38. return false;
  39. }
  40. if (op->arguments().size() != 2) {
  41. *out << "which does not have two operands";
  42. return false;
  43. }
  44. if (op->op() != op_) {
  45. *out << "whose operator is not " << OperatorToString(op_);
  46. return false;
  47. }
  48. *out << "which is a " << OperatorToString(op_)
  49. << " expression whose left operand ";
  50. bool matched = lhs_.MatchAndExplain(*op->arguments()[0], out);
  51. *out << " and right operand ";
  52. if (!rhs_.MatchAndExplain(*op->arguments()[1], out)) {
  53. matched = false;
  54. }
  55. return matched;
  56. }
  57. void BinaryOperatorExpressionMatcher::DescribeToImpl(std::ostream* out,
  58. bool negated) const {
  59. *out << "is " << (negated ? "not " : "") << "a " << OperatorToString(op_)
  60. << " expression whose ";
  61. *out << "left operand ";
  62. lhs_.DescribeTo(out);
  63. *out << " and right operand ";
  64. rhs_.DescribeTo(out);
  65. }
  66. auto MatchesReturnMatcher::MatchAndExplainImpl(
  67. const AstNode* node, ::testing::MatchResultListener* listener) const
  68. -> bool {
  69. const auto* ret = llvm::dyn_cast<ReturnExpression>(node);
  70. if (ret == nullptr) {
  71. *listener << "which is not a return statement";
  72. return false;
  73. }
  74. *listener << "which is a return statement ";
  75. if (ret->is_omitted_expression()) {
  76. *listener << "with no operand";
  77. return !matcher_.has_value();
  78. } else if (matcher_.has_value()) {
  79. *listener << "whose operand ";
  80. return matcher_->MatchAndExplain(ret->expression(), listener);
  81. } else {
  82. *listener << "that has an operand";
  83. return false;
  84. }
  85. }
  86. void MatchesReturnMatcher::DescribeToImpl(std::ostream* out,
  87. bool negated) const {
  88. *out << "is " << (negated ? "not " : "") << "a return statement ";
  89. if (matcher_.has_value()) {
  90. *out << "whose operand ";
  91. matcher_->DescribeTo(out);
  92. } else {
  93. *out << "with no operand";
  94. }
  95. }
  96. namespace {
  97. // llvm::raw_ostream implementation backed by a MatchResultListener, so
  98. // we can use tools like llvm::ListSeparator.
  99. class RawListenerOstream : public llvm::raw_ostream {
  100. public:
  101. explicit RawListenerOstream(Nonnull<::testing::MatchResultListener*> listener)
  102. : out_(listener->stream()), fake_pos_(0) {}
  103. void write_impl(const char* ptr, size_t size) override {
  104. if (out_ == nullptr) {
  105. fake_pos_ += size;
  106. } else {
  107. out_->write(ptr, size);
  108. }
  109. }
  110. auto current_pos() const -> uint64_t override {
  111. if (out_ == nullptr) {
  112. return fake_pos_;
  113. } else {
  114. return out_->tellp();
  115. }
  116. }
  117. ~RawListenerOstream() override { flush(); }
  118. private:
  119. std::ostream* out_;
  120. // fake_pos_ tracks the notional output position when out_ is null.
  121. uint64_t fake_pos_;
  122. };
  123. } // namespace
  124. auto MatchesFunctionDeclarationMatcher::MatchAndExplainImpl(
  125. const AstNode* node, ::testing::MatchResultListener* listener) const
  126. -> bool {
  127. RawListenerOstream out(listener);
  128. const auto* decl = llvm::dyn_cast<FunctionDeclaration>(node);
  129. if (decl == nullptr) {
  130. out << "which is not a function declaration";
  131. return false;
  132. }
  133. out << "which is a function declaration ";
  134. llvm::ListSeparator sep(", and");
  135. if (name_matcher_.has_value()) {
  136. out << sep << "whose name ";
  137. if (!name_matcher_->MatchAndExplain(std::string(decl->name().inner_name()),
  138. listener)) {
  139. // We short-circuit here because if the name doesn't match, that's
  140. // probably the only information the user cares about.
  141. return false;
  142. }
  143. }
  144. bool matched = true;
  145. if (body_matcher_.has_value()) {
  146. out << sep;
  147. if (!decl->body().has_value()) {
  148. out << "that doesn't have a body";
  149. matched = false;
  150. } else {
  151. out << "whose body ";
  152. if (!body_matcher_->MatchAndExplain(**decl->body(), listener)) {
  153. matched = false;
  154. }
  155. }
  156. }
  157. return matched;
  158. }
  159. void MatchesFunctionDeclarationMatcher::DescribeToImpl(std::ostream* out,
  160. bool negated) const {
  161. llvm::raw_os_ostream raw_out(*out);
  162. raw_out << "is " << (negated ? "not " : "") << "a function declaration ";
  163. llvm::ListSeparator sep(", and");
  164. if (name_matcher_.has_value()) {
  165. raw_out << sep << "whose name ";
  166. name_matcher_->DescribeTo(out);
  167. }
  168. if (body_matcher_.has_value()) {
  169. raw_out << sep << "whose body ";
  170. body_matcher_->DescribeTo(out);
  171. }
  172. }
  173. auto MatchesUnimplementedExpressionMatcher::MatchAndExplainImpl(
  174. const AstNode* node, ::testing::MatchResultListener* listener) const
  175. -> bool {
  176. const auto* unimplemented = llvm::dyn_cast<UnimplementedExpression>(node);
  177. if (unimplemented == nullptr) {
  178. *listener << "is not an UnimplementedExpression";
  179. return false;
  180. }
  181. if (unimplemented->label() != label_) {
  182. *listener << "is not labeled " << label_;
  183. return false;
  184. }
  185. *listener << "is an unimplemented " << label_ << " node whose children ";
  186. return children_matcher_.MatchAndExplain(unimplemented->children(), listener);
  187. }
  188. void MatchesUnimplementedExpressionMatcher::DescribeToImpl(std::ostream* out,
  189. bool negated) const {
  190. *out << "is " << (negated ? "not " : "") << "an unimplemented " << label_
  191. << " node whose children ";
  192. children_matcher_.DescribeTo(out);
  193. }
  194. } // namespace Carbon::TestingInternal