Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Analysis/ConstructionContext.h
Line
Count
Source (jump to first uncovered line)
1
//===- ConstructionContext.h - CFG constructor information ------*- 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 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
#ifndef LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H
17
#define LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H
18
19
#include "clang/Analysis/Support/BumpVector.h"
20
#include "clang/AST/ExprCXX.h"
21
#include "clang/AST/ExprObjC.h"
22
23
namespace clang {
24
25
/// Represents a single point (AST node) in the program that requires attention
26
/// during construction of an object. ConstructionContext would be represented
27
/// as a list of such items.
28
class ConstructionContextItem {
29
public:
30
  enum ItemKind {
31
    VariableKind,
32
    NewAllocatorKind,
33
    ReturnKind,
34
    MaterializationKind,
35
    TemporaryDestructorKind,
36
    ElidedDestructorKind,
37
    ElidableConstructorKind,
38
    ArgumentKind,
39
    STATEMENT_WITH_INDEX_KIND_BEGIN=ArgumentKind,
40
    STATEMENT_WITH_INDEX_KIND_END=ArgumentKind,
41
    STATEMENT_KIND_BEGIN = VariableKind,
42
    STATEMENT_KIND_END = ArgumentKind,
43
    InitializerKind,
44
    INITIALIZER_KIND_BEGIN=InitializerKind,
45
    INITIALIZER_KIND_END=InitializerKind
46
  };
47
48
40
  LLVM_DUMP_METHOD static StringRef getKindAsString(ItemKind K) {
49
40
    switch (K) {
50
21
      case VariableKind:            return "construct into local variable";
51
8
      case NewAllocatorKind:        return "construct into new-allocator";
52
0
      case ReturnKind:              return "construct into return address";
53
2
      case MaterializationKind:     return "materialize temporary";
54
0
      case TemporaryDestructorKind: return "destroy temporary";
55
0
      case ElidedDestructorKind:    return "elide destructor";
56
4
      case ElidableConstructorKind: return "elide constructor";
57
0
      case ArgumentKind:            return "construct into argument";
58
5
      case InitializerKind:         return "construct into member variable";
59
40
    };
60
0
    llvm_unreachable("Unknown ItemKind");
61
0
  }
62
63
private:
64
  const void *const Data;
65
  const ItemKind Kind;
66
  const unsigned Index = 0;
67
68
194k
  bool hasStatement() const {
69
194k
    return Kind >= STATEMENT_KIND_BEGIN &&
70
194k
           Kind <= STATEMENT_KIND_END;
71
194k
  }
72
73
4.17k
  bool hasIndex() const {
74
4.17k
    return Kind >= STATEMENT_WITH_INDEX_KIND_BEGIN &&
75
4.17k
           Kind >= STATEMENT_WITH_INDEX_KIND_END;
76
4.17k
  }
77
78
5.03k
  bool hasInitializer() const {
79
5.03k
    return Kind >= INITIALIZER_KIND_BEGIN &&
80
5.03k
           Kind <= INITIALIZER_KIND_END;
81
5.03k
  }
82
83
public:
84
  // ConstructionContextItem should be simple enough so that it was easy to
85
  // re-construct it from the AST node it captures. For that reason we provide
86
  // simple implicit conversions from all sorts of supported AST nodes.
87
  ConstructionContextItem(const DeclStmt *DS)
88
236k
      : Data(DS), Kind(VariableKind) {}
89
90
  ConstructionContextItem(const CXXNewExpr *NE)
91
6.95k
      : Data(NE), Kind(NewAllocatorKind) {}
92
93
  ConstructionContextItem(const ReturnStmt *RS)
94
163k
      : Data(RS), Kind(ReturnKind) {}
95
96
  ConstructionContextItem(const MaterializeTemporaryExpr *MTE)
97
54.6k
      : Data(MTE), Kind(MaterializationKind) {}
98
99
  ConstructionContextItem(const CXXBindTemporaryExpr *BTE,
100
                          bool IsElided = false)
101
      : Data(BTE),
102
16.5k
        Kind(IsElided ? ElidedDestructorKind : TemporaryDestructorKind) {}
103
104
  ConstructionContextItem(const CXXConstructExpr *CE)
105
52.9k
      : Data(CE), Kind(ElidableConstructorKind) {}
106
107
  ConstructionContextItem(const CallExpr *CE, unsigned Index)
108
3.50k
      : Data(CE), Kind(ArgumentKind), Index(Index) {}
109
110
  ConstructionContextItem(const CXXConstructExpr *CE, unsigned Index)
111
892
      : Data(CE), Kind(ArgumentKind), Index(Index) {}
112
113
  ConstructionContextItem(const CXXInheritedCtorInitExpr *CE, unsigned Index)
114
0
      : Data(CE), Kind(ArgumentKind), Index(Index) {}
115
116
  ConstructionContextItem(const ObjCMessageExpr *ME, unsigned Index)
117
29
      : Data(ME), Kind(ArgumentKind), Index(Index) {}
118
119
  // A polymorphic version of the previous calls with dynamic type check.
120
  ConstructionContextItem(const Expr *E, unsigned Index)
121
180k
      : Data(E), Kind(ArgumentKind), Index(Index) {
122
180k
    assert(isa<CallExpr>(E) || isa<CXXConstructExpr>(E) ||
123
180k
           isa<CXXDeleteExpr>(E) || isa<CXXInheritedCtorInitExpr>(E) ||
124
180k
           isa<ObjCMessageExpr>(E));
125
180k
  }
126
127
  ConstructionContextItem(const CXXCtorInitializer *Init)
128
31.6k
      : Data(Init), Kind(InitializerKind), Index(0) {}
129
130
63.1k
  ItemKind getKind() const { return Kind; }
131
132
40
  LLVM_DUMP_METHOD StringRef getKindAsString() const {
133
40
    return getKindAsString(getKind());
134
40
  }
135
136
  /// The construction site - the statement that triggered the construction
137
  /// for one of its parts. For instance, stack variable declaration statement
138
  /// triggers construction of itself or its elements if it's an array,
139
  /// new-expression triggers construction of the newly allocated object(s).
140
119k
  const Stmt *getStmt() const {
141
119k
    assert(hasStatement());
142
0
    return static_cast<const Stmt *>(Data);
143
119k
  }
144
145
74.6k
  const Stmt *getStmtOrNull() const {
146
74.6k
    return hasStatement() ? 
getStmt()69.3k
:
nullptr5.31k
;
147
74.6k
  }
148
149
  /// The construction site is not necessarily a statement. It may also be a
150
  /// CXXCtorInitializer, which means that a member variable is being
151
  /// constructed during initialization of the object that contains it.
152
5.03k
  const CXXCtorInitializer *getCXXCtorInitializer() const {
153
5.03k
    assert(hasInitializer());
154
0
    return static_cast<const CXXCtorInitializer *>(Data);
155
5.03k
  }
156
157
  /// If a single trigger statement triggers multiple constructors, they are
158
  /// usually being enumerated. This covers function argument constructors
159
  /// triggered by a call-expression and items in an initializer list triggered
160
  /// by an init-list-expression.
161
4.17k
  unsigned getIndex() const {
162
    // This is a fairly specific request. Let's make sure the user knows
163
    // what he's doing.
164
4.17k
    assert(hasIndex());
165
0
    return Index;
166
4.17k
  }
167
168
105k
  void Profile(llvm::FoldingSetNodeID &ID) const {
169
105k
    ID.AddPointer(Data);
170
105k
    ID.AddInteger(Kind);
171
105k
    ID.AddInteger(Index);
172
105k
  }
173
174
430k
  bool operator==(const ConstructionContextItem &Other) const {
175
    // For most kinds the Index comparison is trivially true, but
176
    // checking kind separately doesn't seem to be less expensive
177
    // than checking Index. Same in operator<().
178
430k
    return std::make_tuple(Data, Kind, Index) ==
179
430k
           std::make_tuple(Other.Data, Other.Kind, Other.Index);
180
430k
  }
181
182
292k
  bool operator<(const ConstructionContextItem &Other) const {
183
292k
    return std::make_tuple(Data, Kind, Index) <
184
292k
           std::make_tuple(Other.Data, Other.Kind, Other.Index);
185
292k
  }
186
};
187
188
/// Construction context can be seen as a linked list of multiple layers.
189
/// Sometimes a single trigger is not enough to describe the construction
190
/// site. That's what causing us to have a chain of "partial" construction
191
/// context layers. Some examples:
192
/// - A constructor within in an aggregate initializer list within a variable
193
///   would have a construction context of the initializer list with
194
///   the parent construction context of a variable.
195
/// - A constructor for a temporary that needs to be both destroyed
196
///   and materialized into an elidable copy constructor would have a
197
///   construction context of a CXXBindTemporaryExpr with the parent
198
///   construction context of a MaterializeTemproraryExpr.
199
/// Not all of these are currently supported.
200
/// Layers are created gradually while traversing the AST, and layers that
201
/// represent the outmost AST nodes are built first, while the node that
202
/// immediately contains the constructor would be built last and capture the
203
/// previous layers as its parents. Construction context captures the last layer
204
/// (which has links to the previous layers) and classifies the seemingly
205
/// arbitrary chain of layers into one of the possible ways of constructing
206
/// an object in C++ for user-friendly experience.
207
class ConstructionContextLayer {
208
  const ConstructionContextLayer *Parent = nullptr;
209
  ConstructionContextItem Item;
210
211
  ConstructionContextLayer(ConstructionContextItem Item,
212
                           const ConstructionContextLayer *Parent)
213
413k
      : Parent(Parent), Item(Item) {}
214
215
public:
216
  static const ConstructionContextLayer *
217
  create(BumpVectorContext &C, const ConstructionContextItem &Item,
218
         const ConstructionContextLayer *Parent = nullptr);
219
220
62.4k
  const ConstructionContextItem &getItem() const { return Item; }
221
55.4k
  const ConstructionContextLayer *getParent() const { return Parent; }
222
28.3k
  bool isLast() const { return !Parent; }
223
224
  /// See if Other is a proper initial segment of this construction context
225
  /// in terms of the parent chain - i.e. a few first parents coincide and
226
  /// then the other context terminates but our context goes further - i.e.,
227
  /// we are providing the same context that the other context provides,
228
  /// and a bit more above that.
229
  bool isStrictlyMoreSpecificThan(const ConstructionContextLayer *Other) const;
230
};
231
232
233
/// ConstructionContext's subclasses describe different ways of constructing
234
/// an object in C++. The context re-captures the essential parent AST nodes
235
/// of the CXXConstructExpr it is assigned to and presents these nodes
236
/// through easy-to-understand accessor methods.
237
class ConstructionContext {
238
public:
239
  enum Kind {
240
    SimpleVariableKind,
241
    CXX17ElidedCopyVariableKind,
242
    VARIABLE_BEGIN = SimpleVariableKind,
243
    VARIABLE_END = CXX17ElidedCopyVariableKind,
244
    SimpleConstructorInitializerKind,
245
    CXX17ElidedCopyConstructorInitializerKind,
246
    INITIALIZER_BEGIN = SimpleConstructorInitializerKind,
247
    INITIALIZER_END = CXX17ElidedCopyConstructorInitializerKind,
248
    NewAllocatedObjectKind,
249
    SimpleTemporaryObjectKind,
250
    ElidedTemporaryObjectKind,
251
    TEMPORARY_BEGIN = SimpleTemporaryObjectKind,
252
    TEMPORARY_END = ElidedTemporaryObjectKind,
253
    SimpleReturnedValueKind,
254
    CXX17ElidedCopyReturnedValueKind,
255
    RETURNED_VALUE_BEGIN = SimpleReturnedValueKind,
256
    RETURNED_VALUE_END = CXX17ElidedCopyReturnedValueKind,
257
    ArgumentKind
258
  };
259
260
protected:
261
  Kind K;
262
263
  // Do not make public! These need to only be constructed
264
  // via createFromLayers().
265
40.6k
  explicit ConstructionContext(Kind K) : K(K) {}
266
267
private:
268
  // A helper function for constructing an instance into a bump vector context.
269
  template <typename T, typename... ArgTypes>
270
40.6k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
40.6k
    auto *CC = C.getAllocator().Allocate<T>();
272
40.6k
    return new (CC) T(Args...);
273
40.6k
  }
clang::SimpleTemporaryObjectConstructionContext* clang::ConstructionContext::create<clang::SimpleTemporaryObjectConstructionContext, clang::CXXBindTemporaryExpr const*, clang::MaterializeTemporaryExpr const*>(clang::BumpVectorContext&, clang::CXXBindTemporaryExpr const*, clang::MaterializeTemporaryExpr const*)
Line
Count
Source
270
3.82k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
3.82k
    auto *CC = C.getAllocator().Allocate<T>();
272
3.82k
    return new (CC) T(Args...);
273
3.82k
  }
