ast_test_matchers_internal.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "executable_semantics/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<PrimitiveOperatorExpression>(node);
  36. if (op == nullptr) {
  37. *out << "which is not a PrimitiveOperatorExpression";
  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 " << ToString(op_);
  46. return false;
  47. }
  48. *out << "which is a " << ToString(op_) << " expression whose left operand ";
  49. bool matched = lhs_.MatchAndExplain(*op->arguments()[0], out);
  50. *out << " and right operand ";
  51. if (!rhs_.MatchAndExplain(*op->arguments()[1], out)) {
  52. matched = false;
  53. }
  54. return matched;
  55. }
  56. void BinaryOperatorExpressionMatcher::DescribeToImpl(std::ostream* out,
  57. bool negated) const {
  58. *out << "is " << (negated ? "not " : "") << "a " << ToString(op_)
  59. << " expression whose ";
  60. *out << "left operand ";
  61. lhs_.DescribeTo(out);
  62. *out << " and right operand ";
  63. rhs_.DescribeTo(out);
  64. }
  65. auto MatchesReturnMatcher::MatchAndExplainImpl(
  66. const AstNode* node, ::testing::MatchResultListener* listener) const
  67. -> bool {
  68. const auto* ret = llvm::dyn_cast<Return>(node);
  69. if (ret == nullptr) {
  70. *listener << "which is not a return statement";
  71. return false;
  72. }
  73. *listener << "which is a return statement ";
  74. if (ret->is_omitted_expression()) {
  75. *listener << "with no operand";
  76. return !matcher_.has_value();
  77. } else if (matcher_.has_value()) {
  78. *listener << "whose operand ";
  79. return matcher_->MatchAndExplain(ret->expression(), listener);
  80. } else {
  81. *listener << "that has an operand";
  82. return false;
  83. }
  84. }
  85. void MatchesReturnMatcher::DescribeToImpl(std::ostream* out,
  86. bool negated) const {
  87. *out << "is " << (negated ? "not " : "") << "a return statement ";
  88. if (matcher_.has_value()) {
  89. *out << "whose operand ";
  90. matcher_->DescribeTo(out);
  91. } else {
  92. *out << "with no operand";
  93. }
  94. }
  95. namespace {
  96. // llvm::raw_ostream implementation backed by a MatchResultListener, so
  97. // we can use tools like llvm::ListSeparator.
  98. class RawListenerOstream : public llvm::raw_ostream {
  99. public:
  100. explicit RawListenerOstream(Nonnull<::testing::MatchResultListener*> listener)
  101. : out_(listener->stream()), fake_pos_(0) {}
  102. void write_impl(const char* ptr, size_t size) override {
  103. if (out_ == nullptr) {
  104. fake_pos_ += size;
  105. } else {
  106. out_->write(ptr, size);
  107. }
  108. }
  109. auto current_pos() const -> uint64_t override {
  110. if (out_ == nullptr) {
  111. return fake_pos_;
  112. } else {
  113. return out_->tellp();
  114. }
  115. }
  116. ~RawListenerOstream() override { flush(); }
  117. private:
  118. std::ostream* out_;
  119. // fake_pos_ tracks the notional output position when out_ is null.
  120. uint64_t fake_pos_;
  121. };
  122. } // namespace
  123. auto MatchesFunctionDeclarationMatcher::MatchAndExplainImpl(
  124. const AstNode* node, ::testing::MatchResultListener* listener) const
  125. -> bool {
  126. RawListenerOstream out(listener);
  127. const auto* decl = llvm::dyn_cast<FunctionDeclaration>(node);
  128. if (decl == nullptr) {
  129. out << "which is not a function declaration";
  130. return false;
  131. }
  132. out << "which is a function declaration ";
  133. llvm::ListSeparator sep(", and");
  134. if (name_matcher_.has_value()) {
  135. out << sep << "whose name ";
  136. if (!name_matcher_->MatchAndExplain(decl->name(), listener)) {
  137. // We short-circuit here because if the name doesn't match, that's
  138. // probably the only information the user cares about.
  139. return false;
  140. }
  141. }
  142. bool matched = true;
  143. if (body_matcher_.has_value()) {
  144. out << sep;
  145. if (!decl->body().has_value()) {
  146. out << "that doesn't have a body";
  147. matched = false;
  148. } else {
  149. out << "whose body ";
  150. if (!body_matcher_->MatchAndExplain(**decl->body(), listener)) {
  151. matched = false;
  152. }
  153. }
  154. }
  155. return matched;
  156. }
  157. void MatchesFunctionDeclarationMatcher::DescribeToImpl(std::ostream* out,
  158. bool negated) const {
  159. llvm::raw_os_ostream raw_out(*out);
  160. raw_out << "is " << (negated ? "not " : "") << "a function declaration ";
  161. llvm::ListSeparator sep(", and");
  162. if (name_matcher_.has_value()) {
  163. raw_out << sep << "whose name ";
  164. name_matcher_->DescribeTo(out);
  165. }
  166. if (body_matcher_.has_value()) {
  167. raw_out << sep << "whose body ";
  168. body_matcher_->DescribeTo(out);
  169. }
  170. }
  171. auto MatchesUnimplementedExpressionMatcher::MatchAndExplainImpl(
  172. const AstNode* node, ::testing::MatchResultListener* listener) const
  173. -> bool {
  174. const auto* unimplemented = llvm::dyn_cast<UnimplementedExpression>(node);
  175. if (unimplemented == nullptr) {
  176. *listener << "is not an UnimplementedExpression";
  177. return false;
  178. }
  179. if (unimplemented->label() != label_) {
  180. *listener << "is not labeled " << label_;
  181. return false;
  182. }
  183. *listener << "is an unimplemented " << label_ << " node whose children ";
  184. return children_matcher_.MatchAndExplain(unimplemented->children(), listener);
  185. }
  186. void MatchesUnimplementedExpressionMatcher::DescribeToImpl(std::ostream* out,
  187. bool negated) const {
  188. *out << "is " << (negated ? "not " : "") << "an unimplemented " << label_
  189. << " node whose children ";
  190. children_matcher_.DescribeTo(out);
  191. }
  192. } // namespace Carbon::TestingInternal