123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- //===- 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
- //
- //===----------------------------------------------------------------------===//
- //
- // This file implements several matchers for popular gtest macros. 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.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/ASTMatchers/GtestMatchers.h"
- #include "clang/AST/ASTConsumer.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/RecursiveASTVisitor.h"
- #include "clang/ASTMatchers/ASTMatchFinder.h"
- #include "llvm/ADT/DenseMap.h"
- #include "llvm/ADT/StringMap.h"
- #include "llvm/ADT/StringRef.h"
- namespace clang {
- namespace ast_matchers {
- namespace {
- enum class MacroType {
- Expect,
- Assert,
- On,
- };
- } // namespace
- 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 getMacroTypeName(MacroType Macro) {
- switch (Macro) {
- case MacroType::Expect:
- return "EXPECT";
- case MacroType::Assert:
- return "ASSERT";
- case MacroType::On:
- return "ON";
- }
- llvm_unreachable("Unhandled MacroType enum");
- }
- static llvm::StringRef getComparisonTypeName(GtestCmp Cmp) {
- switch (Cmp) {
- case GtestCmp::Eq:
- return "EQ";
- case GtestCmp::Ne:
- return "NE";
- case GtestCmp::Ge:
- return "GE";
- case GtestCmp::Gt:
- return "GT";
- case GtestCmp::Le:
- return "LE";
- case GtestCmp::Lt:
- return "LT";
- }
- llvm_unreachable("Unhandled GtestCmp enum");
- }
- static std::string getMacroName(MacroType Macro, GtestCmp Cmp) {
- return (getMacroTypeName(Macro) + "_" + getComparisonTypeName(Cmp)).str();
- }
- static std::string getMacroName(MacroType Macro, llvm::StringRef Operation) {
- return (getMacroTypeName(Macro) + "_" + Operation).str();
- }
- // Under the hood, ON_CALL is expanded to a call to `InternalDefaultActionSetAt`
- // to set a default action spec to the underlying function mocker, while
- // EXPECT_CALL is expanded to a call to `InternalExpectedAt` to set a new
- // expectation spec.
- static llvm::StringRef getSpecSetterName(MacroType Macro) {
- switch (Macro) {
- case MacroType::On:
- return "InternalDefaultActionSetAt";
- case MacroType::Expect:
- return "InternalExpectedAt";
- default:
- llvm_unreachable("Unhandled MacroType enum");
- }
- llvm_unreachable("Unhandled MacroType 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.
- static internal::BindableMatcher<Stmt>
- gtestComparisonInternal(MacroType Macro, GtestCmp Cmp, StatementMatcher Left,
- StatementMatcher Right) {
- return callExpr(isExpandedFromMacro(getMacroName(Macro, Cmp)),
- callee(getComparisonDecl(Cmp)), hasArgument(2, Left),
- hasArgument(3, Right));
- }
- static internal::BindableMatcher<Stmt>
- gtestThatInternal(MacroType Macro, StatementMatcher Actual,
- StatementMatcher Matcher) {
- return cxxOperatorCallExpr(
- isExpandedFromMacro(getMacroName(Macro, "THAT")),
- hasOverloadedOperatorName("()"), hasArgument(2, Actual),
- hasArgument(
- 0, expr(hasType(classTemplateSpecializationDecl(hasName(
- "::testing::internal::PredicateFormatterFromMatcher"))),
- ignoringImplicit(
- callExpr(callee(functionDecl(hasName(
- "::testing::internal::"
- "MakePredicateFormatterFromMatcher"))),
- hasArgument(0, ignoringImplicit(Matcher)))))));
- }
- static internal::BindableMatcher<Stmt>
- gtestCallInternal(MacroType Macro, StatementMatcher MockCall, MockArgs Args) {
- // A ON_CALL or EXPECT_CALL macro expands to different AST structures
- // depending on whether the mock method has arguments or not.
- switch (Args) {
- // For example,
- // `ON_CALL(mock, TwoParamMethod)` is expanded to
- // `mock.gmock_TwoArgsMethod(WithoutMatchers(),
- // nullptr).InternalDefaultActionSetAt(...)`.
- // EXPECT_CALL is the same except
- // that it calls `InternalExpectedAt` instead of `InternalDefaultActionSetAt`
- // in the end.
- case MockArgs::None:
- return cxxMemberCallExpr(
- isExpandedFromMacro(getMacroName(Macro, "CALL")),
- callee(functionDecl(hasName(getSpecSetterName(Macro)))),
- onImplicitObjectArgument(ignoringImplicit(MockCall)));
- // For example,
- // `ON_CALL(mock, TwoParamMethod(m1, m2))` is expanded to
- // `mock.gmock_TwoParamMethod(m1,m2)(WithoutMatchers(),
- // nullptr).InternalDefaultActionSetAt(...)`.
- // EXPECT_CALL is the same except that it calls `InternalExpectedAt` instead
- // of `InternalDefaultActionSetAt` in the end.
- case MockArgs::Some:
- return cxxMemberCallExpr(
- isExpandedFromMacro(getMacroName(Macro, "CALL")),
- callee(functionDecl(hasName(getSpecSetterName(Macro)))),
- onImplicitObjectArgument(ignoringImplicit(cxxOperatorCallExpr(
- hasOverloadedOperatorName("()"), argumentCountIs(3),
- hasArgument(0, ignoringImplicit(MockCall))))));
- }
- llvm_unreachable("Unhandled MockArgs enum");
- }
- static internal::BindableMatcher<Stmt>
- gtestCallInternal(MacroType Macro, StatementMatcher MockObject,
- llvm::StringRef MockMethodName, MockArgs Args) {
- return gtestCallInternal(
- Macro,
- cxxMemberCallExpr(
- onImplicitObjectArgument(MockObject),
- callee(functionDecl(hasName(("gmock_" + MockMethodName).str())))),
- Args);
- }
- internal::BindableMatcher<Stmt> gtestAssert(GtestCmp Cmp, StatementMatcher Left,
- StatementMatcher Right) {
- return gtestComparisonInternal(MacroType::Assert, Cmp, Left, Right);
- }
- internal::BindableMatcher<Stmt> gtestExpect(GtestCmp Cmp, StatementMatcher Left,
- StatementMatcher Right) {
- return gtestComparisonInternal(MacroType::Expect, Cmp, Left, Right);
- }
- internal::BindableMatcher<Stmt> gtestAssertThat(StatementMatcher Actual,
- StatementMatcher Matcher) {
- return gtestThatInternal(MacroType::Assert, Actual, Matcher);
- }
- internal::BindableMatcher<Stmt> gtestExpectThat(StatementMatcher Actual,
- StatementMatcher Matcher) {
- return gtestThatInternal(MacroType::Expect, Actual, Matcher);
- }
- internal::BindableMatcher<Stmt> gtestOnCall(StatementMatcher MockObject,
- llvm::StringRef MockMethodName,
- MockArgs Args) {
- return gtestCallInternal(MacroType::On, MockObject, MockMethodName, Args);
- }
- internal::BindableMatcher<Stmt> gtestOnCall(StatementMatcher MockCall,
- MockArgs Args) {
- return gtestCallInternal(MacroType::On, MockCall, Args);
- }
- internal::BindableMatcher<Stmt> gtestExpectCall(StatementMatcher MockObject,
- llvm::StringRef MockMethodName,
- MockArgs Args) {
- return gtestCallInternal(MacroType::Expect, MockObject, MockMethodName, Args);
- }
- internal::BindableMatcher<Stmt> gtestExpectCall(StatementMatcher MockCall,
- MockArgs Args) {
- return gtestCallInternal(MacroType::Expect, MockCall, Args);
- }
- } // end namespace ast_matchers
- } // end namespace clang
|