clang::ElidedTemporaryObjectConstructionContext* clang::ConstructionContext::create<clang::ElidedTemporaryObjectConstructionContext, clang::CXXBindTemporaryExpr const*, clang::MaterializeTemporaryExpr const*, clang::CXXConstructExpr const*, clang::ConstructionContext const*>(clang::BumpVectorContext&, clang::CXXBindTemporaryExpr const*, clang::MaterializeTemporaryExpr const*, clang::CXXConstructExpr const*, clang::ConstructionContext const*)
Line
Count
Source
270
8.11k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
8.11k
    auto *CC = C.getAllocator().Allocate<T>();
272
8.11k
    return new (CC) T(Args...);
273
8.11k
  }
clang::SimpleTemporaryObjectConstructionContext* clang::ConstructionContext::create<clang::SimpleTemporaryObjectConstructionContext, clang::CXXBindTemporaryExpr const*, std::nullptr_t>(clang::BumpVectorContext&, clang::CXXBindTemporaryExpr const*, std::nullptr_t)
Line
Count
Source
270
294
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
294
    auto *CC = C.getAllocator().Allocate<T>();
272
294
    return new (CC) T(Args...);
273
294
  }
clang::CXX17ElidedCopyVariableConstructionContext* clang::ConstructionContext::create<clang::CXX17ElidedCopyVariableConstructionContext, clang::DeclStmt const*, clang::CXXBindTemporaryExpr const*>(clang::BumpVectorContext&, clang::DeclStmt const*, clang::CXXBindTemporaryExpr const*)
Line
Count
Source
270
78
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
78
    auto *CC = C.getAllocator().Allocate<T>();
