Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Analysis/ConstructionContext.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ConstructionContext.cpp - CFG constructor information --------------===//
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 ConstructionContext class and its sub-classes,
10
// which represent various different ways of constructing C++ objects
11
// with the additional information the users may want to know about
12
// the constructor.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "clang/Analysis/ConstructionContext.h"
17
#include "clang/AST/ExprObjC.h"
18
19
using namespace clang;
20
21
const ConstructionContextLayer *
22
ConstructionContextLayer::create(BumpVectorContext &C,
23
                                 const ConstructionContextItem &Item,
24
251k
                                 const ConstructionContextLayer *Parent) {
25
251k
  ConstructionContextLayer *CC =
26
251k
      C.getAllocator().Allocate<ConstructionContextLayer>();
27
251k
  return new (CC) ConstructionContextLayer(Item, Parent);
28
251k
}
29
30
bool ConstructionContextLayer::isStrictlyMoreSpecificThan(
31
8.56k
    const ConstructionContextLayer *Other) const {
32
8.56k
  const ConstructionContextLayer *Self = this;
33
20.8k
  while (true) {
34
20.8k
    if (!Other)
35
8.56k
      return Self;
36
12.3k
    if (!Self || !(Self->Item == Other->Item))
37
0
      return false;
38
12.3k
    Self = Self->getParent();
39
12.3k
    Other = Other->getParent();
40
12.3k
  }
41
8.56k
  
llvm_unreachable0
("The above loop can only be terminated via return!");
42
8.56k
}
43
44
const ConstructionContext *
45
ConstructionContext::createMaterializedTemporaryFromLayers(
46
    BumpVectorContext &C, const MaterializeTemporaryExpr *MTE,
47
    const CXXBindTemporaryExpr *BTE,
48
8.04k
    const ConstructionContextLayer *ParentLayer) {
49
8.04k
  assert(MTE);
50
8.04k
51
8.04k
  // If the object requires destruction and is not lifetime-extended,
52
8.04k
  // then it must have a BTE within its MTE, otherwise it shouldn't.
53
8.04k
  // FIXME: This should be an assertion.
54
8.04k
  if (!BTE && 
!(5.68k
MTE->getType().getCanonicalType()->getAsCXXRecordDecl()
55
5.68k
                    ->hasTrivialDestructor() ||
56
5.68k
                
MTE->getStorageDuration() != SD_FullExpression28
)) {
57
4
    return nullptr;
58
4
  }
59
8.04k
60
8.04k
  // If the temporary is lifetime-extended, don't save the BTE,
61
8.04k
  // because we don't need a temporary destructor, but an automatic
62
8.04k
  // destructor.
63
8.04k
  if (MTE->getStorageDuration() != SD_FullExpression) {
64
580
    BTE = nullptr;
65
580
  }
66
8.04k
67
8.04k
  // Handle pre-C++17 copy and move elision.
68
8.04k
  const CXXConstructExpr *ElidedCE = nullptr;
69
8.04k
  const ConstructionContext *ElidedCC = nullptr;
70
8.04k
  if (ParentLayer) {
71
4.85k
    const ConstructionContextItem &ElidedItem = ParentLayer->getItem();
72
4.85k
    assert(ElidedItem.getKind() ==
73
4.85k
           ConstructionContextItem::ElidableConstructorKind);
74
4.85k
    ElidedCE = cast<CXXConstructExpr>(ElidedItem.getStmt());
75
4.85k
    assert(ElidedCE->isElidable());
76
4.85k
    // We're creating a construction context that might have already
77
4.85k
    // been created elsewhere. Maybe we should unique our construction
78
4.85k
    // contexts. That's what we often do, but in this case it's unlikely
79
4.85k
    // to bring any benefits.
80
4.85k
    ElidedCC = createFromLayers(C, ParentLayer->getParent());
81
4.85k
    if (!ElidedCC) {
82
0
      // We may fail to create the elided construction context.
83
0
      // In this case, skip copy elision entirely.
84
0
      return create<SimpleTemporaryObjectConstructionContext>(C, BTE, MTE);
85
0
    }
86
4.85k
    return create<ElidedTemporaryObjectConstructionContext>(
87
4.85k
        C, BTE, MTE, ElidedCE, ElidedCC);
88
4.85k
  }
89
3.18k
90
3.18k
  // This is a normal temporary.
91
3.18k
  assert(!ParentLayer);
92
3.18k
  return create<SimpleTemporaryObjectConstructionContext>(C, BTE, MTE);
93
3.18k
}
94
95
const ConstructionContext *ConstructionContext::createBoundTemporaryFromLayers(
96
    BumpVectorContext &C, const CXXBindTemporaryExpr *BTE,
97
4.00k
    const ConstructionContextLayer *ParentLayer) {
98
4.00k
  if (!ParentLayer) {
99
292
    // A temporary object that doesn't require materialization.
100
292
    // In particular, it shouldn't require copy elision, because
101
292
    // copy/move constructors take a reference, which requires
102
292
    // materialization to obtain the glvalue.
103
292
    return create<SimpleTemporaryObjectConstructionContext>(C, BTE,
104
292
                                                            /*MTE=*/nullptr);
105
292
  }
106
3.71k
107
3.71k
  const ConstructionContextItem &ParentItem = ParentLayer->getItem();
108
3.71k
  switch (ParentItem.getKind()) {
109
66
  case ConstructionContextItem::VariableKind: {
110
66
    const auto *DS = cast<DeclStmt>(ParentItem.getStmt());
111
66
    assert(!cast<VarDecl>(DS->getSingleDecl())->getType().getCanonicalType()
112
66
                            ->getAsCXXRecordDecl()->hasTrivialDestructor());
113
66
    return create<CXX17ElidedCopyVariableConstructionContext>(C, DS, BTE);
114
0
  }
115
0
  case ConstructionContextItem::NewAllocatorKind: {
116
0
    llvm_unreachable("This context does not accept a bound temporary!");
117
0
  }
118
72
  case ConstructionContextItem::ReturnKind: {
119
72
    assert(ParentLayer->isLast());
120
72
    const auto *RS = cast<ReturnStmt>(ParentItem.getStmt());
121
72
    assert(!RS->getRetValue()->getType().getCanonicalType()
122
72
              ->getAsCXXRecordDecl()->hasTrivialDestructor());
123
72
    return create<CXX17ElidedCopyReturnedValueConstructionContext>(C, RS,
124
72
                                                                   BTE);
125
0
  }
126
0
127
2.36k
  case ConstructionContextItem::MaterializationKind: {
128
2.36k
    // No assert. We may have an elidable copy on the grandparent layer.
129
2.36k
    const auto *MTE = cast<MaterializeTemporaryExpr>(ParentItem.getStmt());
130
2.36k
    return createMaterializedTemporaryFromLayers(C, MTE, BTE,
131
2.36k
                                                 ParentLayer->getParent());
132
0
  }
133
0
  case ConstructionContextItem::TemporaryDestructorKind: {
134
0
    llvm_unreachable("Duplicate CXXBindTemporaryExpr in the AST!");
135
0
  }
136
0
  case ConstructionContextItem::ElidedDestructorKind: {
137
0
    llvm_unreachable("Elided destructor items are not produced by the CFG!");
138
0
  }
139
0
  case ConstructionContextItem::ElidableConstructorKind: {
140
0
    llvm_unreachable("Materialization is necessary to put temporary into a "
141
0
                     "copy or move constructor!");
142
0
  }
143
1.19k
  case ConstructionContextItem::ArgumentKind: {
144
1.19k
    assert(ParentLayer->isLast());
145
1.19k
    const auto *E = cast<Expr>(ParentItem.getStmt());
146
1.19k
    assert(isa<CallExpr>(E) || isa<CXXConstructExpr>(E) ||
147
1.19k
           isa<ObjCMessageExpr>(E));
148
1.19k
    return create<ArgumentConstructionContext>(C, E, ParentItem.getIndex(),
149
1.19k
                                               BTE);
150
0
  }
151
12
  case ConstructionContextItem::InitializerKind: {
152
12
    assert(ParentLayer->isLast());
153
12
    const auto *I = ParentItem.getCXXCtorInitializer();
154
12
    assert(!I->getAnyMember()->getType().getCanonicalType()
155
12
             ->getAsCXXRecordDecl()->hasTrivialDestructor());
156
12
    return create<CXX17ElidedCopyConstructorInitializerConstructionContext>(
157
12
        C, I, BTE);
158
0
  }
159
0
  } // switch (ParentItem.getKind())
160
0
161
0
  llvm_unreachable("Unexpected construction context with destructor!");
162
0
}
163
164
const ConstructionContext *ConstructionContext::createFromLayers(
165
27.9k
    BumpVectorContext &C, const ConstructionContextLayer *TopLayer) {
166
27.9k
  // Before this point all we've had was a stockpile of arbitrary layers.
167
27.9k
  // Now validate that it is shaped as one of the finite amount of expected
168
27.9k
  // patterns.
169
27.9k
  const ConstructionContextItem &TopItem = TopLayer->getItem();
170
27.9k
  switch (TopItem.getKind()) {
171
11.5k
  case ConstructionContextItem::VariableKind: {
172
11.5k
    assert(TopLayer->isLast());
173
11.5k
    const auto *DS = cast<DeclStmt>(TopItem.getStmt());
174
11.5k
    return create<SimpleVariableConstructionContext>(C, DS);
175
0
  }
176
789
  case ConstructionContextItem::NewAllocatorKind: {
177
789
    assert(TopLayer->isLast());
178
789
    const auto *NE = cast<CXXNewExpr>(TopItem.getStmt());
179
789
    return create<NewAllocatedObjectConstructionContext>(C, NE);
180
0
  }
181
1.63k
  case ConstructionContextItem::ReturnKind: {
182
1.63k
    assert(TopLayer->isLast());
183
1.63k
    const auto *RS = cast<ReturnStmt>(TopItem.getStmt());
184
1.63k
    return create<SimpleReturnedValueConstructionContext>(C, RS);
185
0
  }
186
5.68k
  case ConstructionContextItem::MaterializationKind: {
187
5.68k
    const auto *MTE = cast<MaterializeTemporaryExpr>(TopItem.getStmt());
188
5.68k
    return createMaterializedTemporaryFromLayers(C, MTE, /*BTE=*/nullptr,
189
5.68k
                                                 TopLayer->getParent());
190
0
  }
191
4.00k
  case ConstructionContextItem::TemporaryDestructorKind: {
192
4.00k
    const auto *BTE = cast<CXXBindTemporaryExpr>(TopItem.getStmt());
193
4.00k
    assert(BTE->getType().getCanonicalType()->getAsCXXRecordDecl()
194
4.00k
              ->hasNonTrivialDestructor());
195
4.00k
    return createBoundTemporaryFromLayers(C, BTE, TopLayer->getParent());
196
0
  }
197
0
  case ConstructionContextItem::ElidedDestructorKind: {
198
0
    llvm_unreachable("Elided destructor items are not produced by the CFG!");
199
0
  }
200
0
  case ConstructionContextItem::ElidableConstructorKind: {
201
0
    llvm_unreachable("The argument needs to be materialized first!");
202
0
  }
203
2.30k
  case ConstructionContextItem::InitializerKind: {
204
2.30k
    assert(TopLayer->isLast());
205
2.30k
    const CXXCtorInitializer *I = TopItem.getCXXCtorInitializer();
206
2.30k
    return create<SimpleConstructorInitializerConstructionContext>(C, I);
207
0
  }
208
2.00k
  case ConstructionContextItem::ArgumentKind: {
209
2.00k
    assert(TopLayer->isLast());
210
2.00k
    const auto *E = cast<Expr>(TopItem.getStmt());
211
2.00k
    return create<ArgumentConstructionContext>(C, E, TopItem.getIndex(),
212
2.00k
                                               /*BTE=*/nullptr);
213
0
  }
214
0
  } // switch (TopItem.getKind())
215
0
  llvm_unreachable("Unexpected construction context!");
216
0
}