1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
//===- 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
|