272
78
    return new (CC) T(Args...);
273
78
  }
clang::CXX17ElidedCopyReturnedValueConstructionContext* clang::ConstructionContext::create<clang::CXX17ElidedCopyReturnedValueConstructionContext, clang::ReturnStmt const*, clang::CXXBindTemporaryExpr const*>(clang::BumpVectorContext&, clang::ReturnStmt const*, clang::CXXBindTemporaryExpr const*)
Line
Count
Source
270
81
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
81
    auto *CC = C.getAllocator().Allocate<T>();
272
81
    return new (CC) T(Args...);
273
81
  }
clang::ArgumentConstructionContext* clang::ConstructionContext::create<clang::ArgumentConstructionContext, clang::Expr const*, unsigned int, clang::CXXBindTemporaryExpr const*>(clang::BumpVectorContext&, clang::Expr const*, unsigned int, clang::CXXBindTemporaryExpr const*)
Line
Count
Source
270
1.20k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
1.20k
    auto *CC = C.getAllocator().Allocate<T>();
272
1.20k
    return new (CC) T(Args...);
273
1.20k
  }
clang::CXX17ElidedCopyConstructorInitializerConstructionContext* clang::ConstructionContext::create<clang::CXX17ElidedCopyConstructorInitializerConstructionContext, clang::CXXCtorInitializer const*, clang::CXXBindTemporaryExpr const*>(clang::BumpVectorContext&, clang::CXXCtorInitializer const*, clang::CXXBindTemporaryExpr const*)
Line
Count
Source
270
12
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
12
    auto *CC = C.getAllocator().Allocate<T>();
