#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,
};
}
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();
}
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");
}
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) {
switch (Args) {
case MockArgs::None:
return cxxMemberCallExpr(
isExpandedFromMacro(getMacroName(Macro, "CALL")),
callee(functionDecl(hasName(getSpecSetterName(Macro)))),
onImplicitObjectArgument(ignoringImplicit(MockCall)));
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);
}
} }