Coverage Report

Created: 2022-07-16 07:03

/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
554k
                           OpaqueValueMode OVMode = OV_Transparent) {
31
554k
  if (!S)
32
1
    return;
33
34
554k
  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
37
  case Stmt::BinaryConditionalOperatorClass: {
56
37
    assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
57
0
    BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
58
59
37
    M[BCO->getCommon()] = S;
60
37
    BuildParentMap(M, BCO->getCommon(), OV_Transparent);
61
62
37
    M[BCO->getCond()] = S;
63
37
    BuildParentMap(M, BCO->getCond(), OV_Opaque);
64
65
37
    M[BCO->getTrueExpr()] = S;
66
37
    BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
67
68
37
    M[BCO->getFalseExpr()] = S;
69
37
    BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
70
71
37
    break;
72
0
  }
73
3.53k
  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.53k
    OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
80
3.53k
    if (OVMode == OV_Transparent || 
!M[OVE->getSourceExpr()]2.22k
) {
81
1.50k
      M[OVE->getSourceExpr()] = S;
82
1.50k
      BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
83
1.50k
    }
84
3.53k
    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
550k
  default:
99
550k
    for (Stmt *SubStmt : S->children()) {
100
519k
      if (SubStmt) {
101
518k
        M[SubStmt] = S;
102
518k
        BuildParentMap(M, SubStmt, OVMode);
103
518k
      }
104
519k
    }
105
550k
    break;
106
554k
  }
107
554k
}
108
109
27.2k
ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
110
27.2k
  if (S) {
111
27.1k
    MapTy *M = new MapTy();
112
27.1k
    BuildParentMap(*M, S);
113
27.1k
    Impl = M;
114
27.1k
  }
115
27.2k
}
116
117
27.2k
ParentMap::~ParentMap() {
118
27.2k
  delete (MapTy*) Impl;
119
27.2k
}
120
121
4.72k
void ParentMap::addStmt(Stmt* S) {
122
4.72k
  if (S) {
123
4.72k
    BuildParentMap(*(MapTy*) Impl, S);
124
4.72k
  }
125
4.72k
}
126
127
2.76k
void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
128
2.76k
  assert(S);
129
0
  assert(Parent);
130
0
  MapTy *M = reinterpret_cast<MapTy *>(Impl);
131
2.76k
  M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
132
2.76k
}
133
134
8.22M
Stmt* ParentMap::getParent(Stmt* S) const {
135
8.22M
  MapTy* M = (MapTy*) Impl;
136
8.22M
  return M->lookup(S);
137
8.22M
}
138
139
74.9k
Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
140
75.2k
  do { S = getParent(S); } while (S && 
isa<ParenExpr>(S)74.7k
);
141
74.9k
  return S;
142
74.9k
}
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.55M
bool ParentMap::isConsumedExpr(Expr* E) const {
171
1.55M
  Stmt *P = getParent(E);
172
1.55M
  Stmt *DirectChild = E;
173
174
  // Ignore parents that don't guarantee consumption.
175
2.12M
  while (P && 
(2.10M
isa<ParenExpr>(P)2.10M
||
isa<CastExpr>(P)2.08M
||
176
2.10M
               
isa<FullExpr>(P)1.54M
)) {
177
562k
    DirectChild = P;
178
562k
    P = getParent(P);
179
562k
  }
180
181
1.55M
  if (!P)
182
16.3k
    return false;
183
184
1.54M
  switch (P->getStmtClass()) {
185
691k
    default:
186
691k
      return isa<Expr>(P);
187
54.7k
    case Stmt::DeclStmtClass:
188
54.7k
      return true;
189
692k
    case Stmt::BinaryOperatorClass: {
190
692k
      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
692k
      return BE->getOpcode()!=BO_Comma ||
DirectChild==BE->getRHS()1.10k
;
194
0
    }
195
19.0k
    case Stmt::ForStmtClass:
196
19.0k
      return DirectChild == cast<ForStmt>(P)->getCond();
197
18.5k
    case Stmt::WhileStmtClass:
198
18.5k
      return DirectChild == cast<WhileStmt>(P)->getCond();
199
523
    case Stmt::DoStmtClass:
200
523
      return DirectChild == cast<DoStmt>(P)->getCond();
201
37.2k
    case Stmt::IfStmtClass:
202
37.2k
      return DirectChild == cast<IfStmt>(P)->getCond();
203
16
    case Stmt::IndirectGotoStmtClass:
204
16
      return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
205
581
    case Stmt::SwitchStmtClass:
206
581
      return DirectChild == cast<SwitchStmt>(P)->getCond();
207
130
    case Stmt::ObjCForCollectionStmtClass:
208
130
      return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
209
28.0k
    case Stmt::ReturnStmtClass:
210
28.0k
      return true;
211
1.54M
  }
212
1.54M
}
213