272
12
    return new (CC) T(Args...);
273
12
  }
clang::SimpleVariableConstructionContext* clang::ConstructionContext::create<clang::SimpleVariableConstructionContext, clang::DeclStmt const*>(clang::BumpVectorContext&, clang::DeclStmt const*)
Line
Count
Source
270
17.3k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
17.3k
    auto *CC = C.getAllocator().Allocate<T>();
272
17.3k
    return new (CC) T(Args...);
273
17.3k
  }
clang::NewAllocatedObjectConstructionContext* clang::ConstructionContext::create<clang::NewAllocatedObjectConstructionContext, clang::CXXNewExpr const*>(clang::BumpVectorContext&, clang::CXXNewExpr const*)
Line
Count
Source
270
901
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
901
    auto *CC = C.getAllocator().Allocate<T>();
272
901
    return new (CC) T(Args...);
273
901
  }
clang::SimpleReturnedValueConstructionContext* clang::ConstructionContext::create<clang::SimpleReturnedValueConstructionContext, clang::ReturnStmt const*>(clang::BumpVectorContext&, clang::ReturnStmt const*)
Line
Count
Source
270
3.46k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
3.46k
    auto *CC = C.getAllocator().Allocate<T>();
272
3.46k
    return new (CC) T(Args...);
273
3.46k
  }
clang::SimpleConstructorInitializerConstructionContext* clang::ConstructionContext::create<clang::SimpleConstructorInitializerConstructionContext, clang::CXXCtorInitializer const*>(clang::BumpVectorContext&, clang::CXXCtorInitializer const*)
Line
Count
Source
270
2.36k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
2.36k
    auto *CC = C.getAllocator().Allocate<T>();
272
2.36k
    return new (CC) T(Args...);
273
2.36k
  }
