Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
412k
                           OpaqueValueMode OVMode = OV_Transparent) {
31
412k
  if (!S)
32
1
    return;
33
412k
34
412k
  switch (S->getStmtClass()) {
35
762
  case Stmt::PseudoObjectExprClass: {
36
762
    assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
37
762
    PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
38
762
39
762
    // If we are rebuilding the map, clear out any existing state.
40
762
    if (M[POE->getSyntacticForm()])
41
0
      for (Stmt *SubStmt : S->children())
42
0
        M[SubStmt] = nullptr;
43
762
44
762
    M[POE->getSyntacticForm()] = S;
45
762
    BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
46
762
47
762
    for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
48
762
                                              E = POE->semantics_end();
49
2.67k
         I != E; 
++I1.91k
) {
50
1.91k
      M[*I] = S;
51
1.91k
      BuildParentMap(M, *I, OV_Opaque);
52
1.91k
    }
53
762
    break;
54
0
  }
55
35
  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
0
  }
73
3.43k
  case Stmt::OpaqueValueExprClass: {
74
3.43k
    // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
75
3.43k
    // share a single source expression, but in the AST a single
76
3.43k
    // OpaqueValueExpr is shared among multiple parent expressions.
77
3.43k
    // The right thing to do is to give the OpaqueValueExpr its syntactic
78
3.43k
    // parent, then not reassign that when traversing the semantic expressions.
79
3.43k
    OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
80
3.43k
    if (OVMode == OV_Transparent || 
!M[OVE->getSourceExpr()]2.18k
) {
81
1.44k
      M[OVE->getSourceExpr()] = S;
82
1.44k
      BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
83
1.44k
    }
84
3.43k
    break;
85
0
  }
86
0
  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
408k
  default:
99
408k
    for (Stmt *SubStmt : S->children()) {
100
383k
      if (SubStmt) {
101
382k
        M[SubStmt] = S;
102
382k
        BuildParentMap(M, SubStmt, OVMode);
103
382k
      }
104
383k
    }
105
408k
    break;
106
412k
  }
107
412k
}
108
109
21.9k
ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
110
21.9k
  if (S) {
111
21.7k
    MapTy *M = new MapTy();
112
21.7k
    BuildParentMap(*M, S);
113
21.7k
    Impl = M;
114
21.7k
  }
115
21.9k
}
116
117
21.9k
ParentMap::~ParentMap() {
118
21.9k
  delete (MapTy*) Impl;
119
21.9k
}
120
121
4.14k
void ParentMap::addStmt(Stmt* S) {
122
4.14k
  if (S) {
123
4.14k
    BuildParentMap(*(MapTy*) Impl, S);
124
4.14k
  }
125
4.14k
}
126
127
1.70k
void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
128
1.70k
  assert(S);
129
1.70k
  assert(Parent);
130
1.70k
  MapTy *M = reinterpret_cast<MapTy *>(Impl);
131
1.70k
  M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
132
1.70k
}
133
134
4.15M
Stmt* ParentMap::getParent(Stmt* S) const {
135
4.15M
  MapTy* M = (MapTy*) Impl;
136
4.15M
  MapTy::iterator I = M->find(S);
137
4.15M
  return I == M->end() ? 
nullptr143k
:
I->second4.00M
;
138
4.15M
}
139
140
62.3k
Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
141
62.5k
  do { S = getParent(S); } while (S && 
isa<ParenExpr>(S)62.2k
);
142
62.3k
  return S;
143
62.3k
}
144
145
3.82k
Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
146
4.48k
  do {
147
4.48k
    S = getParent(S);
148
4.48k
  }
149
4.48k
  while (S && 
(4.48k
isa<ParenExpr>(S)4.48k
||
isa<CastExpr>(S)4.43k
));
150
3.82k
151
3.82k
  return S;
152
3.82k
}
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.39M
bool ParentMap::isConsumedExpr(Expr* E) const {
172
1.39M
  Stmt *P = getParent(E);
173
1.39M
  Stmt *DirectChild = E;
174
1.39M
175
1.39M
  // Ignore parents that don't guarantee consumption.
176
1.87M
  while (P && 
(1.84M
isa<ParenExpr>(P)1.84M
||
isa<CastExpr>(P)1.83M
||
177
1.84M
               
isa<FullExpr>(P)1.37M
)) {
178
474k
    DirectChild = P;
179
474k
    P = getParent(P);
180
474k
  }
181
1.39M
182
1.39M
  if (!P)
183
23.0k
    return false;
184
1.37M
185
1.37M
  switch (P->getStmtClass()) {
186
799k
    default:
187
799k
      return isa<Expr>(P);
188
60.4k
    case Stmt::DeclStmtClass:
189
60.4k
      return true;
190
389k
    case Stmt::BinaryOperatorClass: {
191
389k
      BinaryOperator *BE = cast<BinaryOperator>(P);
192
389k
      // If it is a comma, only the right side is consumed.
193
389k
      // If it isn't a comma, both sides are consumed.
194
389k
      return BE->getOpcode()!=BO_Comma ||
DirectChild==BE->getRHS()1.10k
;
195
0
    }
196
13.2k
    case Stmt::ForStmtClass:
197
13.2k
      return DirectChild == cast<ForStmt>(P)->getCond();
198
18.5k
    case Stmt::WhileStmtClass:
199
18.5k
      return DirectChild == cast<WhileStmt>(P)->getCond();
200
181
    case Stmt::DoStmtClass:
201
181
      return DirectChild == cast<DoStmt>(P)->getCond();
202
38.8k
    case Stmt::IfStmtClass:
203
38.8k
      return DirectChild == cast<IfStmt>(P)->getCond();
204
16
    case Stmt::IndirectGotoStmtClass:
205
16
      return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
206
551
    case Stmt::SwitchStmtClass:
207
551
      return DirectChild == cast<SwitchStmt>(P)->getCond();
208
128
    case Stmt::ObjCForCollectionStmtClass:
209
128
      return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
210
52.8k
    case Stmt::ReturnStmtClass:
211
52.8k
      return true;
212
1.37M
  }
213
1.37M
}
214