Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/AST/ParentMap.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- 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
//  This file defines the ParentMap class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/ParentMap.h"
14
#include "clang/AST/Decl.h"
15
#include "clang/AST/Expr.h"
16
#include "clang/AST/ExprCXX.h"
17
#include "clang/AST/StmtObjC.h"
18
#include "llvm/ADT/DenseMap.h"
19
20
using namespace clang;
21
22
typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
23
24
enum OpaqueValueMode {
25
  OV_Transparent,
26
  OV_Opaque
27
};
28
29
static void BuildParentMap(MapTy& M, Stmt* S,
30
365k
                           OpaqueValueMode OVMode = OV_Transparent) {
31
365k
  if (!S)
32
1
    return;
33
365k
34
365k
  switch (S->getStmtClass()) {
35
365k
  case Stmt::PseudoObjectExprClass: {
36
747
    assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
37
747
    PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
38
747
39
747
    // If we are rebuilding the map, clear out any existing state.
40
747
    if (M[POE->getSyntacticForm()])
41
0
      for (Stmt *SubStmt : S->children())
42
0
        M[SubStmt] = nullptr;
43
747
44
747
    M[POE->getSyntacticForm()] = S;
45
747
    BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
46
747
47
747
    for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
48
747
                                              E = POE->semantics_end();
49
2.62k
         I != E; 
++I1.87k
) {
50
1.87k
      M[*I] = S;
51
1.87k
      BuildParentMap(M, *I, OV_Opaque);
52
1.87k
    }
53
747
    break;
54
365k
  }
55
365k
  case Stmt::BinaryConditionalOperatorClass: {
56
35
    assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
57
35
    BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
58
35
59
35
    M[BCO->getCommon()] = S;
60
35
    BuildParentMap(M, BCO->getCommon(), OV_Transparent);
61
35
62
35
    M[BCO->getCond()] = S;
63
35
    BuildParentMap(M, BCO->getCond(), OV_Opaque);
64
35
65
35
    M[BCO->getTrueExpr()] = S;
66
35
    BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
67
35
68
35
    M[BCO->getFalseExpr()] = S;
69
35
    BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
70
35
71
35
    break;
72
365k
  }
73
365k
  case Stmt::OpaqueValueExprClass: {
74
3.31k
    // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
75
3.31k
    // share a single source expression, but in the AST a single
76
3.31k
    // OpaqueValueExpr is shared among multiple parent expressions.
77
3.31k
    // The right thing to do is to give the OpaqueValueExpr its syntactic
78
3.31k
    // parent, then not reassign that when traversing the semantic expressions.
79
3.31k
    OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
80
3.31k
    if (OVMode == OV_Transparent || 
!M[OVE->getSourceExpr()]2.14k
) {
81
1.35k
      M[OVE->getSourceExpr()] = S;
82
1.35k
      BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
83
1.35k
    }
84
3.31k
    break;
85
365k
  }
86
365k
  case Stmt::CapturedStmtClass:
87
0
    for (Stmt *SubStmt : S->children()) {
88
0
      if (SubStmt) {
89
0
        M[SubStmt] = S;
90
0
        BuildParentMap(M, SubStmt, OVMode);
91
0
      }
92
0
    }
93
0
    if (Stmt *SubStmt = cast<CapturedStmt>(S)->getCapturedStmt()) {
94
0
      M[SubStmt] = S;
95
0
      BuildParentMap(M, SubStmt, OVMode);
96
0
    }
97
0
    break;
98
365k
  default:
99
361k
    for (Stmt *SubStmt : S->children()) {
100
338k
      if (SubStmt) {
101
337k
        M[SubStmt] = S;
102
337k
        BuildParentMap(M, SubStmt, OVMode);
103
337k
      }
104
338k
    }
105
361k
    break;
106
365k
  }
107
365k
}
108
109
20.0k
ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
110
20.0k
  if (S) {
111
19.9k
    MapTy *M = new MapTy();
112
19.9k
    BuildParentMap(*M, S);
113
19.9k
    Impl = M;
114
19.9k
  }
