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/StringRef.h"
27 namespace ast_matchers
{
30 enum class MacroType
{
38 static DeclarationMatcher
getComparisonDecl(GtestCmp Cmp
) {
41 return cxxMethodDecl(hasName("Compare"),
42 ofClass(cxxRecordDecl(isSameOrDerivedFrom(
43 hasName("::testing::internal::EqHelper")))));
45 return functionDecl(hasName("::testing::internal::CmpHelperNE"));
47 return functionDecl(hasName("::testing::internal::CmpHelperGE"));
49 return functionDecl(hasName("::testing::internal::CmpHelperGT"));
51 return functionDecl(hasName("::testing::internal::CmpHelperLE"));
53 return functionDecl(hasName("::testing::internal::CmpHelperLT"));
55 llvm_unreachable("Unhandled GtestCmp enum");
58 static llvm::StringRef
getMacroTypeName(MacroType Macro
) {
60 case MacroType::Expect
:
62 case MacroType::Assert
:
67 llvm_unreachable("Unhandled MacroType enum");
70 static llvm::StringRef
getComparisonTypeName(GtestCmp Cmp
) {
85 llvm_unreachable("Unhandled GtestCmp enum");
88 static std::string
getMacroName(MacroType Macro
, GtestCmp Cmp
) {
89 return (getMacroTypeName(Macro
) + "_" + getComparisonTypeName(Cmp
)).str();
92 static std::string
getMacroName(MacroType Macro
, llvm::StringRef Operation
) {
93 return (getMacroTypeName(Macro
) + "_" + Operation
).str();
96 // Under the hood, ON_CALL is expanded to a call to `InternalDefaultActionSetAt`
97 // to set a default action spec to the underlying function mocker, while
98 // EXPECT_CALL is expanded to a call to `InternalExpectedAt` to set a new
100 static llvm::StringRef
getSpecSetterName(MacroType Macro
) {
103 return "InternalDefaultActionSetAt";
104 case MacroType::Expect
:
105 return "InternalExpectedAt";
107 llvm_unreachable("Unhandled MacroType enum");
109 llvm_unreachable("Unhandled MacroType enum");
112 // In general, AST matchers cannot match calls to macros. However, we can
113 // simulate such matches if the macro definition has identifiable elements that
114 // themselves can be matched. In that case, we can match on those elements and
115 // then check that the match occurs within an expansion of the desired
116 // macro. The more uncommon the identified elements, the more efficient this
119 // We use this approach to implement the derived matchers gtestAssert and
121 static internal::BindableMatcher
<Stmt
>
122 gtestComparisonInternal(MacroType Macro
, GtestCmp Cmp
, StatementMatcher Left
,
123 StatementMatcher Right
) {
124 return callExpr(isExpandedFromMacro(getMacroName(Macro
, Cmp
)),
125 callee(getComparisonDecl(Cmp
)), hasArgument(2, Left
),
126 hasArgument(3, Right
));
129 static internal::BindableMatcher
<Stmt
>
130 gtestThatInternal(MacroType Macro
, StatementMatcher Actual
,
131 StatementMatcher Matcher
) {
132 return cxxOperatorCallExpr(
133 isExpandedFromMacro(getMacroName(Macro
, "THAT")),
134 hasOverloadedOperatorName("()"), hasArgument(2, Actual
),
136 0, expr(hasType(classTemplateSpecializationDecl(hasName(
137 "::testing::internal::PredicateFormatterFromMatcher"))),
139 callExpr(callee(functionDecl(hasName(
140 "::testing::internal::"
141 "MakePredicateFormatterFromMatcher"))),
142 hasArgument(0, ignoringImplicit(Matcher
)))))));
145 static internal::BindableMatcher
<Stmt
>
146 gtestCallInternal(MacroType Macro
, StatementMatcher MockCall
, MockArgs Args
) {
147 // A ON_CALL or EXPECT_CALL macro expands to different AST structures
148 // depending on whether the mock method has arguments or not.
151 // `ON_CALL(mock, TwoParamMethod)` is expanded to
152 // `mock.gmock_TwoArgsMethod(WithoutMatchers(),
153 // nullptr).InternalDefaultActionSetAt(...)`.
154 // EXPECT_CALL is the same except
155 // that it calls `InternalExpectedAt` instead of `InternalDefaultActionSetAt`
158 return cxxMemberCallExpr(
159 isExpandedFromMacro(getMacroName(Macro
, "CALL")),
160 callee(functionDecl(hasName(getSpecSetterName(Macro
)))),
161 onImplicitObjectArgument(ignoringImplicit(MockCall
)));
163 // `ON_CALL(mock, TwoParamMethod(m1, m2))` is expanded to
164 // `mock.gmock_TwoParamMethod(m1,m2)(WithoutMatchers(),
165 // nullptr).InternalDefaultActionSetAt(...)`.
166 // EXPECT_CALL is the same except that it calls `InternalExpectedAt` instead
167 // of `InternalDefaultActionSetAt` in the end.
169 return cxxMemberCallExpr(
170 isExpandedFromMacro(getMacroName(Macro
, "CALL")),
171 callee(functionDecl(hasName(getSpecSetterName(Macro
)))),
172 onImplicitObjectArgument(ignoringImplicit(cxxOperatorCallExpr(
173 hasOverloadedOperatorName("()"), argumentCountIs(3),
174 hasArgument(0, ignoringImplicit(MockCall
))))));
176 llvm_unreachable("Unhandled MockArgs enum");
179 static internal::BindableMatcher
<Stmt
>
180 gtestCallInternal(MacroType Macro
, StatementMatcher MockObject
,
181 llvm::StringRef MockMethodName
, MockArgs Args
) {
182 return gtestCallInternal(
185 onImplicitObjectArgument(MockObject
),
186 callee(functionDecl(hasName(("gmock_" + MockMethodName
).str())))),
190 internal::BindableMatcher
<Stmt
> gtestAssert(GtestCmp Cmp
, StatementMatcher Left
,
191 StatementMatcher Right
) {
192 return gtestComparisonInternal(MacroType::Assert
, Cmp
, Left
, Right
);
195 internal::BindableMatcher
<Stmt
> gtestExpect(GtestCmp Cmp
, StatementMatcher Left
,
196 StatementMatcher Right
) {
197 return gtestComparisonInternal(MacroType::Expect
, Cmp
, Left
, Right
);
200 internal::BindableMatcher
<Stmt
> gtestAssertThat(StatementMatcher Actual
,
201 StatementMatcher Matcher
) {
202 return gtestThatInternal(MacroType::Assert
, Actual
, Matcher
);
205 internal::BindableMatcher
<Stmt
> gtestExpectThat(StatementMatcher Actual
,
206 StatementMatcher Matcher
) {
207 return gtestThatInternal(MacroType::Expect
, Actual
, Matcher
);
210 internal::BindableMatcher
<Stmt
> gtestOnCall(StatementMatcher MockObject
,
211 llvm::StringRef MockMethodName
,
213 return gtestCallInternal(MacroType::On
, MockObject
, MockMethodName
, Args
);
216 internal::BindableMatcher
<Stmt
> gtestOnCall(StatementMatcher MockCall
,
218 return gtestCallInternal(MacroType::On
, MockCall
, Args
);
221 internal::BindableMatcher
<Stmt
> gtestExpectCall(StatementMatcher MockObject
,
222 llvm::StringRef MockMethodName
,
224 return gtestCallInternal(MacroType::Expect
, MockObject
, MockMethodName
, Args
);
227 internal::BindableMatcher
<Stmt
> gtestExpectCall(StatementMatcher MockCall
,
229 return gtestCallInternal(MacroType::Expect
, MockCall
, Args
);
232 } // end namespace ast_matchers
233 } // end namespace clang