|
| 1 | +//===- ConstructionContext.h - CFG constructor information ------*- C++ -*-===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file defines the ConstructionContext class and its sub-classes, |
| 11 | +// which represent various different ways of constructing C++ objects |
| 12 | +// with the additional information the users may want to know about |
| 13 | +// the constructor. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H |
| 18 | +#define LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H |
| 19 | + |
| 20 | +#include "clang/Analysis/Support/BumpVector.h" |
| 21 | +#include "clang/AST/ExprCXX.h" |
| 22 | + |
| 23 | +namespace clang { |
| 24 | + |
| 25 | +/// Construction context is a linked list of multiple layers. Layers are |
| 26 | +/// created gradually while traversing the AST, and layers that represent |
| 27 | +/// the outmost AST nodes are built first, while the node that immediately |
| 28 | +/// contains the constructor would be built last and capture the previous |
| 29 | +/// layers as its parents. Construction context captures the last layer |
| 30 | +/// (which has links to the previous layers) and classifies the seemingly |
| 31 | +/// arbitrary chain of layers into one of the possible ways of constructing |
| 32 | +/// an object in C++ for user-friendly experience. |
| 33 | +class ConstructionContextLayer { |
| 34 | +public: |
| 35 | + typedef llvm::PointerUnion<Stmt *, CXXCtorInitializer *> TriggerTy; |
| 36 | + |
| 37 | +private: |
| 38 | + /// The construction site - the statement that triggered the construction |
| 39 | + /// for one of its parts. For instance, stack variable declaration statement |
| 40 | + /// triggers construction of itself or its elements if it's an array, |
| 41 | + /// new-expression triggers construction of the newly allocated object(s). |
| 42 | + TriggerTy Trigger; |
| 43 | + |
| 44 | + /// Sometimes a single trigger is not enough to describe the construction |
| 45 | + /// site. In this case we'd have a chain of "partial" construction context |
| 46 | + /// layers. |
| 47 | + /// Some examples: |
| 48 | + /// - A constructor within in an aggregate initializer list within a variable |
| 49 | + /// would have a construction context of the initializer list with |
| 50 | + /// the parent construction context of a variable. |
| 51 | + /// - A constructor for a temporary that needs to be both destroyed |
| 52 | + /// and materialized into an elidable copy constructor would have a |
| 53 | + /// construction context of a CXXBindTemporaryExpr with the parent |
| 54 | + /// construction context of a MaterializeTemproraryExpr. |
| 55 | + /// Not all of these are currently supported. |
| 56 | + const ConstructionContextLayer *Parent = nullptr; |
| 57 | + |
| 58 | + ConstructionContextLayer(TriggerTy Trigger, |
| 59 | + const ConstructionContextLayer *Parent) |
| 60 | + : Trigger(Trigger), Parent(Parent) {} |
| 61 | + |
| 62 | +public: |
| 63 | + static const ConstructionContextLayer * |
| 64 | + create(BumpVectorContext &C, TriggerTy Trigger, |
| 65 | + const ConstructionContextLayer *Parent = nullptr); |
| 66 | + |
| 67 | + const ConstructionContextLayer *getParent() const { return Parent; } |
| 68 | + bool isLast() const { return !Parent; } |
| 69 | + |
| 70 | + const Stmt *getTriggerStmt() const { |
| 71 | + return Trigger.dyn_cast<Stmt *>(); |
| 72 | + } |
| 73 | + |
| 74 | + const CXXCtorInitializer *getTriggerInit() const { |
| 75 | + return Trigger.dyn_cast<CXXCtorInitializer *>(); |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns true if these layers are equal as individual layers, even if |
| 79 | + /// their parents are different. |
| 80 | + bool isSameLayer(const ConstructionContextLayer *Other) const { |
| 81 | + assert(Other); |
| 82 | + return (Trigger == Other->Trigger); |
| 83 | + } |
| 84 | + |
| 85 | + /// See if Other is a proper initial segment of this construction context |
| 86 | + /// in terms of the parent chain - i.e. a few first parents coincide and |
| 87 | + /// then the other context terminates but our context goes further - i.e., |
| 88 | + /// we are providing the same context that the other context provides, |
| 89 | + /// and a bit more above that. |
| 90 | + bool isStrictlyMoreSpecificThan(const ConstructionContextLayer *Other) const; |
| 91 | +}; |
| 92 | + |
| 93 | + |
| 94 | +/// ConstructionContext's subclasses describe different ways of constructing |
| 95 | +/// an object in C++. The context re-captures the essential parent AST nodes |
| 96 | +/// of the CXXConstructExpr it is assigned to and presents these nodes |
| 97 | +/// through easy-to-understand accessor methods. |
| 98 | +class ConstructionContext { |
| 99 | +public: |
| 100 | + enum Kind { |
| 101 | + SimpleVariableKind, |
| 102 | + ConstructorInitializerKind, |
| 103 | + NewAllocatedObjectKind, |
| 104 | + TemporaryObjectKind, |
| 105 | + ReturnedValueKind |
| 106 | + }; |
| 107 | + |
| 108 | +protected: |
| 109 | + Kind K; |
| 110 | + |
| 111 | +protected: |
| 112 | + // Do not make public! These need to only be constructed |
| 113 | + // via createFromLayers(). |
| 114 | + explicit ConstructionContext(Kind K) : K(K) {} |
| 115 | + |
| 116 | +public: |
| 117 | + |
| 118 | + /// Consume the construction context layer, together with its parent layers, |
| 119 | + /// and wrap it up into a complete construction context. |
| 120 | + static const ConstructionContext * |
| 121 | + createFromLayers(BumpVectorContext &C, |
| 122 | + const ConstructionContextLayer *TopLayer); |
| 123 | + |
| 124 | + Kind getKind() const { return K; } |
| 125 | +}; |
| 126 | + |
| 127 | +/// Represents construction into a simple local variable, eg. T var(123);. |
| 128 | +class SimpleVariableConstructionContext : public ConstructionContext { |
| 129 | + const DeclStmt *DS; |
| 130 | + |
| 131 | +public: |
| 132 | + explicit SimpleVariableConstructionContext(const DeclStmt *DS) |
| 133 | + : ConstructionContext(ConstructionContext::SimpleVariableKind), DS(DS) { |
| 134 | + assert(DS); |
| 135 | + } |
| 136 | + |
| 137 | + const DeclStmt *getDeclStmt() const { return DS; } |
| 138 | + |
| 139 | + static bool classof(const ConstructionContext *CC) { |
| 140 | + return CC->getKind() == SimpleVariableKind; |
| 141 | + } |
| 142 | +}; |
| 143 | + |
| 144 | +/// Represents construction into a field or a base class within a bigger object |
| 145 | +/// via a constructor initializer, eg. T(): field(123) { ... }. |
| 146 | +class ConstructorInitializerConstructionContext : public ConstructionContext { |
| 147 | + const CXXCtorInitializer *I; |
| 148 | + |
| 149 | +public: |
| 150 | + explicit ConstructorInitializerConstructionContext( |
| 151 | + const CXXCtorInitializer *I) |
| 152 | + : ConstructionContext(ConstructionContext::ConstructorInitializerKind), |
| 153 | + I(I) { |
| 154 | + assert(I); |
| 155 | + } |
| 156 | + |
| 157 | + const CXXCtorInitializer *getCXXCtorInitializer() const { return I; } |
| 158 | + |
| 159 | + static bool classof(const ConstructionContext *CC) { |
| 160 | + return CC->getKind() == ConstructorInitializerKind; |
| 161 | + } |
| 162 | +}; |
| 163 | + |
| 164 | +/// Represents immediate initialization of memory allocated by operator new, |
| 165 | +/// eg. new T(123);. |
| 166 | +class NewAllocatedObjectConstructionContext : public ConstructionContext { |
| 167 | + const CXXNewExpr *NE; |
| 168 | + |
| 169 | +public: |
| 170 | + explicit NewAllocatedObjectConstructionContext(const CXXNewExpr *NE) |
| 171 | + : ConstructionContext(ConstructionContext::NewAllocatedObjectKind), |
| 172 | + NE(NE) { |
| 173 | + assert(NE); |
| 174 | + } |
| 175 | + |
| 176 | + const CXXNewExpr *getCXXNewExpr() const { return NE; } |
| 177 | + |
| 178 | + static bool classof(const ConstructionContext *CC) { |
| 179 | + return CC->getKind() == NewAllocatedObjectKind; |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +/// Represents a temporary object, eg. T(123), that does not immediately cross |
| 184 | +/// function boundaries "by value"; constructors that construct function |
| 185 | +/// value-type arguments or values that are immediately returned from the |
| 186 | +/// function that returns a value receive separate construction context kinds. |
| 187 | +class TemporaryObjectConstructionContext : public ConstructionContext { |
| 188 | + const CXXBindTemporaryExpr *BTE; |
| 189 | + const MaterializeTemporaryExpr *MTE; |
| 190 | + |
| 191 | +public: |
| 192 | + explicit TemporaryObjectConstructionContext( |
| 193 | + const CXXBindTemporaryExpr *BTE, const MaterializeTemporaryExpr *MTE) |
| 194 | + : ConstructionContext(ConstructionContext::TemporaryObjectKind), |
| 195 | + BTE(BTE), MTE(MTE) { |
| 196 | + // Both BTE and MTE can be null here, all combinations possible. |
| 197 | + // Even though for now at least one should be non-null, we simply haven't |
| 198 | + // implemented this case yet (this would be a temporary in the middle of |
| 199 | + // nowhere that doesn't have a non-trivial destructor). |
| 200 | + } |
| 201 | + |
| 202 | + /// CXXBindTemporaryExpr here is non-null as long as the temporary has |
| 203 | + /// a non-trivial destructor. |
| 204 | + const CXXBindTemporaryExpr *getCXXBindTemporaryExpr() const { |
| 205 | + return BTE; |
| 206 | + } |
| 207 | + |
| 208 | + /// MaterializeTemporaryExpr is non-null as long as the temporary is actually |
| 209 | + /// used after construction, eg. by binding to a reference (lifetime |
| 210 | + /// extension), accessing a field, calling a method, or passing it into |
| 211 | + /// a function (an elidable copy or move constructor would be a common |
| 212 | + /// example) by reference. |
| 213 | + const MaterializeTemporaryExpr *getMaterializedTemporaryExpr() const { |
| 214 | + return MTE; |
| 215 | + } |
| 216 | + |
| 217 | + static bool classof(const ConstructionContext *CC) { |
| 218 | + return CC->getKind() == TemporaryObjectKind; |
| 219 | + } |
| 220 | +}; |
| 221 | + |
| 222 | +/// Represents a temporary object that is being immediately returned from a |
| 223 | +/// function by value, eg. return t; or return T(123);. In this case there is |
| 224 | +/// always going to be a constructor at the return site. However, the usual |
| 225 | +/// temporary-related bureaucracy (CXXBindTemporaryExpr, |
| 226 | +/// MaterializeTemporaryExpr) is normally located in the caller function's AST. |
| 227 | +class ReturnedValueConstructionContext : public ConstructionContext { |
| 228 | + const ReturnStmt *RS; |
| 229 | + |
| 230 | +public: |
| 231 | + explicit ReturnedValueConstructionContext(const ReturnStmt *RS) |
| 232 | + : ConstructionContext(ConstructionContext::ReturnedValueKind), RS(RS) { |
| 233 | + assert(RS); |
| 234 | + } |
| 235 | + |
| 236 | + const ReturnStmt *getReturnStmt() const { return RS; } |
| 237 | + |
| 238 | + static bool classof(const ConstructionContext *CC) { |
| 239 | + return CC->getKind() == ReturnedValueKind; |
| 240 | + } |
| 241 | +}; |
| 242 | + |
| 243 | +} // end namespace clang |
| 244 | + |
| 245 | +#endif // LLVM_CLANG_ANALYSIS_CONSTRUCTIONCONTEXT_H |
0 commit comments