clang::ArgumentConstructionContext* clang::ConstructionContext::create<clang::ArgumentConstructionContext, clang::Expr const*, unsigned int, std::nullptr_t>(clang::BumpVectorContext&, clang::Expr const*, unsigned int, std::nullptr_t)
Line
Count
Source
270
2.97k
  static T *create(BumpVectorContext &C, ArgTypes... Args) {
271
2.97k
    auto *CC = C.getAllocator().Allocate<T>();
272
2.97k
    return new (CC) T(Args...);
273
2.97k
  }
274
275
  // A sub-routine of createFromLayers() that deals with temporary objects
276
  // that need to be materialized. The BTE argument is for the situation when
277
  // the object also needs to be bound for destruction.
278
  static const ConstructionContext *createMaterializedTemporaryFromLayers(
279
      BumpVectorContext &C, const MaterializeTemporaryExpr *MTE,
280
      const CXXBindTemporaryExpr *BTE,
281
      const ConstructionContextLayer *ParentLayer);
282
283
  // A sub-routine of createFromLayers() that deals with temporary objects
284
  // that need to be bound for destruction. Automatically finds out if the
285
  // object also needs to be materialized and delegates to
286
  // createMaterializedTemporaryFromLayers() if necessary.
287
  static const ConstructionContext *
288
  createBoundTemporaryFromLayers(
289
      BumpVectorContext &C, const CXXBindTemporaryExpr *BTE,
290
      const ConstructionContextLayer *ParentLayer);
291
292
public:
293
  /// Consume the construction context layer, together with its parent layers,
294
  /// and wrap it up into a complete construction context. May return null
295
  /// if layers do not form any supported construction context.
296
  static const ConstructionContext *
297
  createFromLayers(BumpVectorContext &C,
298
                   const ConstructionContextLayer *TopLayer);
299
300
229k
  Kind getKind() const { return K; }
301
};
302
303
/// An abstract base class for local variable constructors.
304
class VariableConstructionContext : public ConstructionContext {
305
  const DeclStmt *DS;
306
307
protected:
308
  VariableConstructionContext(ConstructionContext::Kind K, const DeclStmt *DS)
309
17.4k
      : ConstructionContext(K), DS(DS) {
310
17.4k
    assert(classof(this));
311
0
    assert(DS);
312
17.4k
  }
313
314
public:
315
15.7k
  const DeclStmt *getDeclStmt() const { return DS; }
316
317
33.4k
  static bool classof(const ConstructionContext *CC) {
318
33.4k
    return CC->getKind() >= VARIABLE_BEGIN &&
319
33.4k
           CC->getKind() <= VARIABLE_END;
320
33.4k
  }
321
};
322
323
/// Represents construction into a simple local variable, eg. T var(123);.
324
/// If a variable has an initializer, eg. T var = makeT();, then the final
325
/// elidable copy-constructor from makeT() into var would also be a simple
326
/// variable constructor handled by this class.
327
class SimpleVariableConstructionContext : public VariableConstructionContext {
328
  friend class ConstructionContext; // Allows to create<>() itself.
329
330
  explicit SimpleVariableConstructionContext(const DeclStmt *DS)
331
      : VariableConstructionContext(ConstructionContext::SimpleVariableKind,
332
17.3k
                                    DS) {}
333
334
public:
335
287
  static bool classof(const ConstructionContext *CC) {
336
287
    return CC->getKind() == SimpleVariableKind;
337
287
  }
338
};
339
340
/// Represents construction into a simple variable with an initializer syntax,
341
/// with a single constructor, eg. T var = makeT();. Such construction context
342
/// may only appear in C++17 because previously it was split into a temporary
343
/// object constructor and an elidable simple variable copy-constructor and
344
/// we were producing separate construction contexts for these constructors.
345
/// In C++17 we have a single construction context that combines both.
346
/// Note that if the object has trivial destructor, then this code is
347
/// indistinguishable from a simple variable constructor on the AST level;
348
/// in this case we provide a simple variable construction context.
349
class CXX17ElidedCopyVariableConstructionContext
350
    : public VariableConstructionContext {
351
  const CXXBindTemporaryExpr *BTE;
352
353
  friend class ConstructionContext; // Allows to create<>() itself.
354
355
  explicit CXX17ElidedCopyVariableConstructionContext(
356
      const DeclStmt *DS, const CXXBindTemporaryExpr *BTE)
357
78
      : VariableConstructionContext(CXX17ElidedCopyVariableKind, DS), BTE(BTE) {
358
78
    assert(BTE);
359
78
  }
360
361
public:
362
7
  const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; }
363
364
7
  static bool classof(const ConstructionContext *CC) {
365
7
    return CC->getKind() == CXX17ElidedCopyVariableKind;
366
7
  }