115
20.0k
}
116
117
20.0k
ParentMap::~ParentMap() {
118
20.0k
  delete (MapTy*) Impl;
119
20.0k
}
120
121
3.62k
void ParentMap::addStmt(Stmt* S) {
122
3.62k
  if (S) {
123
3.62k
    BuildParentMap(*(MapTy*) Impl, S);
124
3.62k
  }
125
3.62k
}
126
127
1.20k
void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
128
1.20k
  assert(S);
129
1.20k
  assert(Parent);
130
1.20k
  MapTy *M = reinterpret_cast<MapTy *>(Impl);
131
1.20k
  M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
132
1.20k
}
133
134
2.43M
Stmt* ParentMap::getParent(Stmt* S) const {
135
2.43M
  MapTy* M = (MapTy*) Impl;
136
2.43M
  MapTy::iterator I = M->find(S);
137
2.43M
  return I == M->end() ? 
nullptr78.3k
:
I->second2.35M
;
138
2.43M
}
139
140
60.6k
Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
141
60.9k
  do { S = getParent(S); } while (S && 
isa<ParenExpr>(S)60.4k
);
142
60.6k
  return S;
143
60.6k
}
144
145
3.79k
Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
146
4.45k
  do {
147
4.45k
    S = getParent(S);
148
4.45k
  }
149
4.45k
  while (S && 
(4.45k
isa<ParenExpr>(S)4.45k
||
isa<CastExpr>(S)4.40k
));
150
3.79k
151
3.79k
  return S;
152
3.79k
}
153
154
134
Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
155
142
  do {
156
142
    S = getParent(S);
157
142
  } while (S && isa<Expr>(S) && 
cast<Expr>(S)->IgnoreParenImpCasts() != S50
);
158
134
159
134
  return S;
160
134
}
161
162
0
Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
163
0
  Stmt *Paren = nullptr;
164
0
  while (isa<ParenExpr>(S)) {
165
0
    Paren = S;
166
0
    S = getParent(S);
167
0
  };
168
0
  return Paren;
169
0
}
170
171
1.06M
bool ParentMap::isConsumedExpr(Expr* E) const {
172
1.06M
  Stmt *P = getParent(E);
173
1.06M
  Stmt *DirectChild = E;
174
1.06M
175
1.06M
  // Ignore parents that don't guarantee consumption.
176
1.42M
  while (P && 
(1.40M
isa<ParenExpr>(P)1.40M
||
isa<CastExpr>(P)1.39M
||
177
1.40M
               
isa<FullExpr>(P)1.04M
)) {
178
357k
    DirectChild = P;
179
357k
    P = getParent(P);
180
357k
  }
181
1.06M
182
1.06M
  if (!P)
183
14.2k
    return false;
184
1.04M
185
1.04M
  switch (P->getStmtClass()) {
186
1.04M
    default:
187
522k
      return isa<Expr>(P);
188
1.04M
    case Stmt::DeclStmtClass:
189
36.9k
      return true;
190
1.04M
    case Stmt::BinaryOperatorClass: {
191
387k
      BinaryOperator *BE = cast<BinaryOperator>(P);
192
387k
      // If it is a comma, only the right side is consumed.
193
387k
      // If it isn't a comma, both sides are consumed.
194
387k
      return BE->getOpcode()!=BO_Comma ||
DirectChild==BE->getRHS()1.10k
;
195
1.04M
    }
196
1.04M
    case Stmt::ForStmtClass:
197
13.0k
      return DirectChild == cast<ForStmt>(P)->getCond();
198
1.04M
    case Stmt::WhileStmtClass:
199
18.4k
      return DirectChild == cast<WhileStmt>(P)->getCond();
200
1.04M
    case Stmt::DoStmtClass:
201
151
      return DirectChild == cast<DoStmt>(P)->getCond();
202
1.04M
    case Stmt::IfStmtClass:
203
33.0k
      return DirectChild == cast<IfStmt>(P)->getCond();
204
1.04M
    case Stmt::IndirectGotoStmtClass:
205
16
      return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
206
1.04M
    case Stmt::SwitchStmtClass:
207
545
      return DirectChild == cast<SwitchStmt>(P)->getCond();
208
1.04M
    case Stmt::ObjCForCollectionStmtClass:
209
128
      return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
210
1.04M
    case Stmt::ReturnStmtClass:
211
36.5k
      return true;
212
1.04M
  }
213
1.04M
}
214