/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/APValue.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===// |
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 implements the APValue class. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/APValue.h" |
14 | | #include "Linkage.h" |
15 | | #include "clang/AST/ASTContext.h" |
16 | | #include "clang/AST/CharUnits.h" |
17 | | #include "clang/AST/DeclCXX.h" |
18 | | #include "clang/AST/Expr.h" |
19 | | #include "clang/AST/ExprCXX.h" |
20 | | #include "clang/AST/Type.h" |
21 | | #include "llvm/Support/ErrorHandling.h" |
22 | | #include "llvm/Support/raw_ostream.h" |
23 | | using namespace clang; |
24 | | |
25 | | /// The identity of a type_info object depends on the canonical unqualified |
26 | | /// type only. |
27 | | TypeInfoLValue::TypeInfoLValue(const Type *T) |
28 | 1.46k | : T(T->getCanonicalTypeUnqualified().getTypePtr()) {} |
29 | | |
30 | | void TypeInfoLValue::print(llvm::raw_ostream &Out, |
31 | 29 | const PrintingPolicy &Policy) const { |
32 | 29 | Out << "typeid("; |
33 | 29 | QualType(getType(), 0).print(Out, Policy); |
34 | 29 | Out << ")"; |
35 | 29 | } |
36 | | |
37 | | static_assert( |
38 | | 1 << llvm::PointerLikeTypeTraits<TypeInfoLValue>::NumLowBitsAvailable <= |
39 | | alignof(Type), |
40 | | "Type is insufficiently aligned"); |
41 | | |
42 | | APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V) |
43 | 44.6M | : Ptr(P ? cast<ValueDecl>(P->getCanonicalDecl())19.3M : nullptr25.2M ), Local{I, V} {} |
44 | | APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V) |
45 | 2.00M | : Ptr(P), Local{I, V} {} |
46 | | |
47 | | APValue::LValueBase APValue::LValueBase::getDynamicAlloc(DynamicAllocLValue LV, |
48 | 1.22k | QualType Type) { |
49 | 1.22k | LValueBase Base; |
50 | 1.22k | Base.Ptr = LV; |
51 | 1.22k | Base.DynamicAllocType = Type.getAsOpaquePtr(); |
52 | 1.22k | return Base; |
53 | 1.22k | } |
54 | | |
55 | | APValue::LValueBase APValue::LValueBase::getTypeInfo(TypeInfoLValue LV, |
56 | 1.46k | QualType TypeInfo) { |
57 | 1.46k | LValueBase Base; |
58 | 1.46k | Base.Ptr = LV; |
59 | 1.46k | Base.TypeInfoType = TypeInfo.getAsOpaquePtr(); |
60 | 1.46k | return Base; |
61 | 1.46k | } |
62 | | |
63 | 26.8M | QualType APValue::LValueBase::getType() const { |
64 | 26.8M | if (!*this) return QualType()23 ; |
65 | 26.8M | if (const ValueDecl *D = dyn_cast<const ValueDecl*>()) { |
66 | | // FIXME: It's unclear where we're supposed to take the type from, and |
67 | | // this actually matters for arrays of unknown bound. Eg: |
68 | | // |
69 | | // extern int arr[]; void f() { extern int arr[3]; }; |
70 | | // constexpr int *p = &arr[1]; // valid? |
71 | | // |
72 | | // For now, we take the most complete type we can find. |
73 | 24.5M | for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl; |
74 | 24.5M | Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())2.74k ) { |
75 | 24.5M | QualType T = Redecl->getType(); |
76 | 24.5M | if (!T->isIncompleteArrayType()) |
77 | 24.5M | return T; |
78 | 24.5M | } |
79 | 2.26k | return D->getType(); |
80 | 24.5M | } |
81 | | |
82 | 2.23M | if (is<TypeInfoLValue>()) |
83 | 2.69k | return getTypeInfoType(); |
84 | | |
85 | 2.23M | if (is<DynamicAllocLValue>()) |
86 | 53.8k | return getDynamicAllocType(); |
87 | | |
88 | 2.18M | const Expr *Base = get<const Expr*>(); |
89 | | |
90 | | // For a materialized temporary, the type of the temporary we materialized |
91 | | // may not be the type of the expression. |
92 | 2.18M | if (const MaterializeTemporaryExpr *MTE = |
93 | 2.18M | clang::dyn_cast<MaterializeTemporaryExpr>(Base)) { |
94 | 1.96M | SmallVector<const Expr *, 2> CommaLHSs; |
95 | 1.96M | SmallVector<SubobjectAdjustment, 2> Adjustments; |
96 | 1.96M | const Expr *Temp = MTE->getSubExpr(); |
97 | 1.96M | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
98 | 1.96M | Adjustments); |
99 | | // Keep any cv-qualifiers from the reference if we generated a temporary |
100 | | // for it directly. Otherwise use the type after adjustment. |
101 | 1.96M | if (!Adjustments.empty()) |
102 | 2.58k | return Inner->getType(); |
103 | 1.96M | } |
104 | | |
105 | 2.18M | return Base->getType(); |
106 | 2.18M | } |
107 | | |
108 | 15.1M | unsigned APValue::LValueBase::getCallIndex() const { |
109 | 15.1M | return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()15.1M ) ? 047.2k |
110 | 15.1M | : Local.CallIndex15.1M ; |
111 | 15.1M | } |
112 | | |
113 | 7.04M | unsigned APValue::LValueBase::getVersion() const { |
114 | 7.04M | return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()7.04M ) ? 065 : Local.Version7.04M ; |
115 | 7.04M | } |
116 | | |
117 | 2.69k | QualType APValue::LValueBase::getTypeInfoType() const { |
118 | 2.69k | assert(is<TypeInfoLValue>() && "not a type_info lvalue"); |
119 | 2.69k | return QualType::getFromOpaquePtr(TypeInfoType); |
120 | 2.69k | } |
121 | | |
122 | 100k | QualType APValue::LValueBase::getDynamicAllocType() const { |
123 | 100k | assert(is<DynamicAllocLValue>() && "not a dynamic allocation lvalue"); |
124 | 100k | return QualType::getFromOpaquePtr(DynamicAllocType); |
125 | 100k | } |
126 | | |
127 | 4.07k | void APValue::LValueBase::Profile(llvm::FoldingSetNodeID &ID) const { |
128 | 4.07k | ID.AddPointer(Ptr.getOpaqueValue()); |
129 | 4.07k | if (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) |
130 | 0 | return; |
131 | 4.07k | ID.AddInteger(Local.CallIndex); |
132 | 4.07k | ID.AddInteger(Local.Version); |
133 | 4.07k | } |
134 | | |
135 | | namespace clang { |
136 | | bool operator==(const APValue::LValueBase &LHS, |
137 | 10.6M | const APValue::LValueBase &RHS) { |
138 | 10.6M | if (LHS.Ptr != RHS.Ptr) |
139 | 8.62M | return false; |
140 | 1.99M | if (LHS.is<TypeInfoLValue>() || LHS.is<DynamicAllocLValue>()) |
141 | 633 | return true; |
142 | 1.99M | return LHS.Local.CallIndex == RHS.Local.CallIndex && |
143 | 1.99M | LHS.Local.Version == RHS.Local.Version1.99M ; |
144 | 1.99M | } |
145 | | } |
146 | | |
147 | 223k | APValue::LValuePathEntry::LValuePathEntry(BaseOrMemberType BaseOrMember) { |
148 | 223k | if (const Decl *D = BaseOrMember.getPointer()) |
149 | 223k | BaseOrMember.setPointer(D->getCanonicalDecl()); |
150 | 223k | Value = reinterpret_cast<uintptr_t>(BaseOrMember.getOpaqueValue()); |
151 | 223k | } |
152 | | |
153 | 4.07k | void APValue::LValuePathEntry::Profile(llvm::FoldingSetNodeID &ID) const { |
154 | 4.07k | ID.AddInteger(Value); |
155 | 4.07k | } |
156 | | |
157 | | APValue::LValuePathSerializationHelper::LValuePathSerializationHelper( |
158 | | ArrayRef<LValuePathEntry> Path, QualType ElemTy) |
159 | 41 | : Ty((const void *)ElemTy.getTypePtrOrNull()), Path(Path) {} |
160 | | |
161 | 23 | QualType APValue::LValuePathSerializationHelper::getType() { |
162 | 23 | return QualType::getFromOpaquePtr(Ty); |
163 | 23 | } |
164 | | |
165 | | namespace { |
166 | | struct LVBase { |
167 | | APValue::LValueBase Base; |
168 | | CharUnits Offset; |
169 | | unsigned PathLength; |
170 | | bool IsNullPtr : 1; |
171 | | bool IsOnePastTheEnd : 1; |
172 | | }; |
173 | | } |
174 | | |
175 | 166k | void *APValue::LValueBase::getOpaqueValue() const { |
176 | 166k | return Ptr.getOpaqueValue(); |
177 | 166k | } |
178 | | |
179 | 14.7k | bool APValue::LValueBase::isNull() const { |
180 | 14.7k | return Ptr.isNull(); |
181 | 14.7k | } |
182 | | |
183 | 39.6M | APValue::LValueBase::operator bool () const { |
184 | 39.6M | return static_cast<bool>(Ptr); |
185 | 39.6M | } |
186 | | |
187 | | clang::APValue::LValueBase |
188 | 244k | llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() { |
189 | 244k | clang::APValue::LValueBase B; |
190 | 244k | B.Ptr = DenseMapInfo<const ValueDecl*>::getEmptyKey(); |
191 | 244k | return B; |
192 | 244k | } |
193 | | |
194 | | clang::APValue::LValueBase |
195 | 217k | llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() { |
196 | 217k | clang::APValue::LValueBase B; |
197 | 217k | B.Ptr = DenseMapInfo<const ValueDecl*>::getTombstoneKey(); |
198 | 217k | return B; |
199 | 217k | } |
200 | | |
201 | | namespace clang { |
202 | 149k | llvm::hash_code hash_value(const APValue::LValueBase &Base) { |
203 | 149k | if (Base.is<TypeInfoLValue>() || Base.is<DynamicAllocLValue>()) |
204 | 947 | return llvm::hash_value(Base.getOpaqueValue()); |
205 | 148k | return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(), |
206 | 148k | Base.getVersion()); |
207 | 149k | } |
208 | | } |
209 | | |
210 | | unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue( |
211 | 0 | const clang::APValue::LValueBase &Base) { |
212 | 0 | return hash_value(Base); |
213 | 0 | } |
214 | | |
215 | | bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual( |
216 | | const clang::APValue::LValueBase &LHS, |
217 | 0 | const clang::APValue::LValueBase &RHS) { |
218 | 0 | return LHS == RHS; |
219 | 0 | } |
220 | | |
221 | | struct APValue::LV : LVBase { |
222 | | static const unsigned InlinePathSpace = |
223 | | (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry); |
224 | | |
225 | | /// Path - The sequence of base classes, fields and array indices to follow to |
226 | | /// walk from Base to the subobject. When performing GCC-style folding, there |
227 | | /// may not be such a path. |
228 | | union { |
229 | | LValuePathEntry Path[InlinePathSpace]; |
230 | | LValuePathEntry *PathPtr; |
231 | | }; |
232 | | |
233 | 1.84M | LV() { PathLength = (unsigned)-1; } |
234 | 1.82M | ~LV() { resizePath(0); } |
235 | | |
236 | 3.67M | void resizePath(unsigned Length) { |
237 | 3.67M | if (Length == PathLength) |
238 | 1.65M | return; |
239 | 2.01M | if (hasPathPtr()) |
240 | 2.67k | delete [] PathPtr; |
241 | 2.01M | PathLength = Length; |
242 | 2.01M | if (hasPathPtr()) |
243 | 2.67k | PathPtr = new LValuePathEntry[Length]; |
244 | 2.01M | } |
245 | | |
246 | 11.7M | bool hasPath() const { return PathLength != (unsigned)-1; } |
247 | 8.20M | bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace6.36M ; } |
248 | | |
249 | 1.83M | LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr2.67k : Path1.83M ; } |
250 | 2.32M | const LValuePathEntry *getPath() const { |
251 | 2.32M | return hasPathPtr() ? PathPtr4.24k : Path2.32M ; |
252 | 2.32M | } |
253 | | }; |
254 | | |
255 | | namespace { |
256 | | struct MemberPointerBase { |
257 | | llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember; |
258 | | unsigned PathLength; |
259 | | }; |
260 | | } |
261 | | |
262 | | struct APValue::MemberPointerData : MemberPointerBase { |
263 | | static const unsigned InlinePathSpace = |
264 | | (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*); |
265 | | typedef const CXXRecordDecl *PathElem; |
266 | | union { |
267 | | PathElem Path[InlinePathSpace]; |
268 | | PathElem *PathPtr; |
269 | | }; |
270 | | |
271 | 4.48k | MemberPointerData() { PathLength = 0; } |
272 | 3.79k | ~MemberPointerData() { resizePath(0); } |
273 | | |
274 | 8.27k | void resizePath(unsigned Length) { |
275 | 8.27k | if (Length == PathLength) |
276 | 7.53k | return; |
277 | 741 | if (hasPathPtr()) |
278 | 91 | delete [] PathPtr; |
279 | 741 | PathLength = Length; |
280 | 741 | if (hasPathPtr()) |
281 | 91 | PathPtr = new PathElem[Length]; |
282 | 741 | } |
283 | | |
284 | 8.26k | bool hasPathPtr() const { return PathLength > InlinePathSpace; } |
285 | | |
286 | 4.48k | PathElem *getPath() { return hasPathPtr() ? PathPtr91 : Path4.39k ; } |
287 | 1.60k | const PathElem *getPath() const { |
288 | 1.60k | return hasPathPtr() ? PathPtr168 : Path1.43k ; |
289 | 1.60k | } |
290 | | }; |
291 | | |
292 | | // FIXME: Reduce the malloc traffic here. |
293 | | |
294 | | APValue::Arr::Arr(unsigned NumElts, unsigned Size) : |
295 | 26.5k | Elts(new APValue[NumElts + (NumElts != Size ? 116.9k : 09.55k )]), |
296 | 26.5k | NumElts(NumElts), ArrSize(Size) {} |
297 | 26.5k | APValue::Arr::~Arr() { delete [] Elts; } |
298 | | |
299 | | APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) : |
300 | 84.1k | Elts(new APValue[NumBases+NumFields]), |
301 | 84.1k | NumBases(NumBases), NumFields(NumFields) {} |
302 | 84.1k | APValue::StructData::~StructData() { |
303 | 84.1k | delete [] Elts; |
304 | 84.1k | } |
305 | | |
306 | 3.49k | APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {} |
307 | 3.49k | APValue::UnionData::~UnionData () { |
308 | 3.49k | delete Value; |
309 | 3.49k | } |
310 | | |
311 | 11.3M | APValue::APValue(const APValue &RHS) : Kind(None) { |
312 | 11.3M | switch (RHS.getKind()) { |
313 | 12.2k | case None: |
314 | 29.1k | case Indeterminate: |
315 | 29.1k | Kind = RHS.getKind(); |
316 | 29.1k | break; |
317 | 11.2M | case Int: |
318 | 11.2M | MakeInt(); |
319 | 11.2M | setInt(RHS.getInt()); |
320 | 11.2M | break; |
321 | 20.2k | case Float: |
322 | 20.2k | MakeFloat(); |
323 | 20.2k | setFloat(RHS.getFloat()); |
324 | 20.2k | break; |
325 | 6 | case FixedPoint: { |
326 | 6 | APFixedPoint FXCopy = RHS.getFixedPoint(); |
327 | 6 | MakeFixedPoint(std::move(FXCopy)); |
328 | 6 | break; |
329 | 12.2k | } |
330 | 1.99k | case Vector: |
331 | 1.99k | MakeVector(); |
332 | 1.99k | setVector(((const Vec *)(const char *)&RHS.Data)->Elts, |
333 | 1.99k | RHS.getVectorLength()); |
334 | 1.99k | break; |
335 | 171 | case ComplexInt: |
336 | 171 | MakeComplexInt(); |
337 | 171 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
338 | 171 | break; |
339 | 260 | case ComplexFloat: |
340 | 260 | MakeComplexFloat(); |
341 | 260 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
342 | 260 | break; |
343 | 94.7k | case LValue: |
344 | 94.7k | MakeLValue(); |
345 | 94.7k | if (RHS.hasLValuePath()) |
346 | 94.2k | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(), |
347 | 94.2k | RHS.isLValueOnePastTheEnd(), RHS.isNullPointer()); |
348 | 480 | else |
349 | 480 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(), |
350 | 480 | RHS.isNullPointer()); |
351 | 94.7k | break; |
352 | 4.02k | case Array: |
353 | 4.02k | MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize()); |
354 | 21.1k | for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I17.1k ) |
355 | 17.1k | getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I); |
356 | 4.02k | if (RHS.hasArrayFiller()) |
357 | 645 | getArrayFiller() = RHS.getArrayFiller(); |
358 | 4.02k | break; |
359 | 14.0k | case Struct: |
360 | 14.0k | MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields()); |
361 | 15.0k | for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I959 ) |
362 | 959 | getStructBase(I) = RHS.getStructBase(I); |
363 | 33.1k | for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I19.1k ) |
364 | 19.1k | getStructField(I) = RHS.getStructField(I); |
365 | 14.0k | break; |
366 | 1.06k | case Union: |
367 | 1.06k | MakeUnion(); |
368 | 1.06k | setUnion(RHS.getUnionField(), RHS.getUnionValue()); |
369 | 1.06k | break; |
370 | 761 | case MemberPointer: |
371 | 761 | MakeMemberPointer(RHS.getMemberPointerDecl(), |
372 | 761 | RHS.isMemberPointerToDerivedMember(), |
373 | 761 | RHS.getMemberPointerPath()); |
374 | 761 | break; |
375 | 19 | case AddrLabelDiff: |
376 | 19 | MakeAddrLabelDiff(); |
377 | 19 | setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS()); |
378 | 19 | break; |
379 | 11.3M | } |
380 | 11.3M | } |
381 | | |
382 | 89.1k | APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) { |
383 | 89.1k | RHS.Kind = None; |
384 | 89.1k | } |
385 | | |
386 | 5.00M | APValue &APValue::operator=(const APValue &RHS) { |
387 | 5.00M | if (this != &RHS) |
388 | 5.00M | *this = APValue(RHS); |
389 | 5.00M | return *this; |
390 | 5.00M | } |
391 | | |
392 | 54.1M | APValue &APValue::operator=(APValue &&RHS) { |
393 | 54.1M | if (this != &RHS) { |
394 | 54.1M | if (Kind != None && Kind != Indeterminate3.10M ) |
395 | 3.09M | DestroyDataAndMakeUninit(); |
396 | 54.1M | Kind = RHS.Kind; |
397 | 54.1M | Data = RHS.Data; |
398 | 54.1M | RHS.Kind = None; |
399 | 54.1M | } |
400 | 54.1M | return *this; |
401 | 54.1M | } |
402 | | |
403 | 54.8M | void APValue::DestroyDataAndMakeUninit() { |
404 | 54.8M | if (Kind == Int) |
405 | 52.7M | ((APSInt *)(char *)&Data)->~APSInt(); |
406 | 2.04M | else if (Kind == Float) |
407 | 86.3k | ((APFloat *)(char *)&Data)->~APFloat(); |
408 | 1.95M | else if (Kind == FixedPoint) |
409 | 2.94k | ((APFixedPoint *)(char *)&Data)->~APFixedPoint(); |
410 | 1.95M | else if (Kind == Vector) |
411 | 4.88k | ((Vec *)(char *)&Data)->~Vec(); |
412 | 1.94M | else if (Kind == ComplexInt) |
413 | 258 | ((ComplexAPSInt *)(char *)&Data)->~ComplexAPSInt(); |
414 | 1.94M | else if (Kind == ComplexFloat) |
415 | 503 | ((ComplexAPFloat *)(char *)&Data)->~ComplexAPFloat(); |
416 | 1.94M | else if (Kind == LValue) |
417 | 1.82M | ((LV *)(char *)&Data)->~LV(); |
418 | 118k | else if (Kind == Array) |
419 | 26.5k | ((Arr *)(char *)&Data)->~Arr(); |
420 | 91.5k | else if (Kind == Struct) |
421 | 84.1k | ((StructData *)(char *)&Data)->~StructData(); |
422 | 7.34k | else if (Kind == Union) |
423 | 3.49k | ((UnionData *)(char *)&Data)->~UnionData(); |
424 | 3.84k | else if (Kind == MemberPointer) |
425 | 3.79k | ((MemberPointerData *)(char *)&Data)->~MemberPointerData(); |
426 | 56 | else if (Kind == AddrLabelDiff) |
427 | 56 | ((AddrLabelDiffData *)(char *)&Data)->~AddrLabelDiffData(); |
428 | 54.8M | Kind = None; |
429 | 54.8M | } |
430 | | |
431 | 438k | bool APValue::needsCleanup() const { |
432 | 438k | switch (getKind()) { |
433 | 10 | case None: |
434 | 10 | case Indeterminate: |
435 | 17 | case AddrLabelDiff: |
436 | 17 | return false; |
437 | 21.0k | case Struct: |
438 | 21.3k | case Union: |
439 | 24.2k | case Array: |
440 | 24.6k | case Vector: |
441 | 24.6k | return true; |
442 | 397k | case Int: |
443 | 397k | return getInt().needsCleanup(); |
444 | 2.97k | case Float: |
445 | 2.97k | return getFloat().needsCleanup(); |
446 | 350 | case FixedPoint: |
447 | 350 | return getFixedPoint().getValue().needsCleanup(); |
448 | 57 | case ComplexFloat: |
449 | 57 | assert(getComplexFloatImag().needsCleanup() == |
450 | 57 | getComplexFloatReal().needsCleanup() && |
451 | 57 | "In _Complex float types, real and imaginary values always have the " |
452 | 57 | "same size."); |
453 | 57 | return getComplexFloatReal().needsCleanup(); |
454 | 31 | case ComplexInt: |
455 | 31 | assert(getComplexIntImag().needsCleanup() == |
456 | 31 | getComplexIntReal().needsCleanup() && |
457 | 31 | "In _Complex int types, real and imaginary values must have the " |
458 | 31 | "same size."); |
459 | 31 | return getComplexIntReal().needsCleanup(); |
460 | 12.2k | case LValue: |
461 | 12.2k | return reinterpret_cast<const LV *>(&Data)->hasPathPtr(); |
462 | 696 | case MemberPointer: |
463 | 696 | return reinterpret_cast<const MemberPointerData *>(&Data)->hasPathPtr(); |
464 | 438k | } |
465 | 0 | llvm_unreachable("Unknown APValue kind!"); |
466 | 0 | } |
467 | | |
468 | 19.7M | void APValue::swap(APValue &RHS) { |
469 | 19.7M | std::swap(Kind, RHS.Kind); |
470 | 19.7M | std::swap(Data, RHS.Data); |
471 | 19.7M | } |
472 | | |
473 | | /// Profile the value of an APInt, excluding its bit-width. |
474 | 6.20k | static void profileIntValue(llvm::FoldingSetNodeID &ID, const llvm::APInt &V) { |
475 | 12.4k | for (unsigned I = 0, N = V.getBitWidth(); I < N; I += 326.27k ) |
476 | 6.27k | ID.AddInteger((uint32_t)V.extractBitsAsZExtValue(std::min(32u, N - I), I)); |
477 | 6.20k | } |
478 | | |
479 | 13.3k | void APValue::Profile(llvm::FoldingSetNodeID &ID) const { |
480 | | // Note that our profiling assumes that only APValues of the same type are |
481 | | // ever compared. As a result, we don't consider collisions that could only |
482 | | // happen if the types are different. (For example, structs with different |
483 | | // numbers of members could profile the same.) |
484 | | |
485 | 13.3k | ID.AddInteger(Kind); |
486 | | |
487 | 13.3k | switch (Kind) { |
488 | 32 | case None: |
489 | 32 | case Indeterminate: |
490 | 32 | return; |
491 | | |
492 | 0 | case AddrLabelDiff: |
493 | 0 | ID.AddPointer(getAddrLabelDiffLHS()->getLabel()->getCanonicalDecl()); |
494 | 0 | ID.AddPointer(getAddrLabelDiffRHS()->getLabel()->getCanonicalDecl()); |
495 | 0 | return; |
496 | | |
497 | 2.65k | case Struct: |
498 | 2.68k | for (unsigned I = 0, N = getStructNumBases(); I != N; ++I34 ) |
499 | 34 | getStructBase(I).Profile(ID); |
500 | 11.4k | for (unsigned I = 0, N = getStructNumFields(); I != N; ++I8.79k ) |
501 | 8.79k | getStructField(I).Profile(ID); |
502 | 2.65k | return; |
503 | | |
504 | 93 | case Union: |
505 | 93 | if (!getUnionField()) { |
506 | 17 | ID.AddInteger(0); |
507 | 17 | return; |
508 | 17 | } |
509 | 76 | ID.AddInteger(getUnionField()->getFieldIndex() + 1); |
510 | 76 | getUnionValue().Profile(ID); |
511 | 76 | return; |
512 | | |
513 | 186 | case Array: { |
514 | 186 | if (getArraySize() == 0) |
515 | 0 | return; |
516 | | |
517 | | // The profile should not depend on whether the array is expanded or |
518 | | // not, but we don't want to profile the array filler many times for |
519 | | // a large array. So treat all equal trailing elements as the filler. |
520 | | // Elements are profiled in reverse order to support this, and the |
521 | | // first profiled element is followed by a count. For example: |
522 | | // |
523 | | // ['a', 'c', 'x', 'x', 'x'] is profiled as |
524 | | // [5, 'x', 3, 'c', 'a'] |
525 | 186 | llvm::FoldingSetNodeID FillerID; |
526 | 186 | (hasArrayFiller() ? getArrayFiller()86 |
527 | 186 | : getArrayInitializedElt(getArrayInitializedElts() - 1)100 ) |
528 | 186 | .Profile(FillerID); |
529 | 186 | ID.AddNodeID(FillerID); |
530 | 186 | unsigned NumFillers = getArraySize() - getArrayInitializedElts(); |
531 | 186 | unsigned N = getArrayInitializedElts(); |
532 | | |
533 | | // Count the number of elements equal to the last one. This loop ends |
534 | | // by adding an integer indicating the number of such elements, with |
535 | | // N set to the number of elements left to profile. |
536 | 321 | while (true) { |
537 | 321 | if (N == 0) { |
538 | | // All elements are fillers. |
539 | 24 | assert(NumFillers == getArraySize()); |
540 | 24 | ID.AddInteger(NumFillers); |
541 | 24 | break; |
542 | 24 | } |
543 | | |
544 | | // No need to check if the last element is equal to the last |
545 | | // element. |
546 | 297 | if (N != getArraySize()) { |
547 | 197 | llvm::FoldingSetNodeID ElemID; |
548 | 197 | getArrayInitializedElt(N - 1).Profile(ElemID); |
549 | 197 | if (ElemID != FillerID) { |
550 | 162 | ID.AddInteger(NumFillers); |
551 | 162 | ID.AddNodeID(ElemID); |
552 | 162 | --N; |
553 | 162 | break; |
554 | 162 | } |
555 | 197 | } |
556 | | |
557 | | // This is a filler. |
558 | 135 | ++NumFillers; |
559 | 135 | --N; |
560 | 135 | } |
561 | | |
562 | | // Emit the remaining elements. |
563 | 1.47k | for (; 186 N != 0; --N1.29k ) |
564 | 1.29k | getArrayInitializedElt(N - 1).Profile(ID); |
565 | 186 | return; |
566 | 186 | } |
567 | | |
568 | 31 | case Vector: |
569 | 123 | for (unsigned I = 0, N = getVectorLength(); I != N; ++I92 ) |
570 | 92 | getVectorElt(I).Profile(ID); |
571 | 31 | return; |
572 | | |
573 | 6.04k | case Int: |
574 | 6.04k | profileIntValue(ID, getInt()); |
575 | 6.04k | return; |
576 | | |
577 | 72 | case Float: |
578 | 72 | profileIntValue(ID, getFloat().bitcastToAPInt()); |
579 | 72 | return; |
580 | | |
581 | 0 | case FixedPoint: |
582 | 0 | profileIntValue(ID, getFixedPoint().getValue()); |
583 | 0 | return; |
584 | | |
585 | 24 | case ComplexFloat: |
586 | 24 | profileIntValue(ID, getComplexFloatReal().bitcastToAPInt()); |
587 | 24 | profileIntValue(ID, getComplexFloatImag().bitcastToAPInt()); |
588 | 24 | return; |
589 | | |
590 | 24 | case ComplexInt: |
591 | 24 | profileIntValue(ID, getComplexIntReal()); |
592 | 24 | profileIntValue(ID, getComplexIntImag()); |
593 | 24 | return; |
594 | | |
595 | 4.07k | case LValue: |
596 | 4.07k | getLValueBase().Profile(ID); |
597 | 4.07k | ID.AddInteger(getLValueOffset().getQuantity()); |
598 | 4.07k | ID.AddInteger((isNullPointer() ? 123 : 04.04k ) | |
599 | 4.07k | (isLValueOnePastTheEnd() ? 22 : 04.06k ) | |
600 | 4.07k | (hasLValuePath() ? 44.06k : 09 )); |
601 | 4.07k | if (hasLValuePath()) { |
602 | 4.06k | ID.AddInteger(getLValuePath().size()); |
603 | | // For uniqueness, we only need to profile the entries corresponding |
604 | | // to union members, but we don't have the type here so we don't know |
605 | | // how to interpret the entries. |
606 | 4.06k | for (LValuePathEntry E : getLValuePath()) |
607 | 4.07k | E.Profile(ID); |
608 | 4.06k | } |
609 | 4.07k | return; |
610 | | |
611 | 77 | case MemberPointer: |
612 | 77 | ID.AddPointer(getMemberPointerDecl()); |
613 | 77 | ID.AddInteger(isMemberPointerToDerivedMember()); |
614 | 77 | for (const CXXRecordDecl *D : getMemberPointerPath()) |
615 | 13 | ID.AddPointer(D); |
616 | 77 | return; |
617 | 13.3k | } |
618 | | |
619 | 0 | llvm_unreachable("Unknown APValue kind!"); |
620 | 0 | } |
621 | | |
622 | 36 | static double GetApproxValue(const llvm::APFloat &F) { |
623 | 36 | llvm::APFloat V = F; |
624 | 36 | bool ignored; |
625 | 36 | V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven, |
626 | 36 | &ignored); |
627 | 36 | return V.convertToDouble(); |
628 | 36 | } |
629 | | |
630 | | static bool TryPrintAsStringLiteral(raw_ostream &Out, |
631 | | const PrintingPolicy &Policy, |
632 | | const ArrayType *ATy, |
633 | 73 | ArrayRef<APValue> Inits) { |
634 | 73 | if (Inits.empty()) |
635 | 0 | return false; |
636 | | |
637 | 73 | QualType Ty = ATy->getElementType(); |
638 | 73 | if (!Ty->isAnyCharacterType()) |
639 | 14 | return false; |
640 | | |
641 | | // Nothing we can do about a sequence that is not null-terminated |
642 | 59 | if (!Inits.back().isInt() || !Inits.back().getInt().isZero()58 ) |
643 | 1 | return false; |
644 | | |
645 | 58 | Inits = Inits.drop_back(); |
646 | | |
647 | 58 | llvm::SmallString<40> Buf; |
648 | 58 | Buf.push_back('"'); |
649 | | |
650 | | // Better than printing a two-digit sequence of 10 integers. |
651 | 58 | constexpr size_t MaxN = 36; |
652 | 58 | StringRef Ellipsis; |
653 | 58 | if (Inits.size() > MaxN && !Policy.EntireContentsOfLargeArray11 ) { |
654 | 3 | Ellipsis = "[...]"; |
655 | 3 | Inits = |
656 | 3 | Inits.take_front(std::min(MaxN - Ellipsis.size() / 2, Inits.size())); |
657 | 3 | } |
658 | | |
659 | 1.00k | for (auto &Val : Inits) { |
660 | 1.00k | if (!Val.isInt()) |
661 | 0 | return false; |
662 | 1.00k | int64_t Char64 = Val.getInt().getExtValue(); |
663 | 1.00k | if (!isASCII(Char64)) |
664 | 3 | return false; // Bye bye, see you in integers. |
665 | 998 | auto Ch = static_cast<unsigned char>(Char64); |
666 | | // The diagnostic message is 'quoted' |
667 | 998 | StringRef Escaped = escapeCStyle<EscapeChar::SingleAndDouble>(Ch); |
668 | 998 | if (Escaped.empty()) { |
669 | 961 | if (!isPrintable(Ch)) |
670 | 14 | return false; |
671 | 947 | Buf.emplace_back(Ch); |
672 | 947 | } else { |
673 | 37 | Buf.append(Escaped); |
674 | 37 | } |
675 | 998 | } |
676 | | |
677 | 41 | Buf.append(Ellipsis); |
678 | 41 | Buf.push_back('"'); |
679 | | |
680 | 41 | if (Ty->isWideCharType()) |
681 | 9 | Out << 'L'; |
682 | 32 | else if (Ty->isChar8Type()) |
683 | 4 | Out << "u8"; |
684 | 28 | else if (Ty->isChar16Type()) |
685 | 4 | Out << 'u'; |
686 | 24 | else if (Ty->isChar32Type()) |
687 | 4 | Out << 'U'; |
688 | | |
689 | 41 | Out << Buf; |
690 | 41 | return true; |
691 | 58 | } |
692 | | |
693 | | void APValue::printPretty(raw_ostream &Out, const ASTContext &Ctx, |
694 | 3.94k | QualType Ty) const { |
695 | 3.94k | printPretty(Out, Ctx.getPrintingPolicy(), Ty, &Ctx); |
696 | 3.94k | } |
697 | | |
698 | | void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy, |
699 | 4.41k | QualType Ty, const ASTContext *Ctx) const { |
700 | | // There are no objects of type 'void', but values of this type can be |
701 | | // returned from functions. |
702 | 4.41k | if (Ty->isVoidType()) { |
703 | 0 | Out << "void()"; |
704 | 0 | return; |
705 | 0 | } |
706 | | |
707 | 4.41k | switch (getKind()) { |
708 | 0 | case APValue::None: |
709 | 0 | Out << "<out of lifetime>"; |
710 | 0 | return; |
711 | 5 | case APValue::Indeterminate: |
712 | 5 | Out << "<uninitialized>"; |
713 | 5 | return; |
714 | 1.81k | case APValue::Int: |
715 | 1.81k | if (Ty->isBooleanType()) |
716 | 62 | Out << (getInt().getBoolValue() ? "true"40 : "false"22 ); |
717 | 1.75k | else |
718 | 1.75k | Out << getInt(); |
719 | 1.81k | return; |
720 | 32 | case APValue::Float: |
721 | 32 | Out << GetApproxValue(getFloat()); |
722 | 32 | return; |
723 | 0 | case APValue::FixedPoint: |
724 | 0 | Out << getFixedPoint(); |
725 | 0 | return; |
726 | 2 | case APValue::Vector: { |
727 | 2 | Out << '{'; |
728 | 2 | QualType ElemTy = Ty->castAs<VectorType>()->getElementType(); |
729 | 2 | getVectorElt(0).printPretty(Out, Policy, ElemTy, Ctx); |
730 | 8 | for (unsigned i = 1; i != getVectorLength(); ++i6 ) { |
731 | 6 | Out << ", "; |
732 | 6 | getVectorElt(i).printPretty(Out, Policy, ElemTy, Ctx); |
733 | 6 | } |
734 | 2 | Out << '}'; |
735 | 2 | return; |
736 | 0 | } |
737 | 2 | case APValue::ComplexInt: |
738 | 2 | Out << getComplexIntReal() << "+" << getComplexIntImag() << "i"; |
739 | 2 | return; |
740 | 2 | case APValue::ComplexFloat: |
741 | 2 | Out << GetApproxValue(getComplexFloatReal()) << "+" |
742 | 2 | << GetApproxValue(getComplexFloatImag()) << "i"; |
743 | 2 | return; |
744 | 2.29k | case APValue::LValue: { |
745 | 2.29k | bool IsReference = Ty->isReferenceType(); |
746 | 2.29k | QualType InnerTy |
747 | 2.29k | = IsReference ? Ty.getNonReferenceType()1.13k : Ty->getPointeeType()1.16k ; |
748 | 2.29k | if (InnerTy.isNull()) |
749 | 17 | InnerTy = Ty; |
750 | | |
751 | 2.29k | LValueBase Base = getLValueBase(); |
752 | 2.29k | if (!Base) { |
753 | 295 | if (isNullPointer()) { |
754 | 269 | Out << (Policy.Nullptr ? "nullptr"173 : "0"96 ); |
755 | 269 | } else if (26 IsReference26 ) { |
756 | 0 | Out << "*(" << InnerTy.stream(Policy) << "*)" |
757 | 0 | << getLValueOffset().getQuantity(); |
758 | 26 | } else { |
759 | 26 | Out << "(" << Ty.stream(Policy) << ")" |
760 | 26 | << getLValueOffset().getQuantity(); |
761 | 26 | } |
762 | 295 | return; |
763 | 295 | } |
764 | | |
765 | 2.00k | if (!hasLValuePath()) { |
766 | | // No lvalue path: just print the offset. |
767 | 60 | CharUnits O = getLValueOffset(); |
768 | 60 | CharUnits S = Ctx ? Ctx->getTypeSizeInCharsIfKnown(InnerTy).value_or( |
769 | 60 | CharUnits::Zero()) |
770 | 60 | : CharUnits::Zero()0 ; |
771 | 60 | if (!O.isZero()) { |
772 | 2 | if (IsReference) |
773 | 0 | Out << "*("; |
774 | 2 | if (S.isZero() || O % S) { |
775 | 2 | Out << "(char*)"; |
776 | 2 | S = CharUnits::One(); |
777 | 2 | } |
778 | 2 | Out << '&'; |
779 | 58 | } else if (!IsReference) { |
780 | 58 | Out << '&'; |
781 | 58 | } |
782 | | |
783 | 60 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) |
784 | 60 | Out << *VD; |
785 | 0 | else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) { |
786 | 0 | TI.print(Out, Policy); |
787 | 0 | } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { |
788 | 0 | Out << "{*new " |
789 | 0 | << Base.getDynamicAllocType().stream(Policy) << "#" |
790 | 0 | << DA.getIndex() << "}"; |
791 | 0 | } else { |
792 | 0 | assert(Base.get<const Expr *>() != nullptr && |
793 | 0 | "Expecting non-null Expr"); |
794 | 0 | Base.get<const Expr*>()->printPretty(Out, nullptr, Policy); |
795 | 0 | } |
796 | | |
797 | 60 | if (!O.isZero()) { |
798 | 2 | Out << " + " << (O / S); |
799 | 2 | if (IsReference) |
800 | 0 | Out << ')'; |
801 | 2 | } |
802 | 60 | return; |
803 | 60 | } |
804 | | |
805 | | // We have an lvalue path. Print it out nicely. |
806 | 1.94k | if (!IsReference) |
807 | 807 | Out << '&'; |
808 | 1.13k | else if (isLValueOnePastTheEnd()) |
809 | 0 | Out << "*(&"; |
810 | | |
811 | 1.94k | QualType ElemTy = Base.getType(); |
812 | 1.94k | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
813 | 1.64k | Out << *VD; |
814 | 1.64k | } else if (TypeInfoLValue 294 TI294 = Base.dyn_cast<TypeInfoLValue>()) { |
815 | 29 | TI.print(Out, Policy); |
816 | 265 | } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { |
817 | 19 | Out << "{*new " << Base.getDynamicAllocType().stream(Policy) << "#" |
818 | 19 | << DA.getIndex() << "}"; |
819 | 246 | } else { |
820 | 246 | const Expr *E = Base.get<const Expr*>(); |
821 | 246 | assert(E != nullptr && "Expecting non-null Expr"); |
822 | 246 | E->printPretty(Out, nullptr, Policy); |
823 | 246 | } |
824 | | |
825 | 1.94k | ArrayRef<LValuePathEntry> Path = getLValuePath(); |
826 | 1.94k | const CXXRecordDecl *CastToBase = nullptr; |
827 | 2.52k | for (unsigned I = 0, N = Path.size(); I != N; ++I578 ) { |
828 | 578 | if (ElemTy->isRecordType()) { |
829 | | // The lvalue refers to a class type, so the next path entry is a base |
830 | | // or member. |
831 | 159 | const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer(); |
832 | 159 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) { |
833 | 60 | CastToBase = RD; |
834 | | // Leave ElemTy referring to the most-derived class. The actual type |
835 | | // doesn't matter except for array types. |
836 | 99 | } else { |
837 | 99 | const ValueDecl *VD = cast<ValueDecl>(BaseOrMember); |
838 | 99 | Out << "."; |
839 | 99 | if (CastToBase) |
840 | 4 | Out << *CastToBase << "::"; |
841 | 99 | Out << *VD; |
842 | 99 | ElemTy = VD->getType(); |
843 | 99 | } |
844 | 419 | } else if (ElemTy->isAnyComplexType()) { |
845 | | // The lvalue refers to a complex type |
846 | 4 | Out << (Path[I].getAsArrayIndex() == 0 ? ".real"2 : ".imag"2 ); |
847 | 4 | ElemTy = ElemTy->castAs<ComplexType>()->getElementType(); |
848 | 415 | } else { |
849 | | // The lvalue must refer to an array. |
850 | 415 | Out << '[' << Path[I].getAsArrayIndex() << ']'; |
851 | 415 | ElemTy = ElemTy->castAsArrayTypeUnsafe()->getElementType(); |
852 | 415 | } |
853 | 578 | } |
854 | | |
855 | | // Handle formatting of one-past-the-end lvalues. |
856 | 1.94k | if (isLValueOnePastTheEnd()) { |
857 | | // FIXME: If CastToBase is non-0, we should prefix the output with |
858 | | // "(CastToBase*)". |
859 | 22 | Out << " + 1"; |
860 | 22 | if (IsReference) |
861 | 0 | Out << ')'; |
862 | 22 | } |
863 | 1.94k | return; |
864 | 1.94k | } |
865 | 77 | case APValue::Array: { |
866 | 77 | const ArrayType *AT = Ty->castAsArrayTypeUnsafe(); |
867 | 77 | unsigned N = getArrayInitializedElts(); |
868 | 77 | if (N != 0 && TryPrintAsStringLiteral(Out, Policy, AT, |
869 | 73 | {&getArrayInitializedElt(0), N})) |
870 | 41 | return; |
871 | 36 | QualType ElemTy = AT->getElementType(); |
872 | 36 | Out << '{'; |
873 | 36 | unsigned I = 0; |
874 | 36 | switch (N) { |
875 | 4 | case 0: |
876 | 197 | for (; I != N; ++I193 ) { |
877 | 165 | Out << ", "; |
878 | 165 | if (I == 10 && !Policy.EntireContentsOfLargeArray7 ) { |
879 | 4 | Out << "...}"; |
880 | 4 | return; |
881 | 4 | } |
882 | 165 | [[fallthrough]];161 |
883 | 193 | default: |
884 | 193 | getArrayInitializedElt(I).printPretty(Out, Policy, ElemTy, Ctx); |
885 | 193 | } |
886 | 36 | } |
887 | 32 | Out << '}'; |
888 | 32 | return; |
889 | 36 | } |
890 | 155 | case APValue::Struct: { |
891 | 155 | Out << '{'; |
892 | 155 | const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl(); |
893 | 155 | bool First = true; |
894 | 155 | if (unsigned N = getStructNumBases()) { |
895 | 14 | const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD); |
896 | 14 | CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin(); |
897 | 28 | for (unsigned I = 0; I != N; ++I, ++BI14 ) { |
898 | 14 | assert(BI != CD->bases_end()); |
899 | 14 | if (!First) |
900 | 0 | Out << ", "; |
901 | 14 | getStructBase(I).printPretty(Out, Policy, BI->getType(), Ctx); |
902 | 14 | First = false; |
903 | 14 | } |
904 | 14 | } |
905 | 155 | for (const auto *FI : RD->fields()) { |
906 | 132 | if (!First) |
907 | 25 | Out << ", "; |
908 | 132 | if (FI->isUnnamedBitfield()) continue0 ; |
909 | 132 | getStructField(FI->getFieldIndex()). |
910 | 132 | printPretty(Out, Policy, FI->getType(), Ctx); |
911 | 132 | First = false; |
912 | 132 | } |
913 | 155 | Out << '}'; |
914 | 155 | return; |
915 | 155 | } |
916 | 17 | case APValue::Union: |
917 | 17 | Out << '{'; |
918 | 17 | if (const FieldDecl *FD = getUnionField()) { |
919 | 17 | Out << "." << *FD << " = "; |
920 | 17 | getUnionValue().printPretty(Out, Policy, FD->getType(), Ctx); |
921 | 17 | } |
922 | 17 | Out << '}'; |
923 | 17 | return; |
924 | 2 | case APValue::MemberPointer: |
925 | | // FIXME: This is not enough to unambiguously identify the member in a |
926 | | // multiple-inheritance scenario. |
927 | 2 | if (const ValueDecl *VD = getMemberPointerDecl()) { |
928 | 2 | Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD; |
929 | 2 | return; |
930 | 2 | } |
931 | 0 | Out << "0"; |
932 | 0 | return; |
933 | 2 | case APValue::AddrLabelDiff: |
934 | 2 | Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName(); |
935 | 2 | Out << " - "; |
936 | 2 | Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName(); |
937 | 2 | return; |
938 | 4.41k | } |
939 | 0 | llvm_unreachable("Unknown APValue kind!"); |
940 | 0 | } |
941 | | |
942 | 1.43k | std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const { |
943 | 1.43k | std::string Result; |
944 | 1.43k | llvm::raw_string_ostream Out(Result); |
945 | 1.43k | printPretty(Out, Ctx, Ty); |
946 | 1.43k | Out.flush(); |
947 | 1.43k | return Result; |
948 | 1.43k | } |
949 | | |
950 | | bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy, |
951 | 1.19k | const ASTContext &Ctx) const { |
952 | 1.19k | if (isInt()) { |
953 | 301 | Result = getInt(); |
954 | 301 | return true; |
955 | 301 | } |
956 | | |
957 | 894 | if (isLValue() && isNullPointer()893 ) { |
958 | 453 | Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy); |
959 | 453 | return true; |
960 | 453 | } |
961 | | |
962 | 441 | if (isLValue() && !getLValueBase()440 ) { |
963 | 438 | Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy); |
964 | 438 | return true; |
965 | 438 | } |
966 | | |
967 | 3 | return false; |
968 | 441 | } |
969 | | |
970 | 3.48M | const APValue::LValueBase APValue::getLValueBase() const { |
971 | 3.48M | assert(isLValue() && "Invalid accessor"); |
972 | 3.48M | return ((const LV *)(const void *)&Data)->Base; |
973 | 3.48M | } |
974 | | |
975 | 1.21M | bool APValue::isLValueOnePastTheEnd() const { |
976 | 1.21M | assert(isLValue() && "Invalid accessor"); |
977 | 1.21M | return ((const LV *)(const void *)&Data)->IsOnePastTheEnd; |
978 | 1.21M | } |
979 | | |
980 | 1.22M | CharUnits &APValue::getLValueOffset() { |
981 | 1.22M | assert(isLValue() && "Invalid accessor"); |
982 | 1.22M | return ((LV *)(void *)&Data)->Offset; |
983 | 1.22M | } |
984 | | |
985 | 3.56M | bool APValue::hasLValuePath() const { |
986 | 3.56M | assert(isLValue() && "Invalid accessor"); |
987 | 3.56M | return ((const LV *)(const char *)&Data)->hasPath(); |
988 | 3.56M | } |
989 | | |
990 | 2.32M | ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { |
991 | 2.32M | assert(isLValue() && hasLValuePath() && "Invalid accessor"); |
992 | 2.32M | const LV &LVal = *((const LV *)(const char *)&Data); |
993 | 2.32M | return llvm::ArrayRef(LVal.getPath(), LVal.PathLength); |
994 | 2.32M | } |
995 | | |
996 | 0 | unsigned APValue::getLValueCallIndex() const { |
997 | 0 | assert(isLValue() && "Invalid accessor"); |
998 | 0 | return ((const LV *)(const char *)&Data)->Base.getCallIndex(); |
999 | 0 | } |
1000 | | |
1001 | 0 | unsigned APValue::getLValueVersion() const { |
1002 | 0 | assert(isLValue() && "Invalid accessor"); |
1003 | 0 | return ((const LV *)(const char *)&Data)->Base.getVersion(); |
1004 | 0 | } |
1005 | | |
1006 | 1.22M | bool APValue::isNullPointer() const { |
1007 | 1.22M | assert(isLValue() && "Invalid usage"); |
1008 | 1.22M | return ((const LV *)(const char *)&Data)->IsNullPtr; |
1009 | 1.22M | } |
1010 | | |
1011 | | void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath, |
1012 | 8.84k | bool IsNullPtr) { |
1013 | 8.84k | assert(isLValue() && "Invalid accessor"); |
1014 | 8.84k | LV &LVal = *((LV *)(char *)&Data); |
1015 | 8.84k | LVal.Base = B; |
1016 | 8.84k | LVal.IsOnePastTheEnd = false; |
1017 | 8.84k | LVal.Offset = O; |
1018 | 8.84k | LVal.resizePath((unsigned)-1); |
1019 | 8.84k | LVal.IsNullPtr = IsNullPtr; |
1020 | 8.84k | } |
1021 | | |
1022 | | MutableArrayRef<APValue::LValuePathEntry> |
1023 | | APValue::setLValueUninit(LValueBase B, const CharUnits &O, unsigned Size, |
1024 | 1.83M | bool IsOnePastTheEnd, bool IsNullPtr) { |
1025 | 1.83M | assert(isLValue() && "Invalid accessor"); |
1026 | 1.83M | LV &LVal = *((LV *)(char *)&Data); |
1027 | 1.83M | LVal.Base = B; |
1028 | 1.83M | LVal.IsOnePastTheEnd = IsOnePastTheEnd; |
1029 | 1.83M | LVal.Offset = O; |
1030 | 1.83M | LVal.IsNullPtr = IsNullPtr; |
1031 | 1.83M | LVal.resizePath(Size); |
1032 | 1.83M | return {LVal.getPath(), Size}; |
1033 | 1.83M | } |
1034 | | |
1035 | | void APValue::setLValue(LValueBase B, const CharUnits &O, |
1036 | | ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd, |
1037 | 1.83M | bool IsNullPtr) { |
1038 | 1.83M | MutableArrayRef<APValue::LValuePathEntry> InternalPath = |
1039 | 1.83M | setLValueUninit(B, O, Path.size(), IsOnePastTheEnd, IsNullPtr); |
1040 | 1.83M | if (Path.size()) { |
1041 | 176k | memcpy(InternalPath.data(), Path.data(), |
1042 | 176k | Path.size() * sizeof(LValuePathEntry)); |
1043 | 176k | } |
1044 | 1.83M | } |
1045 | | |
1046 | 3.59k | void APValue::setUnion(const FieldDecl *Field, const APValue &Value) { |
1047 | 3.59k | assert(isUnion() && "Invalid accessor"); |
1048 | 3.59k | ((UnionData *)(char *)&Data)->Field = |
1049 | 3.59k | Field ? Field->getCanonicalDecl()2.50k : nullptr1.08k ; |
1050 | 3.59k | *((UnionData *)(char *)&Data)->Value = Value; |
1051 | 3.59k | } |
1052 | | |
1053 | 5.53k | const ValueDecl *APValue::getMemberPointerDecl() const { |
1054 | 5.53k | assert(isMemberPointer() && "Invalid accessor"); |
1055 | 5.53k | const MemberPointerData &MPD = |
1056 | 5.53k | *((const MemberPointerData *)(const char *)&Data); |
1057 | 5.53k | return MPD.MemberAndIsDerivedMember.getPointer(); |
1058 | 5.53k | } |
1059 | | |
1060 | 1.35k | bool APValue::isMemberPointerToDerivedMember() const { |
1061 | 1.35k | assert(isMemberPointer() && "Invalid accessor"); |
1062 | 1.35k | const MemberPointerData &MPD = |
1063 | 1.35k | *((const MemberPointerData *)(const char *)&Data); |
1064 | 1.35k | return MPD.MemberAndIsDerivedMember.getInt(); |
1065 | 1.35k | } |
1066 | | |
1067 | 1.60k | ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const { |
1068 | 1.60k | assert(isMemberPointer() && "Invalid accessor"); |
1069 | 1.60k | const MemberPointerData &MPD = |
1070 | 1.60k | *((const MemberPointerData *)(const char *)&Data); |
1071 | 1.60k | return llvm::ArrayRef(MPD.getPath(), MPD.PathLength); |
1072 | 1.60k | } |
1073 | | |
1074 | 1.84M | void APValue::MakeLValue() { |
1075 | 1.84M | assert(isAbsent() && "Bad state change"); |
1076 | 1.84M | static_assert(sizeof(LV) <= DataSize, "LV too big"); |
1077 | 1.84M | new ((void *)(char *)&Data) LV(); |
1078 | 1.84M | Kind = LValue; |
1079 | 1.84M | } |
1080 | | |
1081 | 26.5k | void APValue::MakeArray(unsigned InitElts, unsigned Size) { |
1082 | 26.5k | assert(isAbsent() && "Bad state change"); |
1083 | 26.5k | new ((void *)(char *)&Data) Arr(InitElts, Size); |
1084 | 26.5k | Kind = Array; |
1085 | 26.5k | } |
1086 | | |
1087 | | MutableArrayRef<APValue::LValuePathEntry> |
1088 | | setLValueUninit(APValue::LValueBase B, const CharUnits &O, unsigned Size, |
1089 | | bool OnePastTheEnd, bool IsNullPtr); |
1090 | | |
1091 | | MutableArrayRef<const CXXRecordDecl *> |
1092 | | APValue::setMemberPointerUninit(const ValueDecl *Member, bool IsDerivedMember, |
1093 | 4.48k | unsigned Size) { |
1094 | 4.48k | assert(isAbsent() && "Bad state change"); |
1095 | 4.48k | MemberPointerData *MPD = new ((void *)(char *)&Data) MemberPointerData; |
1096 | 4.48k | Kind = MemberPointer; |
1097 | 4.48k | MPD->MemberAndIsDerivedMember.setPointer( |
1098 | 4.48k | Member ? cast<ValueDecl>(Member->getCanonicalDecl())3.87k : nullptr606 ); |
1099 | 4.48k | MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember); |
1100 | 4.48k | MPD->resizePath(Size); |
1101 | 4.48k | return {MPD->getPath(), MPD->PathLength}; |
1102 | 4.48k | } |
1103 | | |
1104 | | void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, |
1105 | 4.47k | ArrayRef<const CXXRecordDecl *> Path) { |
1106 | 4.47k | MutableArrayRef<const CXXRecordDecl *> InternalPath = |
1107 | 4.47k | setMemberPointerUninit(Member, IsDerivedMember, Path.size()); |
1108 | 6.04k | for (unsigned I = 0; I != Path.size(); ++I1.57k ) |
1109 | 1.57k | InternalPath[I] = Path[I]->getCanonicalDecl(); |
1110 | 4.47k | } |
1111 | | |
1112 | | LinkageInfo LinkageComputer::getLVForValue(const APValue &V, |
1113 | 3.93k | LVComputationKind computation) { |
1114 | 3.93k | LinkageInfo LV = LinkageInfo::external(); |
1115 | | |
1116 | 3.93k | auto MergeLV = [&](LinkageInfo MergeLV) { |
1117 | 3.18k | LV.merge(MergeLV); |
1118 | 3.18k | return LV.getLinkage() == Linkage::Internal; |
1119 | 3.18k | }; |
1120 | 3.93k | auto Merge = [&](const APValue &V) { |
1121 | 2.95k | return MergeLV(getLVForValue(V, computation)); |
1122 | 2.95k | }; |
1123 | | |
1124 | 3.93k | switch (V.getKind()) { |
1125 | 120 | case APValue::None: |
1126 | 120 | case APValue::Indeterminate: |
1127 | 1.51k | case APValue::Int: |
1128 | 1.75k | case APValue::Float: |
1129 | 1.75k | case APValue::FixedPoint: |
1130 | 1.83k | case APValue::ComplexInt: |
1131 | 1.91k | case APValue::ComplexFloat: |
1132 | 2.02k | case APValue::Vector: |
1133 | 2.02k | break; |
1134 | | |
1135 | 0 | case APValue::AddrLabelDiff: |
1136 | | // Even for an inline function, it's not reasonable to treat a difference |
1137 | | // between the addresses of labels as an external value. |
1138 | 0 | return LinkageInfo::internal(); |
1139 | | |
1140 | 1.16k | case APValue::Struct: { |
1141 | 1.28k | for (unsigned I = 0, N = V.getStructNumBases(); I != N; ++I120 ) |
1142 | 120 | if (Merge(V.getStructBase(I))) |
1143 | 0 | break; |
1144 | 3.15k | for (unsigned I = 0, N = V.getStructNumFields(); I != N; ++I1.99k ) |
1145 | 1.99k | if (Merge(V.getStructField(I))) |
1146 | 4 | break; |
1147 | 1.16k | break; |
1148 | 1.91k | } |
1149 | | |
1150 | 248 | case APValue::Union: |
1151 | 248 | if (V.getUnionField()) |
1152 | 208 | Merge(V.getUnionValue()); |
1153 | 248 | break; |
1154 | | |
1155 | 134 | case APValue::Array: { |
1156 | 684 | for (unsigned I = 0, N = V.getArrayInitializedElts(); I != N; ++I550 ) |
1157 | 550 | if (Merge(V.getArrayInitializedElt(I))) |
1158 | 0 | break; |
1159 | 134 | if (V.hasArrayFiller()) |
1160 | 75 | Merge(V.getArrayFiller()); |
1161 | 134 | break; |
1162 | 1.91k | } |
1163 | | |
1164 | 184 | case APValue::LValue: { |
1165 | 184 | if (!V.getLValueBase()) { |
1166 | | // Null or absolute address: this is external. |
1167 | 134 | } else if (const auto *VD = |
1168 | 134 | V.getLValueBase().dyn_cast<const ValueDecl *>()) { |
1169 | 134 | if (VD && MergeLV(getLVForDecl(VD, computation))) |
1170 | 4 | break; |
1171 | 134 | } else if (const auto 0 TI0 = V.getLValueBase().dyn_cast<TypeInfoLValue>()) { |
1172 | 0 | if (MergeLV(getLVForType(*TI.getType(), computation))) |
1173 | 0 | break; |
1174 | 0 | } else if (const Expr *E = V.getLValueBase().dyn_cast<const Expr *>()) { |
1175 | | // Almost all expression bases are internal. The exception is |
1176 | | // lifetime-extended temporaries. |
1177 | | // FIXME: These should be modeled as having the |
1178 | | // LifetimeExtendedTemporaryDecl itself as the base. |
1179 | | // FIXME: If we permit Objective-C object literals in template arguments, |
1180 | | // they should not imply internal linkage. |
1181 | 0 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E); |
1182 | 0 | if (!MTE || MTE->getStorageDuration() == SD_FullExpression) |
1183 | 0 | return LinkageInfo::internal(); |
1184 | 0 | if (MergeLV(getLVForDecl(MTE->getExtendingDecl(), computation))) |
1185 | 0 | break; |
1186 | 0 | } else { |
1187 | 0 | assert(V.getLValueBase().is<DynamicAllocLValue>() && |
1188 | 0 | "unexpected LValueBase kind"); |
1189 | 0 | return LinkageInfo::internal(); |
1190 | 0 | } |
1191 | | // The lvalue path doesn't matter: pointers to all subobjects always have |
1192 | | // the same visibility as pointers to the complete object. |
1193 | 180 | break; |
1194 | 184 | } |
1195 | | |
1196 | 180 | case APValue::MemberPointer: |
1197 | 178 | if (const NamedDecl *D = V.getMemberPointerDecl()) |
1198 | 103 | MergeLV(getLVForDecl(D, computation)); |
1199 | | // Note that we could have a base-to-derived conversion here to a member of |
1200 | | // a derived class with less linkage/visibility. That's covered by the |
1201 | | // linkage and visibility of the value's type. |
1202 | 178 | break; |
1203 | 3.93k | } |
1204 | | |
1205 | 3.93k | return LV; |
1206 | 3.93k | } |