367
};
368
369
// An abstract base class for constructor-initializer-based constructors.
370
class ConstructorInitializerConstructionContext : public ConstructionContext {
371
  const CXXCtorInitializer *I;
372
373
protected:
374
  explicit ConstructorInitializerConstructionContext(
375
      ConstructionContext::Kind K, const CXXCtorInitializer *I)
376
2.37k
      : ConstructionContext(K), I(I) {
377
2.37k
    assert(classof(this));
378
0
    assert(I);
379
2.37k
  }
380
381
public:
382
5.34k
  const CXXCtorInitializer *getCXXCtorInitializer() const { return I; }
383
384
7.72k
  static bool classof(const ConstructionContext *CC) {
385
7.72k
    return CC->getKind() >= INITIALIZER_BEGIN &&
386
7.72k
           CC->getKind() <= INITIALIZER_END;
387
7.72k
  }
388
};
389
390
/// Represents construction into a field or a base class within a bigger object
391
/// via a constructor initializer, eg. T(): field(123) { ... }.
392
class SimpleConstructorInitializerConstructionContext
393
    : public ConstructorInitializerConstructionContext {
394
  friend class ConstructionContext; // Allows to create<>() itself.
395
396
  explicit SimpleConstructorInitializerConstructionContext(
397
      const CXXCtorInitializer *I)
398
      : ConstructorInitializerConstructionContext(
399
2.36k
            ConstructionContext::SimpleConstructorInitializerKind, I) {}
400
401
public:
402
33
  static bool classof(const ConstructionContext *CC) {
403
33
    return CC->getKind() == SimpleConstructorInitializerKind;
404
33
  }
405
};
406
407
/// Represents construction into a field or a base class within a bigger object
408
/// via a constructor initializer, with a single constructor, eg.
409
/// T(): field(Field(123)) { ... }. Such construction context may only appear
410
/// in C++17 because previously it was split into a temporary object constructor
411
/// and an elidable simple constructor-initializer copy-constructor and we were
412
/// producing separate construction contexts for these constructors. In C++17
413
/// we have a single construction context that combines both. Note that if the
414
/// object has trivial destructor, then this code is indistinguishable from
415
/// a simple constructor-initializer constructor on the AST level; in this case
416
/// we provide a simple constructor-initializer construction context.
417
class CXX17ElidedCopyConstructorInitializerConstructionContext
418
    : public ConstructorInitializerConstructionContext {
419
  const CXXBindTemporaryExpr *BTE;
420
421
  friend class ConstructionContext; // Allows to create<>() itself.
422
423
  explicit CXX17ElidedCopyConstructorInitializerConstructionContext(
424
      const CXXCtorInitializer *I, const CXXBindTemporaryExpr *BTE)
425
      : ConstructorInitializerConstructionContext(
426
            CXX17ElidedCopyConstructorInitializerKind, I),
427
12
        BTE(BTE) {
428
12
    assert(BTE);
429
12
  }
430
431
public:
432
2
  const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; }
433
434
2
  static bool classof(const ConstructionContext *CC) {
435
2
    return CC->getKind() == CXX17ElidedCopyConstructorInitializerKind;
436
2
  }
