/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 | 413k | const ConstructionContextLayer *Parent) { |
25 | 413k | ConstructionContextLayer *CC = |
26 | 413k | C.getAllocator().Allocate<ConstructionContextLayer>(); |
27 | 413k | return new (CC) ConstructionContextLayer(Item, Parent); |
28 | 413k | } |
29 | | |
30 | | bool ConstructionContextLayer::isStrictlyMoreSpecificThan( |
31 | 11.8k | const ConstructionContextLayer *Other) const { |
32 | 11.8k | const ConstructionContextLayer *Self = this; |
33 | 27.5k | while (true) { |
34 | 27.5k | if (!Other) |
35 | 11.8k | return Self; |
36 | 15.6k | if (!Self || !(Self->Item == Other->Item)) |
37 | 0 | return false; |
38 | 15.6k | Self = Self->getParent(); |
39 | 15.6k | Other = Other->getParent(); |
40 | 15.6k | } |
41 | 0 | llvm_unreachable("The above loop can only be terminated via return!"); |
42 | 0 | } |
43 | | |
44 | | const ConstructionContext * |
45 | | ConstructionContext::createMaterializedTemporaryFromLayers( |
46 | | BumpVectorContext &C, const MaterializeTemporaryExpr *MTE, |
47 | | const CXXBindTemporaryExpr *BTE, |
48 | 11.9k | const ConstructionContextLayer *ParentLayer) { |
49 | 11.9k | assert(MTE); |
50 | | |
51 | | // If the object requires destruction and is not lifetime-extended, |
52 | | // then it must have a BTE within its MTE, otherwise it shouldn't. |
53 | | // FIXME: This should be an assertion. |
54 | 11.9k | if (!BTE && !(9.53k MTE->getType().getCanonicalType()->getAsCXXRecordDecl() |
55 | 9.53k | ->hasTrivialDestructor() || |
56 | 9.53k | MTE->getStorageDuration() != SD_FullExpression28 )) { |
57 | 4 | return nullptr; |
58 | 4 | } |
59 | | |
60 | | // If the temporary is lifetime-extended, don't save the BTE, |
61 | | // because we don't need a temporary destructor, but an automatic |
62 | | // destructor. |
63 | 11.9k | if (MTE->getStorageDuration() != SD_FullExpression) { |
64 | 582 | BTE = nullptr; |
65 | 582 | } |
66 | | |
67 | | // Handle pre-C++17 copy and move elision. |
68 | 11.9k | const CXXConstructExpr *ElidedCE = nullptr; |
69 | 11.9k | const ConstructionContext *ElidedCC = nullptr; |
70 | 11.9k | if (ParentLayer) { |
71 | 8.11k | const ConstructionContextItem &ElidedItem = ParentLayer->getItem(); |
72 | 8.11k | assert(ElidedItem.getKind() == |
73 | 8.11k | ConstructionContextItem::ElidableConstructorKind); |
74 | 0 | ElidedCE = cast<CXXConstructExpr>(ElidedItem.getStmt()); |
75 | 8.11k | assert(ElidedCE->isElidable()); |
76 | | // We're creating a construction context that might have already |
77 | | // been created elsewhere. Maybe we should unique our construction |
78 | | // contexts. That's what we often do, but in this case it's unlikely |
79 | | // to bring any benefits. |
80 | 0 | ElidedCC = createFromLayers(C, ParentLayer->getParent()); |
81 | 8.11k | if (!ElidedCC) { |
82 | | // We may fail to create the elided construction context. |
83 | | // In this case, skip copy elision entirely. |
84 | 0 | return create<SimpleTemporaryObjectConstructionContext>(C, BTE, MTE); |
85 | 0 | } |
86 | 8.11k | return create<ElidedTemporaryObjectConstructionContext>( |
87 | 8.11k | C, BTE, MTE, ElidedCE, ElidedCC); |
88 | 8.11k | } |
89 | | |
90 | | // This is a normal temporary. |
91 | 3.82k | assert(!ParentLayer); |
92 | 0 | return create<SimpleTemporaryObjectConstructionContext>(C, BTE, MTE); |
93 | 11.9k | } |
94 | | |
95 | | const ConstructionContext *ConstructionContext::createBoundTemporaryFromLayers( |
96 | | BumpVectorContext &C, const CXXBindTemporaryExpr *BTE, |
97 | 4.07k | const ConstructionContextLayer *ParentLayer) { |
98 | 4.07k | if (!ParentLayer) { |
99 | | // A temporary object that doesn't require materialization. |
100 | | // In particular, it shouldn't require copy elision, because |
101 | | // copy/move constructors take a reference, which requires |
102 | | // materialization to obtain the glvalue. |
103 | 294 | return create<SimpleTemporaryObjectConstructionContext>(C, BTE, |
104 | 294 | /*MTE=*/nullptr); |
105 | 294 | } |
106 | | |
107 | 3.77k | const ConstructionContextItem &ParentItem = ParentLayer->getItem(); |
108 | 3.77k | switch (ParentItem.getKind()) { |
109 | 78 | case ConstructionContextItem::VariableKind: { |
110 | 78 | const auto *DS = cast<DeclStmt>(ParentItem.getStmt()); |
111 | 78 | assert(!cast<VarDecl>(DS->getSingleDecl())->getType().getCanonicalType() |
112 | 78 | ->getAsCXXRecordDecl()->hasTrivialDestructor()); |
113 | 0 | 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 | 81 | case ConstructionContextItem::ReturnKind: { |
119 | 81 | assert(ParentLayer->isLast()); |
120 | 0 | const auto *RS = cast<ReturnStmt>(ParentItem.getStmt()); |
121 | 81 | assert(!RS->getRetValue()->getType().getCanonicalType() |
122 | 81 | ->getAsCXXRecordDecl()->hasTrivialDestructor()); |
123 | 0 | return create<CXX17ElidedCopyReturnedValueConstructionContext>(C, RS, |
124 | 81 | BTE); |
125 | 0 | } |
126 | | |
127 | 2.40k | case ConstructionContextItem::MaterializationKind: { |
128 | | // No assert. We may have an elidable copy on the grandparent layer. |
129 | 2.40k | const auto *MTE = cast<MaterializeTemporaryExpr>(ParentItem.getStmt()); |
130 | 2.40k | return createMaterializedTemporaryFromLayers(C, MTE, BTE, |
131 | 2.40k | 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.20k | case ConstructionContextItem::ArgumentKind: { |
144 | 1.20k | assert(ParentLayer->isLast()); |
145 | 0 | const auto *E = cast<Expr>(ParentItem.getStmt()); |
146 | 1.20k | assert(isa<CallExpr>(E) || isa<CXXConstructExpr>(E) || |
147 | 1.20k | isa<ObjCMessageExpr>(E)); |
148 | 0 | return create<ArgumentConstructionContext>(C, E, ParentItem.getIndex(), |
149 | 1.20k | BTE); |
150 | 0 | } |
151 | 12 | case ConstructionContextItem::InitializerKind: { |
152 | 12 | assert(ParentLayer->isLast()); |
153 | 0 | const auto *I = ParentItem.getCXXCtorInitializer(); |
154 | 12 | assert(!I->getAnyMember()->getType().getCanonicalType() |
155 | 12 | ->getAsCXXRecordDecl()->hasTrivialDestructor()); |
156 | 0 | return create<CXX17ElidedCopyConstructorInitializerConstructionContext>( |
157 | 12 | C, I, BTE); |
158 | 0 | } |
159 | 3.77k | } // switch (ParentItem.getKind()) |
160 | | |
161 | 0 | llvm_unreachable("Unexpected construction context with destructor!"); |
162 | 0 | } |
163 | | |
164 | | const ConstructionContext *ConstructionContext::createFromLayers( |
165 | 40.6k | BumpVectorContext &C, const ConstructionContextLayer *TopLayer) { |
166 | | // Before this point all we've had was a stockpile of arbitrary layers. |
167 | | // Now validate that it is shaped as one of the finite amount of expected |
168 | | // patterns. |
169 | 40.6k | const ConstructionContextItem &TopItem = TopLayer->getItem(); |
170 | 40.6k | switch (TopItem.getKind()) { |
171 | 17.3k | case ConstructionContextItem::VariableKind: { |
172 | 17.3k | assert(TopLayer->isLast()); |
173 | 0 | const auto *DS = cast<DeclStmt>(TopItem.getStmt()); |
174 | 17.3k | return create<SimpleVariableConstructionContext>(C, DS); |
175 | 0 | } |
176 | 901 | case ConstructionContextItem::NewAllocatorKind: { |
177 | 901 | assert(TopLayer->isLast()); |
178 | 0 | const auto *NE = cast<CXXNewExpr>(TopItem.getStmt()); |
179 | 901 | return create<NewAllocatedObjectConstructionContext>(C, NE); |
180 | 0 | } |
181 | 3.46k | case ConstructionContextItem::ReturnKind: { |
182 | 3.46k | assert(TopLayer->isLast()); |
183 | 0 | const auto *RS = cast<ReturnStmt>(TopItem.getStmt()); |
184 | 3.46k | return create<SimpleReturnedValueConstructionContext>(C, RS); |
185 | 0 | } |
186 | 9.53k | case ConstructionContextItem::MaterializationKind: { |
187 | 9.53k | const auto *MTE = cast<MaterializeTemporaryExpr>(TopItem.getStmt()); |
188 | 9.53k | return createMaterializedTemporaryFromLayers(C, MTE, /*BTE=*/nullptr, |
189 | 9.53k | TopLayer->getParent()); |
190 | 0 | } |
191 | 4.07k | case ConstructionContextItem::TemporaryDestructorKind: { |
192 | 4.07k | const auto *BTE = cast<CXXBindTemporaryExpr>(TopItem.getStmt()); |
193 | 4.07k | assert(BTE->getType().getCanonicalType()->getAsCXXRecordDecl() |
194 | 4.07k | ->hasNonTrivialDestructor()); |
195 | 0 | 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.36k | case ConstructionContextItem::InitializerKind: { |
204 | 2.36k | assert(TopLayer->isLast()); |
205 | 0 | const CXXCtorInitializer *I = TopItem.getCXXCtorInitializer(); |
206 | 2.36k | return create<SimpleConstructorInitializerConstructionContext>(C, I); |
207 | 0 | } |
208 | 2.97k | case ConstructionContextItem::ArgumentKind: { |
209 | 2.97k | assert(TopLayer->isLast()); |
210 | 0 | const auto *E = cast<Expr>(TopItem.getStmt()); |
211 | 2.97k | return create<ArgumentConstructionContext>(C, E, TopItem.getIndex(), |
212 | 2.97k | /*BTE=*/nullptr); |
213 | 0 | } |
214 | 40.6k | } // switch (TopItem.getKind()) |
215 | 0 | llvm_unreachable("Unexpected construction context!"); |
216 | 0 | } |