File: GtestMatchers.cpp

package info (click to toggle)
llvm-toolchain-11 1%3A11.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 995,808 kB
  • sloc: cpp: 4,767,656; ansic: 760,916; asm: 477,436; python: 170,940; objc: 69,804; lisp: 29,914; sh: 23,855; f90: 18,173; pascal: 7,551; perl: 7,471; ml: 5,603; awk: 3,489; makefile: 2,573; xml: 915; cs: 573; fortran: 503; javascript: 452
file content (104 lines) | stat: -rw-r--r-- 3,631 bytes parent folder | download | duplicates (2)
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)),
                  hasArgument(2, Left), hasArgument(3, Right));
}

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

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