Coverage Report

Created: 2022-01-25 06:29

/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
527k
                           OpaqueValueMode OVMode = OV_Transparent) {
31
527k
  if (!S)
32
1
    return;
33
34
527k
  switch (S->getStmtClass()) {
35
777
  case Stmt::PseudoObjectExprClass: {
36
777
    assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
37
0
    PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
38
39
    // If we are rebuilding the map, clear out any existing state.
40
777
    if (M[POE->getSyntacticForm()])
41
0
      for (Stmt *SubStmt : S->children())
42
0
        M[SubStmt] = nullptr;
43
44
777
    M[POE->getSyntacticForm()] = S;
45
777
    BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
46
47
777
    for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
48
777
                                              E = POE->semantics_end();
49
2.72k
         I != E; 
++I1.94k
) {
50
1.94k
      M[*I] = S;
51
1.94k
      BuildParentMap(M, *I, OV_Opaque);
52
1.94k
    }
53
777
    break;
54
0
  }
55
35
  case Stmt::BinaryConditionalOperatorClass: {
56
35
    assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
57
0
    BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
58
59
35
    M[BCO->getCommon()] = S;
60
35
    BuildParentMap(M, BCO->getCommon(), OV_Transparent);
61
62
35
    M[BCO->getCond()] = S;
63
35
    BuildParentMap(M, BCO->getCond(), OV_Opaque);
64
65
35
    M[BCO->getTrueExpr()] = S;
66
35
    BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
67
68
35
    M[BCO->getFalseExpr()] = S;
69
35
    BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
70
71
35
    break;
72
0
  }
73
3.48k
  case Stmt::OpaqueValueExprClass: {
74
    // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
75
    // share a single source expression, but in the AST a single
76
    // OpaqueValueExpr is shared among multiple parent expressions.
77
    // The right thing to do is to give the OpaqueValueExpr its syntactic
78
    // parent, then not reassign that when traversing the semantic expressions.
79
3.48k
    OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
80
3.48k
    if (OVMode == OV_Transparent || 
!M[OVE->getSourceExpr()]2.21k
) {
81
1.46k
      M[OVE->getSourceExpr()] = S;
82
1.46k
      BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
83
1.46k
    }
84
3.48k
    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
523k
  default:
99
523k
    for (Stmt *SubStmt : S->children()) {
100
493k
      if (SubStmt) {
101
492k
        M[SubStmt] = S;
102
492k
        BuildParentMap(M, SubStmt, OVMode);
103
492k
      }
104
493k
    }
105
523k
    break;
106
527k
  }
107
527k
}
108
109
26.2k
ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
110
26.2k
  if (S) {
111
26.1k
    MapTy *M = new MapTy();
112
26.1k
    BuildParentMap(*M, S);
113
26.1k
    Impl = M;
114
26.1k
  }
115
26.2k
}
116
117
26.2k
ParentMap::~ParentMap() {
118
26.2k
  delete (MapTy*) Impl;
119
26.2k
}
120
121
4.68k
void ParentMap::addStmt(Stmt* S) {
122
4.68k
  if (S) {
123
4.68k
    BuildParentMap(*(MapTy*) Impl, S);
124
4.68k
  }
125
4.68k
}
126
127
2.62k
void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
128
2.62k
  assert(S);
129
0
  assert(Parent);
130
0
  MapTy *M = reinterpret_cast<MapTy *>(Impl);
131
2.62k
  M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
132
2.62k
}
133
134
7.51M
Stmt* ParentMap::getParent(Stmt* S) const {
135
7.51M
  MapTy* M = (MapTy*) Impl;
136
7.51M
  return M->lookup(S);
137
7.51M
}
138
139
74.0k
Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
140
74.3k
  do { S = getParent(S); } while (S && 
isa<ParenExpr>(S)73.9k
);
141
74.0k
  return S;
142
74.0k
}
143
144
3.92k
Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
145
4.61k
  do {
146
4.61k
    S = getParent(S);
147
4.61k
  }
148
4.61k
  while (S && 
(4.61k
isa<ParenExpr>(S)4.61k
||
isa<CastExpr>(S)4.56k
));
149
150
3.92k
  return S;
151
3.92k
}
152
153
134
Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
154
142
  do {
155
142
    S = getParent(S);
156
142
  } while (S && isa<Expr>(S) && 
cast<Expr>(S)->IgnoreParenImpCasts() != S50
);
157
158
134
  return S;
159
134
}
160
161
0
Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
162
0
  Stmt *Paren = nullptr;
163
0
  while (isa<ParenExpr>(S)) {
164
0
    Paren = S;
165
0
    S = getParent(S);
166
0
  };
167
0
  return Paren;
168
0
}
169
170
1.16M
bool ParentMap::isConsumedExpr(Expr* E) const {
171
1.16M
  Stmt *P = getParent(E);
172
1.16M
  Stmt *DirectChild = E;
173
174
  // Ignore parents that don't guarantee consumption.
175
1.62M
  while (P && 
(1.60M
isa<ParenExpr>(P)1.60M
||
isa<CastExpr>(P)1.59M
||
176
1.60M
               
isa<FullExpr>(P)1.15M
)) {
177
459k
    DirectChild = P;
178
459k
    P = getParent(P);
179
459k
  }
180
181
1.16M
  if (!P)
182
16.2k
    return false;
183
184
1.15M
  switch (P->getStmtClass()) {
185
612k
    default:
186
612k
      return isa<Expr>(P);
187
40.4k
    case Stmt::DeclStmtClass:
188
40.4k
      return true;
189
398k
    case Stmt::BinaryOperatorClass: {
190
398k
      BinaryOperator *BE = cast<BinaryOperator>(P);
191
      // If it is a comma, only the right side is consumed.
192
      // If it isn't a comma, both sides are consumed.
193
398k
      return BE->getOpcode()!=BO_Comma ||
DirectChild==BE->getRHS()1.10k
;
194
0
    }
195
13.6k
    case Stmt::ForStmtClass:
196
13.6k
      return DirectChild == cast<ForStmt>(P)->getCond();
197
18.5k
    case Stmt::WhileStmtClass:
198
18.5k
      return DirectChild == cast<WhileStmt>(P)->getCond();
199
521
    case Stmt::DoStmtClass:
200
521
      return DirectChild == cast<DoStmt>(P)->getCond();
201
37.3k
    case Stmt::IfStmtClass:
202
37.3k
      return DirectChild == cast<IfStmt>(P)->getCond();
203
16
    case Stmt::IndirectGotoStmtClass:
204
16
      return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
205
572
    case Stmt::SwitchStmtClass:
206
572
      return DirectChild == cast<SwitchStmt>(P)->getCond();
207
130
    case Stmt::ObjCForCollectionStmtClass:
208
130
      return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
209
27.8k
    case Stmt::ReturnStmtClass:
210
27.8k
      return true;
211
1.15M
  }
212
1.15M
}
213