437
};
438
439
/// Represents immediate initialization of memory allocated by operator new,
440
/// eg. new T(123);.
441
class NewAllocatedObjectConstructionContext : public ConstructionContext {
442
  const CXXNewExpr *NE;
443
444
  friend class ConstructionContext; // Allows to create<>() itself.
445
446
  explicit NewAllocatedObjectConstructionContext(const CXXNewExpr *NE)
447
      : ConstructionContext(ConstructionContext::NewAllocatedObjectKind),
448
901
        NE(NE) {
449
901
    assert(NE);
450
901
  }
451
452
public:
453
626
  const CXXNewExpr *getCXXNewExpr() const { return NE; }
454
455
9.75k
  static bool classof(const ConstructionContext *CC) {
456
9.75k
    return CC->getKind() == NewAllocatedObjectKind;
457
9.75k
  }
458
};
459
460
/// Represents a temporary object, eg. T(123), that does not immediately cross
461
/// function boundaries "by value"; constructors that construct function
462
/// value-type arguments or values that are immediately returned from the
463
/// function that returns a value receive separate construction context kinds.
464
class TemporaryObjectConstructionContext : public ConstructionContext {
465
  const CXXBindTemporaryExpr *BTE;
466
  const MaterializeTemporaryExpr *MTE;
467
468
protected:
469
  explicit TemporaryObjectConstructionContext(
470
      ConstructionContext::Kind K, const CXXBindTemporaryExpr *BTE,
471
      const MaterializeTemporaryExpr *MTE)
472
12.2k
      : ConstructionContext(K), BTE(BTE), MTE(MTE) {
473
    // Both BTE and MTE can be null here, all combinations possible.
474
    // Even though for now at least one should be non-null, we simply haven't
475
    // implemented the other case yet (this would be a temporary in the middle
476
    // of nowhere that doesn't have a non-trivial destructor).
477
12.2k
  }
478
479
public:
480
  /// CXXBindTemporaryExpr here is non-null as long as the temporary has
481
  /// a non-trivial destructor.
482
9.14k
  const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const {
483
9.14k
    return BTE;
484
9.14k
  }
485
486
  /// MaterializeTemporaryExpr is non-null as long as the temporary is actually
487
  /// used after construction, eg. by binding to a reference (lifetime
488
  /// extension), accessing a field, calling a method, or passing it into
489
  /// a function (an elidable copy or move constructor would be a common
490
  /// example) by reference.
491
11.2k
  const MaterializeTemporaryExpr *getMaterializedTemporaryExpr() const {
492
11.2k
    return MTE;
493
11.2k
  }
494
495
12.2k
  static bool classof(const ConstructionContext *CC) {
496
12.2k
    return CC->getKind() >= TEMPORARY_BEGIN && 
CC->getKind() <= TEMPORARY_END11.6k
;
497
12.2k
  }
498
};
499
500
/// Represents a temporary object that is not constructed for the purpose of
501
/// being immediately copied/moved by an elidable copy/move-constructor.
502
/// This includes temporary objects "in the middle of nowhere" like T(123) and
503
/// lifetime-extended temporaries.
504
class SimpleTemporaryObjectConstructionContext
505
    : public TemporaryObjectConstructionContext {
506
  friend class ConstructionContext; // Allows to create<>() itself.
507
508
  explicit SimpleTemporaryObjectConstructionContext(
509
      const CXXBindTemporaryExpr *BTE, const MaterializeTemporaryExpr *MTE)
510
      : TemporaryObjectConstructionContext(
511
4.12k
            ConstructionContext::SimpleTemporaryObjectKind, BTE, MTE) {}
512
513
public:
514
326
  static bool classof(const ConstructionContext *CC) {
515
326
    return CC->getKind() == SimpleTemporaryObjectKind;
516
326
  }
517
};
518
519
/// Represents a temporary object that is constructed for the sole purpose
520
/// of being immediately copied by an elidable copy/move constructor.
521
/// For example, T t = T(123); includes a temporary T(123) that is immediately
522
/// copied to variable t. In such cases the elidable copy can (but not
523
/// necessarily should) be omitted ("elided") accodring to the rules of the
524
/// language; the constructor would then construct variable t directly.
525
/// This construction context contains information of the elidable constructor
526
/// and its respective construction context.
527
class ElidedTemporaryObjectConstructionContext
528
    : public TemporaryObjectConstructionContext {
529
  const CXXConstructExpr *ElidedCE;
530
  const ConstructionContext *ElidedCC;
531
532
  friend class ConstructionContext; // Allows to create<>() itself.
533
534
  explicit ElidedTemporaryObjectConstructionContext(
535
      const CXXBindTemporaryExpr *BTE, const MaterializeTemporaryExpr *MTE,
536
      const CXXConstructExpr *ElidedCE, const ConstructionContext *ElidedCC)
537
      : TemporaryObjectConstructionContext(
538
            ConstructionContext::ElidedTemporaryObjectKind, BTE, MTE),
539
8.11k
        ElidedCE(ElidedCE), ElidedCC(ElidedCC) {
540
    // Elided constructor and its context should be either both specified
541
    // or both unspecified. In the former case, the constructor must be
542
    // elidable.
543
8.11k
    assert(ElidedCE && ElidedCE->isElidable() && ElidedCC);
544
8.11k
  }
545
546
public:
547
20.1k
  const CXXConstructExpr *getConstructorAfterElision() const {
548
20.1k
    return ElidedCE;
549
20.1k
  }
550
551
13.3k
  const ConstructionContext *getConstructionContextAfterElision() const {
552
13.3k
    return ElidedCC;
553
13.3k
  }
554
555
13.4k
  static bool classof(const ConstructionContext *CC) {
556
13.4k
    return CC->getKind() == ElidedTemporaryObjectKind;
557
13.4k
  }
558
};
559
560
class ReturnedValueConstructionContext : public ConstructionContext {
561
  const ReturnStmt *RS;
562
563
protected:
564
  explicit ReturnedValueConstructionContext(ConstructionContext::Kind K,
565
                                            const ReturnStmt *RS)
566
3.54k
      : ConstructionContext(K), RS(RS) {
567
3.54k
    assert(classof(this));
568
0
    assert(RS);
569
3.54k
  }
570
571
public:
572
102
  const ReturnStmt *getReturnStmt() const { return RS; }
573
574
4.25k
  static bool classof(const ConstructionContext *CC) {
575
4.25k
    return CC->getKind() >= RETURNED_VALUE_BEGIN &&
576
4.25k
           
CC->getKind() <= RETURNED_VALUE_END3.68k
;
577
4.25k
  }
578
};
579
580
/// Represents a temporary object that is being immediately returned from a
581
/// function by value, eg. return t; or return T(123);. In this case there is
582
/// always going to be a constructor at the return site. However, the usual
583
/// temporary-related bureaucracy (CXXBindTemporaryExpr,
584
/// MaterializeTemporaryExpr) is normally located in the caller function's AST.
585
class SimpleReturnedValueConstructionContext
586
    : public ReturnedValueConstructionContext {
587
  friend class ConstructionContext; // Allows to create<>() itself.
588
589
  explicit SimpleReturnedValueConstructionContext(const ReturnStmt *RS)
590
      : ReturnedValueConstructionContext(
591
3.46k
            ConstructionContext::SimpleReturnedValueKind, RS) {}
592
593
public:
594
42
  static bool classof(const ConstructionContext *CC) {
595
42
    return CC->getKind() == SimpleReturnedValueKind;
596
42
  }
597
};
598
599
/// Represents a temporary object that is being immediately returned from a
600
/// function by value, eg. return t; or return T(123); in C++17.
601
/// In C++17 there is not going to be an elidable copy constructor at the
602
/// return site.  However, the usual temporary-related bureaucracy (CXXBindTemporaryExpr,
603
/// MaterializeTemporaryExpr) is normally located in the caller function's AST.
604
/// Note that if the object has trivial destructor, then this code is
605
/// indistinguishable from a simple returned value constructor on the AST level;
606
/// in this case we provide a simple returned value construction context.
607
class CXX17ElidedCopyReturnedValueConstructionContext
608
    : public ReturnedValueConstructionContext {
609
  const CXXBindTemporaryExpr *BTE;
610
611
  friend class ConstructionContext; // Allows to create<>() itself.
612
613
  explicit CXX17ElidedCopyReturnedValueConstructionContext(
614
      const ReturnStmt *RS, const CXXBindTemporaryExpr *BTE)
615
      : ReturnedValueConstructionContext(
616
            ConstructionContext::CXX17ElidedCopyReturnedValueKind, RS),
617
81
        BTE(BTE) {
618
81
    assert(BTE);
619
81
  }
620
621
public:
622
3
  const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; }
