Coverage Report

Created: 2020-09-22 08:39

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/ASTMatchers/GtestMatchers.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- GtestMatchers.cpp - AST Matchers for Gtest ---------------*- C++ -*-===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/ASTMatchers/GtestMatchers.h"
10
#include "clang/ASTMatchers/ASTMatchFinder.h"
11
#include "clang/AST/ASTConsumer.h"
12
#include "clang/AST/ASTContext.h"
13
#include "clang/AST/RecursiveASTVisitor.h"
14
#include "llvm/ADT/DenseMap.h"
15
#include "llvm/ADT/StringMap.h"
16
#include "llvm/Support/Timer.h"
17
#include <deque>
18
#include <memory>
19
#include <set>
20
21
namespace clang {
22
namespace ast_matchers {
23
24
10
static DeclarationMatcher getComparisonDecl(GtestCmp Cmp) {
25
10
  switch (Cmp) {
26
5
    case GtestCmp::Eq:
27
5
      return cxxMethodDecl(hasName("Compare"),
28
5
                           ofClass(cxxRecordDecl(isSameOrDerivedFrom(
29
5
                               hasName("::testing::internal::EqHelper")))));
30
1
    case GtestCmp::Ne:
31
1
      return functionDecl(hasName("::testing::internal::CmpHelperNE"));
32
1
    case GtestCmp::Ge:
33
1
      return functionDecl(hasName("::testing::internal::CmpHelperGE"));
34
1
    case GtestCmp::Gt:
35
1
      return functionDecl(hasName("::testing::internal::CmpHelperGT"));
36
1
    case GtestCmp::Le:
37
1
      return functionDecl(hasName("::testing::internal::CmpHelperLE"));
38
1
    case GtestCmp::Lt:
39
1
      return functionDecl(hasName("::testing::internal::CmpHelperLT"));
40
0
  }
41
0
  llvm_unreachable("Unhandled GtestCmp enum");
42
0
}
43
44
3
static llvm::StringRef getAssertMacro(GtestCmp Cmp) {
45
3
  switch (Cmp) {
46
3
    case GtestCmp::Eq:
47
3
      return "ASSERT_EQ";
48
0
    case GtestCmp::Ne:
49
0
      return "ASSERT_NE";
50
0
    case GtestCmp::Ge:
51
0
      return "ASSERT_GE";
52
0
    case GtestCmp::Gt:
53
0
      return "ASSERT_GT";
54
0
    case GtestCmp::Le:
55
0
      return "ASSERT_LE";
56
0
    case GtestCmp::Lt:
57
0
      return "ASSERT_LT";
58
0
  }
59
0
  llvm_unreachable("Unhandled GtestCmp enum");
60
0
}
61
62
7
static llvm::StringRef getExpectMacro(GtestCmp Cmp) {
63
7
  switch (Cmp) {
64
2
    case GtestCmp::Eq:
65
2
      return "EXPECT_EQ";
66
1
    case GtestCmp::Ne:
67
1
      return "EXPECT_NE";
68
1
    case GtestCmp::Ge:
69
1
      return "EXPECT_GE";
70
1
    case GtestCmp::Gt:
71
1
      return "EXPECT_GT";
72
1
    case GtestCmp::Le:
73
1
      return "EXPECT_LE";
74
1
    case GtestCmp::Lt:
75
1
      return "EXPECT_LT";
76
0
  }
77
0
  llvm_unreachable("Unhandled GtestCmp enum");
78
0
}
79
80
// In general, AST matchers cannot match calls to macros. However, we can
81
// simulate such matches if the macro definition has identifiable elements that
82
// themselves can be matched. In that case, we can match on those elements and
83
// then check that the match occurs within an expansion of the desired
84
// macro. The more uncommon the identified elements, the more efficient this
85
// process will be.
86
//
87
// We use this approach to implement the derived matchers gtestAssert and
88
// gtestExpect.
89
internal::BindableMatcher<Stmt> gtestAssert(GtestCmp Cmp, StatementMatcher Left,
90
3
                                            StatementMatcher Right) {
91
3
  return callExpr(callee(getComparisonDecl(Cmp)),
92
3
                  isExpandedFromMacro(getAssertMacro(Cmp)),
93
3
                  hasArgument(2, Left), hasArgument(3, Right));
94
3
}
95
96
internal::BindableMatcher<Stmt> gtestExpect(GtestCmp Cmp, StatementMatcher Left,
97
7
                                            StatementMatcher Right) {
98
7
  return callExpr(callee(getComparisonDecl(Cmp)),
99
7
                  isExpandedFromMacro(getExpectMacro(Cmp)),
100
7
                  hasArgument(2, Left), hasArgument(3, Right));
101
7
}
102
103
} // end namespace ast_matchers
104
} // end namespace clang