head	1.1;
branch	1.1.1;
access;
symbols
	netbsd-11-0-RC7:1.1.1.1
	netbsd-11-0-RC6:1.1.1.1
	netbsd-11-0-RC5:1.1.1.1
	netbsd-11-0-RC4:1.1.1.1
	netbsd-11-0-RC3:1.1.1.1
	netbsd-11-0-RC2:1.1.1.1
	netbsd-11-0-RC1:1.1.1.1
	perseant-exfatfs-base-20250801:1.1.1.1
	netbsd-11:1.1.1.1.0.10
	netbsd-11-base:1.1.1.1
	netbsd-10-1-RELEASE:1.1.1.1
	perseant-exfatfs-base-20240630:1.1.1.1
	perseant-exfatfs:1.1.1.1.0.8
	perseant-exfatfs-base:1.1.1.1
	netbsd-10-0-RELEASE:1.1.1.1
	netbsd-10-0-RC6:1.1.1.1
	netbsd-10-0-RC5:1.1.1.1
	netbsd-10-0-RC4:1.1.1.1
	netbsd-10-0-RC3:1.1.1.1
	netbsd-10-0-RC2:1.1.1.1
	netbsd-10-0-RC1:1.1.1.1
	netbsd-10:1.1.1.1.0.6
	netbsd-10-base:1.1.1.1
	cjep_sun2x-base1:1.1.1.1
	cjep_sun2x:1.1.1.1.0.4
	cjep_sun2x-base:1.1.1.1
	cjep_staticlib_x:1.1.1.1.0.2
	cjep_staticlib_x-base1:1.1.1.1
	LLVM-249b40b558955afe5ac2b549edcf2d7f859c8cc9:1.1.1.1
	LLVM:1.1.1;
locks; strict;
comment	@// @;


1.1
date	2021.05.30.01.25.32;	author joerg;	state Exp;
branches
	1.1.1.1;
next	;
commitid	uhgdinROdC6tU6VC;

1.1.1.1
date	2021.05.30.01.25.32;	author joerg;	state Exp;
branches
	1.1.1.1.2.1;
next	;
commitid	uhgdinROdC6tU6VC;

1.1.1.1.2.1
date	2021.05.30.01.25.32;	author cjep;	state dead;
branches;
next	1.1.1.1.2.2;
commitid	eWz9SBW0XqKjJlVC;

1.1.1.1.2.2
date	2021.05.31.22.07.11;	author cjep;	state Exp;
branches;
next	;
commitid	eWz9SBW0XqKjJlVC;


desc
@@


1.1
log
@Initial revision
@
text
@//===- GtestMatchers.cpp - AST Matchers for Gtest ---------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/ASTMatchers/GtestMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Timer.h"
#include <deque>
#include <memory>
#include <set>

namespace clang {
namespace ast_matchers {

static DeclarationMatcher getComparisonDecl(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return cxxMethodDecl(hasName("Compare"),
                           ofClass(cxxRecordDecl(isSameOrDerivedFrom(
                               hasName("::testing::internal::EqHelper")))));
    case GtestCmp::Ne:
      return functionDecl(hasName("::testing::internal::CmpHelperNE"));
    case GtestCmp::Ge:
      return functionDecl(hasName("::testing::internal::CmpHelperGE"));
    case GtestCmp::Gt:
      return functionDecl(hasName("::testing::internal::CmpHelperGT"));
    case GtestCmp::Le:
      return functionDecl(hasName("::testing::internal::CmpHelperLE"));
    case GtestCmp::Lt:
      return functionDecl(hasName("::testing::internal::CmpHelperLT"));
  }
  llvm_unreachable("Unhandled GtestCmp enum");
}

static llvm::StringRef getAssertMacro(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return "ASSERT_EQ";
    case GtestCmp::Ne:
      return "ASSERT_NE";
    case GtestCmp::Ge:
      return "ASSERT_GE";
    case GtestCmp::Gt:
      return "ASSERT_GT";
    case GtestCmp::Le:
      return "ASSERT_LE";
    case GtestCmp::Lt:
      return "ASSERT_LT";
  }
  llvm_unreachable("Unhandled GtestCmp enum");
}

static llvm::StringRef getExpectMacro(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return "EXPECT_EQ";
    case GtestCmp::Ne:
      return "EXPECT_NE";
    case GtestCmp::Ge:
      return "EXPECT_GE";
    case GtestCmp::Gt:
      return "EXPECT_GT";
    case GtestCmp::Le:
      return "EXPECT_LE";
    case GtestCmp::Lt:
      return "EXPECT_LT";
  }
  llvm_unreachable("Unhandled GtestCmp enum");
}