623
624
3
  static bool classof(const ConstructionContext *CC) {
625
3
    return CC->getKind() == CXX17ElidedCopyReturnedValueKind;
626
3
  }
627
};
628
629
class ArgumentConstructionContext : public ConstructionContext {
630
  // The call of which the context is an argument.
631
  const Expr *CE;
632
633
  // Which argument we're constructing. Note that when numbering between
634
  // arguments and parameters is inconsistent (eg., operator calls),
635
  // this is the index of the argument, not of the parameter.
636
  unsigned Index;
637
638
  // Whether the object needs to be destroyed.
639
  const CXXBindTemporaryExpr *BTE;
640
641
  friend class ConstructionContext; // Allows to create<>() itself.
642
643
  explicit ArgumentConstructionContext(const Expr *CE, unsigned Index,
644
                                       const CXXBindTemporaryExpr *BTE)
645
      : ConstructionContext(ArgumentKind), CE(CE),
646
4.17k
        Index(Index), BTE(BTE) {
647
4.17k
    assert(isa<CallExpr>(CE) || isa<CXXConstructExpr>(CE) ||
648
4.17k
           isa<ObjCMessageExpr>(CE));
649
    // BTE is optional.
650
4.17k
  }
651
652
public:
653
23.1k
  const Expr *getCallLikeExpr() const { return CE; }
654
23.1k
  unsigned getIndex() const { return Index; }
655
11.5k
  const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { return BTE; }
656
657
23.1k
  static bool classof(const ConstructionContext *CC) {
658
23.1k
    return CC->getKind() == ArgumentKind;
659
23.1k
  }
660
};
661
662
} // end namespace clang
663
664
#endif // LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H