1 //===- GtestMatchers.cpp - AST Matchers for Gtest ---------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements several matchers for popular gtest macros. In general,
10 // AST matchers cannot match calls to macros. However, we can simulate such
11 // matches if the macro definition has identifiable elements that themselves can
12 // be matched. In that case, we can match on those elements and then check that
13 // the match occurs within an expansion of the desired macro. The more uncommon
14 // the identified elements, the more efficient this process will be.
16 //===----------------------------------------------------------------------===//
18 #include "clang/ASTMatchers/GtestMatchers.h"
19 #include "clang/AST/ASTConsumer.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/RecursiveASTVisitor.h"
22 #include "clang/ASTMatchers/ASTMatchFinder.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/ADT/StringRef.h"
28 namespace ast_matchers
{
31 enum class MacroType
{
39 static DeclarationMatcher
getComparisonDecl(GtestCmp Cmp
) {
42 return cxxMethodDecl(hasName("Compare"),
43 ofClass(cxxRecordDecl(isSameOrDerivedFrom(
44 hasName("::testing::internal::EqHelper")))));
46 return functionDecl(hasName("::testing::internal::CmpHelperNE"));
48 return functionDecl(hasName("::testing::internal::CmpHelperGE"));
50 return functionDecl(hasName("::testing::internal::CmpHelperGT"));
52 return functionDecl(hasName("::testing::internal::CmpHelperLE"));
54 return functionDecl(hasName("::testing::internal::CmpHelperLT"));
56 llvm_unreachable("Unhandled GtestCmp enum");
59 static llvm::StringRef
getMacroTypeName(MacroType Macro
) {
61 case MacroType::Expect
:
63 case MacroType::Assert
:
68 llvm_unreachable("Unhandled MacroType enum");
71 static llvm::StringRef
getComparisonTypeName(GtestCmp Cmp
) {
86 llvm_unreachable("Unhandled GtestCmp enum");
89 static std::string
getMacroName(MacroType Macro
, GtestCmp Cmp
) {
90 return (getMacroTypeName(Macro
) + "_" + getComparisonTypeName(Cmp
)).str();
93 static std::string
getMacroName(MacroType Macro
, llvm::StringRef Operation
) {
94 return (getMacroTypeName(Macro
) + "_" + Operation
).str();
97 // Under the hood, ON_CALL is expanded to a call to `InternalDefaultActionSetAt`
98 // to set a default action spec to the underlying function mocker, while
99 // EXPECT_CALL is expanded to a call to `InternalExpectedAt` to set a new
101 static llvm::StringRef
getSpecSetterName(MacroType Macro
) {
104 return "InternalDefaultActionSetAt";
105 case MacroType::Expect
:
106 return "InternalExpectedAt";
108 llvm_unreachable("Unhandled MacroType enum");
110 llvm_unreachable("Unhandled MacroType enum");
113 // In general, AST matchers cannot match calls to macros. However, we can
114 // simulate such matches if the macro definition has identifiable elements that
115 // themselves can be matched. In that case, we can match on those elements and
116 // then check that the match occurs within an expansion of the desired
117 // macro. The more uncommon the identified elements, the more efficient this
120 // We use this approach to implement the derived matchers gtestAssert and
122 static internal::BindableMatcher
<Stmt
>
123 gtestComparisonInternal(MacroType Macro
, GtestCmp Cmp
, StatementMatcher Left
,
124 StatementMatcher Right
) {
125 return callExpr(isExpandedFromMacro(getMacroName(Macro
, Cmp
)),
126 callee(getComparisonDecl(Cmp
)), hasArgument(2, Left
),
127 hasArgument(3, Right
));
130 static internal::BindableMatcher
<Stmt
>
131 gtestThatInternal(MacroType Macro
, StatementMatcher Actual
,
132 StatementMatcher Matcher
) {
133 return cxxOperatorCallExpr(
134 isExpandedFromMacro(getMacroName(Macro
, "THAT")),
135 hasOverloadedOperatorName("()"), hasArgument(2, Actual
),
137 0, expr(hasType(classTemplateSpecializationDecl(hasName(
138 "::testing::internal::PredicateFormatterFromMatcher"))),
140 callExpr(callee(functionDecl(hasName(
141 "::testing::internal::"
142 "MakePredicateFormatterFromMatcher"))),
143 hasArgument(0, ignoringImplicit(Matcher
)))))));
146 static internal::BindableMatcher
<Stmt
>
147 gtestCallInternal(MacroType Macro
, StatementMatcher MockCall
, MockArgs Args
) {
148 // A ON_CALL or EXPECT_CALL macro expands to different AST structures
149 // depending on whether the mock method has arguments or not.
152 // `ON_CALL(mock, TwoParamMethod)` is expanded to
153 // `mock.gmock_TwoArgsMethod(WithoutMatchers(),
154 // nullptr).InternalDefaultActionSetAt(...)`.
155 // EXPECT_CALL is the same except
156 // that it calls `InternalExpectedAt` instead of `InternalDefaultActionSetAt`
159 return cxxMemberCallExpr(
160 isExpandedFromMacro(getMacroName(Macro
, "CALL")),
161 callee(functionDecl(hasName(getSpecSetterName(Macro
)))),
162 onImplicitObjectArgument(ignoringImplicit(MockCall
)));
164 // `ON_CALL(mock, TwoParamMethod(m1, m2))` is expanded to
165 // `mock.gmock_TwoParamMethod(m1,m2)(WithoutMatchers(),
166 // nullptr).InternalDefaultActionSetAt(...)`.
167 // EXPECT_CALL is the same except that it calls `InternalExpectedAt` instead
168 // of `InternalDefaultActionSetAt` in the end.
170 return cxxMemberCallExpr(
171 isExpandedFromMacro(getMacroName(Macro
, "CALL")),
172 callee(functionDecl(hasName(getSpecSetterName(Macro
)))),
173 onImplicitObjectArgument(ignoringImplicit(cxxOperatorCallExpr(
174 hasOverloadedOperatorName("()"), argumentCountIs(3),
175 hasArgument(0, ignoringImplicit(MockCall
))))));
177 llvm_unreachable("Unhandled MockArgs enum");
180 static internal::BindableMatcher
<Stmt
>
181 gtestCallInternal(MacroType Macro
, StatementMatcher MockObject
,
182 llvm::StringRef MockMethodName
, MockArgs Args
) {
183 return gtestCallInternal(
186 onImplicitObjectArgument(MockObject
),
187 callee(functionDecl(hasName(("gmock_" + MockMethodName
).str())))),
191 internal::BindableMatcher
<Stmt
> gtestAssert(GtestCmp Cmp
, StatementMatcher Left
,
192 StatementMatcher Right
) {
193 return gtestComparisonInternal(MacroType::Assert
, Cmp
, Left
, Right
);
196 internal::BindableMatcher
<Stmt
> gtestExpect(GtestCmp Cmp
, StatementMatcher Left
,
197 StatementMatcher Right
) {
198 return gtestComparisonInternal(MacroType::Expect
, Cmp
, Left
, Right
);
201 internal::BindableMatcher
<Stmt
> gtestAssertThat(StatementMatcher Actual
,
202 StatementMatcher Matcher
) {
203 return gtestThatInternal(MacroType::Assert
, Actual
, Matcher
);
206 internal::BindableMatcher
<Stmt
> gtestExpectThat(StatementMatcher Actual
,
207 StatementMatcher Matcher
) {
208 return gtestThatInternal(MacroType::Expect
, Actual
, Matcher
);
211 internal::BindableMatcher
<Stmt
> gtestOnCall(StatementMatcher MockObject
,
212 llvm::StringRef MockMethodName
,
214 return gtestCallInternal(MacroType::On
, MockObject
, MockMethodName
, Args
);
217 internal::BindableMatcher
<Stmt
> gtestOnCall(StatementMatcher MockCall
,
219 return gtestCallInternal(MacroType::On
, MockCall
, Args
);
222 internal::BindableMatcher
<Stmt
> gtestExpectCall(StatementMatcher MockObject
,
223 llvm::StringRef MockMethodName
,
225 return gtestCallInternal(MacroType::Expect
, MockObject
, MockMethodName
, Args
);
228 internal::BindableMatcher
<Stmt
> gtestExpectCall(StatementMatcher MockCall
,
230 return gtestCallInternal(MacroType::Expect
, MockCall
, Args
);
233 } // end namespace ast_matchers
234 } // end namespace clang