// In general, AST matchers cannot match calls to macros. However, we can
// simulate such matches if the macro definition has identifiable elements that
// themselves can be matched. In that case, we can match on those elements and
// then check that the match occurs within an expansion of the desired
// macro. The more uncommon the identified elements, the more efficient this
// process will be.
//
// We use this approach to implement the derived matchers gtestAssert and
// gtestExpect.
internal::BindableMatcher<Stmt> gtestAssert(GtestCmp Cmp, StatementMatcher Left,
                                            StatementMatcher Right) {
  return callExpr(callee(getComparisonDecl(Cmp)),
                  isExpandedFromMacro(getAssertMacro(Cmp).str()),
                  hasArgument(2, Left), hasArgument(3, Right));
}

internal::BindableMatcher<Stmt> gtestExpect(GtestCmp Cmp, StatementMatcher Left,
                                            StatementMatcher Right) {
  return callExpr(callee(getComparisonDecl(Cmp)),
                  isExpandedFromMacro(getExpectMacro(Cmp).str()),
                  hasArgument(2, Left), hasArgument(3, Right));
}

} // end namespace ast_matchers
} // end namespace clang
@


1.1.1.1
log
@Import clang 249b40b558955afe5ac2b549edcf2d7f859c8cc9.
@
text
@@


1.1.1.1.2.1
log
@file GtestMatchers.cpp was added on branch cjep_staticlib_x on 2021-05-31 22:07:11 +0000
@
text
@d1 104
@


1.1.1.1.2.2
log
@sync with head
@
text
@a0 104
//===- GtestMatchers.cpp - AST Matchers for Gtest ---------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/ASTMatchers/GtestMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Timer.h"
#include <deque>
#include <memory>
#include <set>

namespace clang {
namespace ast_matchers {

static DeclarationMatcher getComparisonDecl(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return cxxMethodDecl(hasName("Compare"),
                           ofClass(cxxRecordDecl(isSameOrDerivedFrom(
                               hasName("::testing::internal::EqHelper")))));
    case GtestCmp::Ne:
      return functionDecl(hasName("::testing::internal::CmpHelperNE"));
    case GtestCmp::Ge:
      return functionDecl(hasName("::testing::internal::CmpHelperGE"));
    case GtestCmp::Gt:
      return functionDecl(hasName("::testing::internal::CmpHelperGT"));
    case GtestCmp::Le:
      return functionDecl(hasName("::testing::internal::CmpHelperLE"));
    case GtestCmp::Lt:
      return functionDecl(hasName("::testing::internal::CmpHelperLT"));
  }
  llvm_unreachable("Unhandled GtestCmp enum");
}

static llvm::StringRef getAssertMacro(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return "ASSERT_EQ";
    case GtestCmp::Ne:
      return "ASSERT_NE";
    case GtestCmp::Ge:
      return "ASSERT_GE";
    case GtestCmp::Gt:
      return "ASSERT_GT";
    case GtestCmp::Le:
      return "ASSERT_LE";
    case GtestCmp::Lt:
      return "ASSERT_LT";
  }
  llvm_unreachable("Unhandled GtestCmp enum");
}

static llvm::StringRef getExpectMacro(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return "EXPECT_EQ";
    case GtestCmp::Ne:
      return "EXPECT_NE";
    case GtestCmp::Ge:
      return "EXPECT_GE";
    case GtestCmp::Gt:
      return "EXPECT_GT";
    case GtestCmp::Le:
      return "EXPECT_LE";
    case GtestCmp::Lt:
      return "EXPECT_LT";
  }
  llvm_unreachable("Unhandled GtestCmp enum");
}

// In general, AST matchers cannot match calls to macros. However, we can
// simulate such matches if the macro definition has identifiable elements that
// themselves can be matched. In that case, we can match on those elements and
// then check that the match occurs within an expansion of the desired
// macro. The more uncommon the identified elements, the more efficient this
// process will be.
//
// We use this approach to implement the derived matchers gtestAssert and
// gtestExpect.
internal::BindableMatcher<Stmt> gtestAssert(GtestCmp Cmp, StatementMatcher Left,
                                            StatementMatcher Right) {
  return callExpr(callee(getComparisonDecl(Cmp)),
                  isExpandedFromMacro(getAssertMacro(Cmp).str()),
                  hasArgument(2, Left), hasArgument(3, Right));
}

internal::BindableMatcher<Stmt> gtestExpect(GtestCmp Cmp, StatementMatcher Left,
                                            StatementMatcher Right) {
  return callExpr(callee(getComparisonDecl(Cmp)),
                  isExpandedFromMacro(getExpectMacro(Cmp).str()),
                  hasArgument(2, Left), hasArgument(3, Right));
}

} // end namespace ast_matchers
} // end namespace clang
@


