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 "executable_semantics/ast/ast_test_matchers_internal.h"
  5. #include "llvm/ADT/StringExtras.h"
  6. #include "llvm/Support/raw_ostream.h"
  7. namespace Carbon {
  8. namespace TestingInternal {
  9. AstNodeMatcherBase::~AstNodeMatcherBase() = default;
  10. auto BlockContentsMatcher::MatchAndExplainImpl(
  11. Nonnull<const AstNode*> node, ::testing::MatchResultListener* out) const
  12. -> bool {
  13. const auto* block = llvm::dyn_cast<Block>(node);
  14. if (block == nullptr) {
  15. *out << "is not a Block";
  16. return false;
  17. }
  18. *out << "is a Block whose statements collection ";
  19. return matcher_.MatchAndExplain(block->statements(), out);
  20. }
  21. auto MatchesIntLiteralMatcher::MatchAndExplainImpl(
  22. const AstNode* node, ::testing::MatchResultListener* listener) const
  23. -> bool {
  24. const auto* literal = llvm::dyn_cast<IntLiteral>(node);
  25. if (literal == nullptr) {
  26. *listener << "is not an IntLiteral";
  27. return false;
  28. }
  29. bool matched = literal->value() == value_;
  30. *listener << "is " << (matched ? "" : "not ") << "a literal " << value_;
  31. return matched;
  32. }
  33. auto BinaryOperatorExpressionMatcher::MatchAndExplainImpl(
  34. Nonnull<const AstNode*> node, ::testing::MatchResultListener* out) const
  35. -> bool {
  36. const auto* op = llvm::dyn_cast<PrimitiveOperatorExpression>(node);
  37. if (op == nullptr) {
  38. *out << "which is not a PrimitiveOperatorExpression";
  39. return false;
  40. }
  41. if (op->arguments().size() != 2) {
  42. *out << "which does not have two operands";
  43. return false;
  44. }
  45. if (op->op() != op_) {
  46. *out << "whose operator is not " << ToString(op_);
  47. return false;
  48. }
  49. *out << "which is a " << ToString(op_) << " 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 " << ToString(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<Return>(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(decl->name(), listener)) {
  138. // We short-circuit here because if the name doesn't match, that's
  139. // probably the only information the user cares about.
  140. return false;
  141. }
  142. }
  143. bool matched = true;
  144. if (body_matcher_.has_value()) {
  145. out << sep;
  146. if (!decl->body().has_value()) {
  147. out << "that doesn't have a body";
  148. matched = false;
  149. } else {
  150. out << "whose body ";
  151. if (!body_matcher_->MatchAndExplain(**decl->body(), listener)) {
  152. matched = false;
  153. }
  154. }
  155. }
  156. return matched;
  157. }
  158. void MatchesFunctionDeclarationMatcher::DescribeToImpl(std::ostream* out,
  159. bool negated) const {
  160. llvm::raw_os_ostream raw_out(*out);
  161. raw_out << "is " << (negated ? "not " : "") << "a function declaration ";
  162. llvm::ListSeparator sep(", and");
  163. if (name_matcher_.has_value()) {
  164. raw_out << sep << "whose name ";
  165. name_matcher_->DescribeTo(out);
  166. }
  167. if (body_matcher_.has_value()) {
  168. raw_out << sep << "whose body ";
  169. body_matcher_->DescribeTo(out);
  170. }
  171. }
  172. auto MatchesUnimplementedExpressionMatcher::MatchAndExplainImpl(
  173. const AstNode* node, ::testing::MatchResultListener* listener) const
  174. -> bool {
  175. const auto* unimplemented = llvm::dyn_cast<UnimplementedExpression>(node);
  176. if (unimplemented == nullptr) {
  177. *listener << "is not an UnimplementedExpression";
  178. return false;
  179. }
  180. if (unimplemented->label() != label_) {
  181. *listener << "is not labeled " << label_;
  182. return false;
  183. }
  184. *listener << "is an unimplemented " << label_ << " node whose children ";
  185. return children_matcher_.MatchAndExplain(unimplemented->children(), listener);
  186. }
  187. void MatchesUnimplementedExpressionMatcher::DescribeToImpl(std::ostream* out,
  188. bool negated) const {
  189. *out << "is " << (negated ? "not " : "") << "an unimplemented " << label_
  190. << " node whose children ";
  191. children_matcher_.DescribeTo(out);
  192. }
  193. } // namespace TestingInternal
  194. } // namespace Carbon