aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/ASTMatchers/GtestMatchers.cpp
blob: 0e587c0c3b9f66e22b3630458bc1bd560dff8ccd (plain) (blame)
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
//===- 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
//
//===----------------------------------------------------------------------===//

#include "clang/ASTMatchers/GtestMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Timer.h"
#include <deque>
#include <memory>
#include <set>

namespace clang {
namespace ast_matchers {

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 getAssertMacro(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return "ASSERT_EQ";
    case GtestCmp::Ne:
      return "ASSERT_NE";
    case GtestCmp::Ge:
      return "ASSERT_GE";
    case GtestCmp::Gt:
      return "ASSERT_GT";
    case GtestCmp::Le:
      return "ASSERT_LE";
    case GtestCmp::Lt:
      return "ASSERT_LT";
  }
  llvm_unreachable("Unhandled GtestCmp enum");
}

static llvm::StringRef getExpectMacro(GtestCmp Cmp) {
  switch (Cmp) {
    case GtestCmp::Eq:
      return "EXPECT_EQ";
    case GtestCmp::Ne:
      return "EXPECT_NE";
    case GtestCmp::Ge:
      return "EXPECT_GE";
    case GtestCmp::Gt:
      return "EXPECT_GT";
    case GtestCmp::Le:
      return "EXPECT_LE";
    case GtestCmp::Lt:
      return "EXPECT_LT";
  }
  llvm_unreachable("Unhandled GtestCmp 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.
internal::BindableMatcher<Stmt> gtestAssert(GtestCmp Cmp, StatementMatcher Left,
                                            StatementMatcher Right) {
  return callExpr(callee(getComparisonDecl(Cmp)),
                  isExpandedFromMacro(getAssertMacro(Cmp).str()),
                  hasArgument(2, Left), hasArgument(3, Right));
}

internal::BindableMatcher<Stmt> gtestExpect(GtestCmp Cmp, StatementMatcher Left,
                                            StatementMatcher Right) {
  return callExpr(callee(getComparisonDecl(Cmp)),
                  isExpandedFromMacro(getExpectMacro(Cmp).str()),
                  hasArgument(2, Left), hasArgument(3, Right));
}

} // end namespace ast_matchers
} // end namespace clang