Coverage Report

Created: 2020-02-18 08:44

/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 "clang/AST/ASTContext.h"
15
#include "clang/AST/CharUnits.h"
16
#include "clang/AST/DeclCXX.h"
17
#include "clang/AST/Expr.h"
18
#include "clang/AST/Type.h"
19
#include "llvm/Support/ErrorHandling.h"
20
#include "llvm/Support/raw_ostream.h"
21
using namespace clang;
22
23
/// The identity of a type_info object depends on the canonical unqualified
24
/// type only.
25
TypeInfoLValue::TypeInfoLValue(const Type *T)
26
590
    : T(T->getCanonicalTypeUnqualified().getTypePtr()) {}
27
28
void TypeInfoLValue::print(llvm::raw_ostream &Out,
29
44
                           const PrintingPolicy &Policy) const {
30
44
  Out << "typeid(";
31
44
  QualType(getType(), 0).print(Out, Policy);
32
44
  Out << ")";
33
44
}
34
35
static_assert(
36
    1 << llvm::PointerLikeTypeTraits<TypeInfoLValue>::NumLowBitsAvailable <=
37
        alignof(Type),
38
    "Type is insufficiently aligned");
39
40
APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V)
41
23.0M
    : Ptr(P), Local{I, V} {}
42
APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
43
108k
    : Ptr(P), Local{I, V} {}
44
45
APValue::LValueBase APValue::LValueBase::getDynamicAlloc(DynamicAllocLValue LV,
46
576
                                                         QualType Type) {
47
576
  LValueBase Base;
48
576
  Base.Ptr = LV;
49
576
  Base.DynamicAllocType = Type.getAsOpaquePtr();
50
576
  return Base;
51
576
}
52
53
APValue::LValueBase APValue::LValueBase::getTypeInfo(TypeInfoLValue LV,
54
590
                                                     QualType TypeInfo) {
55
590
  LValueBase Base;
56
590
  Base.Ptr = LV;
57
590
  Base.TypeInfoType = TypeInfo.getAsOpaquePtr();
58
590
  return Base;
59
590
}
60
61
6.32M
unsigned APValue::LValueBase::getCallIndex() const {
62
6.32M
  return (is<TypeInfoLValue>() || 
is<DynamicAllocLValue>()6.32M
) ?
03.01k
63
6.32M
                                                            : 
Local.CallIndex6.31M
;
64
6.32M
}
65
66
157k
unsigned APValue::LValueBase::getVersion() const {
67
157k
  return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) ? 
00
: Local.Version;
68
157k
}
69
70
1.27k
QualType APValue::LValueBase::getTypeInfoType() const {
71
1.27k
  assert(is<TypeInfoLValue>() && "not a type_info lvalue");
72
1.27k
  return QualType::getFromOpaquePtr(TypeInfoType);
73
1.27k
}
74
75
7.93k
QualType APValue::LValueBase::getDynamicAllocType() const {
76
7.93k
  assert(is<DynamicAllocLValue>() && "not a dynamic allocation lvalue");
77
7.93k
  return QualType::getFromOpaquePtr(DynamicAllocType);
78
7.93k
}
79
80
namespace clang {
81
bool operator==(const APValue::LValueBase &LHS,
82
1.19M
                const APValue::LValueBase &RHS) {
83
1.19M
  if (LHS.Ptr != RHS.Ptr)
84
280k
    return false;
85
917k
  if (LHS.is<TypeInfoLValue>())
86
0
    return true;
87
917k
  return LHS.Local.CallIndex == RHS.Local.CallIndex &&
88
917k
         
LHS.Local.Version == RHS.Local.Version917k
;
89
917k
}
90
}
91
92
namespace {
93
  struct LVBase {
94
    APValue::LValueBase Base;
95
    CharUnits Offset;
96
    unsigned PathLength;
97
    bool IsNullPtr : 1;
98
    bool IsOnePastTheEnd : 1;
99
  };
100
}
101
102
61.9k
void *APValue::LValueBase::getOpaqueValue() const {
103
61.9k
  return Ptr.getOpaqueValue();
104
61.9k
}
105
106
5.00k
bool APValue::LValueBase::isNull() const {
107
5.00k
  return Ptr.isNull();
108
5.00k
}
109
110
15.8M
APValue::LValueBase::operator bool () const {
111
15.8M
  return static_cast<bool>(Ptr);
112
15.8M
}
113
114
clang::APValue::LValueBase
115
99.7k
llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
116
99.7k
  return clang::APValue::LValueBase(
117
99.7k
      DenseMapInfo<const ValueDecl*>::getEmptyKey());
118
99.7k
}
119
120
clang::APValue::LValueBase
121
87.3k
llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
122
87.3k
  return clang::APValue::LValueBase(
123
87.3k
      DenseMapInfo<const ValueDecl*>::getTombstoneKey());
124
87.3k
}
125
126
namespace clang {
127
57.6k
llvm::hash_code hash_value(const APValue::LValueBase &Base) {
128
57.6k
  if (Base.is<TypeInfoLValue>() || Base.is<DynamicAllocLValue>())
129
401
    return llvm::hash_value(Base.getOpaqueValue());
130
57.2k
  return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(),
131
57.2k
                            Base.getVersion());
132
57.2k
}
133
}
134
135
unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue(
136
0
    const clang::APValue::LValueBase &Base) {
137
0
  return hash_value(Base);
138
0
}
139
140
bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual(
141
    const clang::APValue::LValueBase &LHS,
142
0
    const clang::APValue::LValueBase &RHS) {
143
0
  return LHS == RHS;
144
0
}
145
146
struct APValue::LV : LVBase {
147
  static const unsigned InlinePathSpace =
148
      (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
149
150
  /// Path - The sequence of base classes, fields and array indices to follow to
151
  /// walk from Base to the subobject. When performing GCC-style folding, there
152
  /// may not be such a path.
153
  union {
154
    LValuePathEntry Path[InlinePathSpace];
155
    LValuePathEntry *PathPtr;
156
  };
157
158
532k
  LV() { PathLength = (unsigned)-1; }
159
524k
  ~LV() { resizePath(0); }
160
161
1.05M
  void resizePath(unsigned Length) {
162
1.05M
    if (Length == PathLength)
163
471k
      return;
164
585k
    if (hasPathPtr())
165
51
      delete [] PathPtr;
166
585k
    PathLength = Length;
167
585k
    if (hasPathPtr())
168
51
      PathPtr = new LValuePathEntry[Length];
169
585k
  }
170
171
3.75M
  bool hasPath() const { return PathLength != (unsigned)-1; }
172
2.51M
  bool hasPathPtr() const { return hasPath() && 
PathLength > InlinePathSpace1.98M
; }
173
174
528k
  LValuePathEntry *getPath() { return hasPathPtr() ? 
PathPtr51
:
Path528k
; }
175
811k
  const LValuePathEntry *getPath() const {
176
811k
    return hasPathPtr() ? 
PathPtr110
:
Path811k
;
177
811k
  }
178
};
179
180
namespace {
181
  struct MemberPointerBase {
182
    llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
183
    unsigned PathLength;
184
  };
185
}
186
187
struct APValue::MemberPointerData : MemberPointerBase {
188
  static const unsigned InlinePathSpace =
189
      (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
190
  typedef const CXXRecordDecl *PathElem;
191
  union {
192
    PathElem Path[InlinePathSpace];
193
    PathElem *PathPtr;
194
  };
195
196
1.36k
  MemberPointerData() { PathLength = 0; }
197
1.10k
  ~MemberPointerData() { resizePath(0); }
198
199
2.46k
  void resizePath(unsigned Length) {
200
2.46k
    if (Length == PathLength)
201
2.05k
      return;
202
403
    if (hasPathPtr())
203
19
      delete [] PathPtr;
204
403
    PathLength = Length;
205
403
    if (hasPathPtr())
206
19
      PathPtr = new PathElem[Length];
207
403
  }
208
209
3.01k
  bool hasPathPtr() const { return PathLength > InlinePathSpace; }
210
211
1.36k
  PathElem *getPath() { return hasPathPtr() ? 
PathPtr19
:
Path1.34k
; }
212
579
  const PathElem *getPath() const {
213
579
    return hasPathPtr() ? 
PathPtr32
:
Path547
;
214
579
  }
215
};
216
217
// FIXME: Reduce the malloc traffic here.
218
219
APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
220
  Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
221
7.07k
  NumElts(NumElts), ArrSize(Size) {}
222
5.99k
APValue::Arr::~Arr() { delete [] Elts; }
223
224
APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
225
  Elts(new APValue[NumBases+NumFields]),
226
24.1k
  NumBases(NumBases), NumFields(NumFields) {}
227
16.5k
APValue::StructData::~StructData() {
228
16.5k
  delete [] Elts;
229
16.5k
}
230
231
1.67k
APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
232
1.36k
APValue::UnionData::~UnionData () {
233
1.36k
  delete Value;
234
1.36k
}
235
236
5.13M
APValue::APValue(const APValue &RHS) : Kind(None) {
237
5.13M
  switch (RHS.getKind()) {
238
90.5k
  case None:
239
90.5k
  case Indeterminate:
240
90.5k
    Kind = RHS.getKind();
241
90.5k
    break;
242
5.01M
  case Int:
243
5.01M
    MakeInt();
244
5.01M
    setInt(RHS.getInt());
245
5.01M
    break;
246
90.5k
  case Float:
247
6.91k
    MakeFloat();
248
6.91k
    setFloat(RHS.getFloat());
249
6.91k
    break;
250
90.5k
  case FixedPoint: {
251
0
    APFixedPoint FXCopy = RHS.getFixedPoint();
252
0
    MakeFixedPoint(std::move(FXCopy));
253
0
    break;
254
90.5k
  }
255
90.5k
  case Vector:
256
758
    MakeVector();
257
758
    setVector(((const Vec *)(const char *)RHS.Data.buffer)->Elts,
258
758
              RHS.getVectorLength());
259
758
    break;
260
90.5k
  case ComplexInt:
261
1
    MakeComplexInt();
262
1
    setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
263
1
    break;
264
90.5k
  case ComplexFloat:
265
10
    MakeComplexFloat();
266
10
    setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
267
10
    break;
268
90.5k
  case LValue:
269
24.8k
    MakeLValue();
270
24.8k
    if (RHS.hasLValuePath())
271
24.5k
      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
272
24.5k
                RHS.isLValueOnePastTheEnd(), RHS.isNullPointer());
273
301
    else
274
301
      setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
275
301
                RHS.isNullPointer());
276
24.8k
    break;
277
90.5k
  case Array:
278
601
    MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
279
3.40k
    for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; 
++I2.79k
)
280
2.79k
      getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
281
601
    if (RHS.hasArrayFiller())
282
67
      getArrayFiller() = RHS.getArrayFiller();
283
601
    break;
284
90.5k
  case Struct:
285
3.42k
    MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
286
3.67k
    for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; 
++I245
)
287
245
      getStructBase(I) = RHS.getStructBase(I);
288
7.83k
    for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; 
++I4.41k
)
289
4.41k
      getStructField(I) = RHS.getStructField(I);
290
3.42k
    break;
291
90.5k
  case Union:
292
99
    MakeUnion();
293
99
    setUnion(RHS.getUnionField(), RHS.getUnionValue());
294
99
    break;
295
90.5k
  case MemberPointer:
296
186
    MakeMemberPointer(RHS.getMemberPointerDecl(),
297
186
                      RHS.isMemberPointerToDerivedMember(),
298
186
                      RHS.getMemberPointerPath());
299
186
    break;
300
90.5k
  case AddrLabelDiff:
301
17
    MakeAddrLabelDiff();
302
17
    setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
303
17
    break;
304
5.13M
  }
305
5.13M
}
306
307
29.1M
void APValue::DestroyDataAndMakeUninit() {
308
29.1M
  if (Kind == Int)
309
28.5M
    ((APSInt*)(char*)Data.buffer)->~APSInt();
310
595k
  else if (Kind == Float)
311
42.3k
    ((APFloat*)(char*)Data.buffer)->~APFloat();
312
553k
  else if (Kind == FixedPoint)
313
1.00k
    ((APFixedPoint *)(char *)Data.buffer)->~APFixedPoint();
314
552k
  else if (Kind == Vector)
315
2.56k
    ((Vec*)(char*)Data.buffer)->~Vec();
316
549k
  else if (Kind == ComplexInt)
317
41
    ((ComplexAPSInt*)(char*)Data.buffer)->~ComplexAPSInt();
318
549k
  else if (Kind == ComplexFloat)
319
123
    ((ComplexAPFloat*)(char*)Data.buffer)->~ComplexAPFloat();
320
549k
  else if (Kind == LValue)
321
524k
    ((LV*)(char*)Data.buffer)->~LV();
322
25.0k
  else if (Kind == Array)
323
5.99k
    ((Arr*)(char*)Data.buffer)->~Arr();
324
19.0k
  else if (Kind == Struct)
325
16.5k
    ((StructData*)(char*)Data.buffer)->~StructData();
326
2.51k
  else if (Kind == Union)
327
1.36k
    ((UnionData*)(char*)Data.buffer)->~UnionData();
328
1.14k
  else if (Kind == MemberPointer)
329
1.10k
    ((MemberPointerData*)(char*)Data.buffer)->~MemberPointerData();
330
45
  else if (Kind == AddrLabelDiff)
331
45
    ((AddrLabelDiffData*)(char*)Data.buffer)->~AddrLabelDiffData();
332
29.1M
  Kind = None;
333
29.1M
}
334
335
284k
bool APValue::needsCleanup() const {
336
284k
  switch (getKind()) {
337
7
  case None:
338
7
  case Indeterminate:
339
7
  case AddrLabelDiff:
340
7
    return false;
341
11.1k
  case Struct:
342
11.1k
  case Union:
343
11.1k
  case Array:
344
11.1k
  case Vector:
345
11.1k
    return true;
346
265k
  case Int:
347
265k
    return getInt().needsCleanup();
348
11.1k
  case Float:
349
520
    return getFloat().needsCleanup();
350
11.1k
  case FixedPoint:
351
152
    return getFixedPoint().getValue().needsCleanup();
352
11.1k
  case ComplexFloat:
353
22
    assert(getComplexFloatImag().needsCleanup() ==
354
22
               getComplexFloatReal().needsCleanup() &&
355
22
           "In _Complex float types, real and imaginary values always have the "
356
22
           "same size.");
357
22
    return getComplexFloatReal().needsCleanup();
358
11.1k
  case ComplexInt:
359
8
    assert(getComplexIntImag().needsCleanup() ==
360
8
               getComplexIntReal().needsCleanup() &&
361
8
           "In _Complex int types, real and imaginary values must have the "
362
8
           "same size.");
363
8
    return getComplexIntReal().needsCleanup();
364
11.1k
  case LValue:
365
6.53k
    return reinterpret_cast<const LV *>(Data.buffer)->hasPathPtr();
366
11.1k
  case MemberPointer:
367
264
    return reinterpret_cast<const MemberPointerData *>(Data.buffer)
368
264
        ->hasPathPtr();
369
0
  }
370
0
  llvm_unreachable("Unknown APValue kind!");
371
0
}
372
373
40.2M
void APValue::swap(APValue &RHS) {
374
40.2M
  std::swap(Kind, RHS.Kind);
375
40.2M
  char TmpData[DataSize];
376
40.2M
  memcpy(TmpData, Data.buffer, DataSize);
377
40.2M
  memcpy(Data.buffer, RHS.Data.buffer, DataSize);
378
40.2M
  memcpy(RHS.Data.buffer, TmpData, DataSize);
379
40.2M
}
380
381
0
LLVM_DUMP_METHOD void APValue::dump() const {
382
0
  dump(llvm::errs());
383
0
  llvm::errs() << '\n';
384
0
}
385
386
19
static double GetApproxValue(const llvm::APFloat &F) {
387
19
  llvm::APFloat V = F;
388
19
  bool ignored;
389
19
  V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven,
390
19
            &ignored);
391
19
  return V.convertToDouble();
392
19
}
393
394
62
void APValue::dump(raw_ostream &OS) const {
395
62
  switch (getKind()) {
396
0
  case None:
397
0
    OS << "None";
398
0
    return;
399
0
  case Indeterminate:
400
0
    OS << "Indeterminate";
401
0
    return;
402
62
  case Int:
403
62
    OS << "Int: " << getInt();
404
62
    return;
405
0
  case Float:
406
0
    OS << "Float: " << GetApproxValue(getFloat());
407
0
    return;
408
0
  case FixedPoint:
409
0
    OS << "FixedPoint : " << getFixedPoint();
410
0
    return;
411
0
  case Vector:
412
0
    OS << "Vector: ";
413
0
    getVectorElt(0).dump(OS);
414
0
    for (unsigned i = 1; i != getVectorLength(); ++i) {
415
0
      OS << ", ";
416
0
      getVectorElt(i).dump(OS);
417
0
    }
418
0
    return;
419
0
  case ComplexInt:
420
0
    OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
421
0
    return;
422
0
  case ComplexFloat:
423
0
    OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
424
0
       << ", " << GetApproxValue(getComplexFloatImag());
425
0
    return;
426
0
  case LValue:
427
0
    OS << "LValue: <todo>";
428
0
    return;
429
0
  case Array:
430
0
    OS << "Array: ";
431
0
    for (unsigned I = 0, N = getArrayInitializedElts(); I != N; ++I) {
432
0
      getArrayInitializedElt(I).dump(OS);
433
0
      if (I != getArraySize() - 1) OS << ", ";
434
0
    }
435
0
    if (hasArrayFiller()) {
436
0
      OS << getArraySize() - getArrayInitializedElts() << " x ";
437
0
      getArrayFiller().dump(OS);
438
0
    }
439
0
    return;
440
0
  case Struct:
441
0
    OS << "Struct ";
442
0
    if (unsigned N = getStructNumBases()) {
443
0
      OS << " bases: ";
444
0
      getStructBase(0).dump(OS);
445
0
      for (unsigned I = 1; I != N; ++I) {
446
0
        OS << ", ";
447
0
        getStructBase(I).dump(OS);
448
0
      }
449
0
    }
450
0
    if (unsigned N = getStructNumFields()) {
451
0
      OS << " fields: ";
452
0
      getStructField(0).dump(OS);
453
0
      for (unsigned I = 1; I != N; ++I) {
454
0
        OS << ", ";
455
0
        getStructField(I).dump(OS);
456
0
      }
457
0
    }
458
0
    return;
459
0
  case Union:
460
0
    OS << "Union: ";
461
0
    getUnionValue().dump(OS);
462
0
    return;
463
0
  case MemberPointer:
464
0
    OS << "MemberPointer: <todo>";
465
0
    return;
466
0
  case AddrLabelDiff:
467
0
    OS << "AddrLabelDiff: <todo>";
468
0
    return;
469
0
  }
470
0
  llvm_unreachable("Unknown APValue kind!");
471
0
}
472
473
void APValue::printPretty(raw_ostream &Out, const ASTContext &Ctx,
474
2.01k
                          QualType Ty) const {
475
2.01k
  switch (getKind()) {
476
0
  case APValue::None:
477
0
    Out << "<out of lifetime>";
478
0
    return;
479
4
  case APValue::Indeterminate:
480
4
    Out << "<uninitialized>";
481
4
    return;
482
1.10k
  case APValue::Int:
483
1.10k
    if (Ty->isBooleanType())
484
18
      Out << (getInt().getBoolValue() ? 
"true"11
:
"false"7
);
485
1.08k
    else
486
1.08k
      Out << getInt();
487
1.10k
    return;
488
15
  case APValue::Float:
489
15
    Out << GetApproxValue(getFloat());
490
15
    return;
491
0
  case APValue::FixedPoint:
492
0
    Out << getFixedPoint();
493
0
    return;
494
2
  case APValue::Vector: {
495
2
    Out << '{';
496
2
    QualType ElemTy = Ty->castAs<VectorType>()->getElementType();
497
2
    getVectorElt(0).printPretty(Out, Ctx, ElemTy);
498
8
    for (unsigned i = 1; i != getVectorLength(); 
++i6
) {
499
6
      Out << ", ";
500
6
      getVectorElt(i).printPretty(Out, Ctx, ElemTy);
501
6
    }
502
2
    Out << '}';
503
2
    return;
504
0
  }
505
2
  case APValue::ComplexInt:
506
2
    Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
507
2
    return;
508
2
  case APValue::ComplexFloat:
509
2
    Out << GetApproxValue(getComplexFloatReal()) << "+"
510
2
        << GetApproxValue(getComplexFloatImag()) << "i";
511
2
    return;
512
848
  case APValue::LValue: {
513
848
    bool IsReference = Ty->isReferenceType();
514
848
    QualType InnerTy
515
848
      = IsReference ? 
Ty.getNonReferenceType()350
:
Ty->getPointeeType()498
;
516
848
    if (InnerTy.isNull())
517
347
      InnerTy = Ty;
518
848
519
848
    LValueBase Base = getLValueBase();
520
848
    if (!Base) {
521
90
      if (isNullPointer()) {
522
74
        Out << (Ctx.getLangOpts().CPlusPlus11 ? "nullptr" : 
"0"0
);
523
74
      } else 
if (16
IsReference16
) {
524
0
        Out << "*(" << InnerTy.stream(Ctx.getPrintingPolicy()) << "*)"
525
0
            << getLValueOffset().getQuantity();
526
16
      } else {
527
16
        Out << "(" << Ty.stream(Ctx.getPrintingPolicy()) << ")"
528
16
            << getLValueOffset().getQuantity();
529
16
      }
530
90
      return;
531
90
    }
532
758
533
758
    if (!hasLValuePath()) {
534
0
      // No lvalue path: just print the offset.
535
0
      CharUnits O = getLValueOffset();
536
0
      CharUnits S = Ctx.getTypeSizeInChars(InnerTy);
537
0
      if (!O.isZero()) {
538
0
        if (IsReference)
539
0
          Out << "*(";
540
0
        if (O % S) {
541
0
          Out << "(char*)";
542
0
          S = CharUnits::One();
543
0
        }
544
0
        Out << '&';
545
0
      } else if (!IsReference) {
546
0
        Out << '&';
547
0
      }
548
0
549
0
      if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
550
0
        Out << *VD;
551
0
      else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
552
0
        TI.print(Out, Ctx.getPrintingPolicy());
553
0
      } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
554
0
        Out << "{*new "
555
0
            << Base.getDynamicAllocType().stream(Ctx.getPrintingPolicy()) << "#"
556
0
            << DA.getIndex() << "}";
557
0
      } else {
558
0
        assert(Base.get<const Expr *>() != nullptr &&
559
0
               "Expecting non-null Expr");
560
0
        Base.get<const Expr*>()->printPretty(Out, nullptr,
561
0
                                             Ctx.getPrintingPolicy());
562
0
      }
563
0
564
0
      if (!O.isZero()) {
565
0
        Out << " + " << (O / S);
566
0
        if (IsReference)
567
0
          Out << ')';
568
0
      }
569
0
      return;
570
0
    }
571
758
572
758
    // We have an lvalue path. Print it out nicely.
573
758
    if (!IsReference)
574
408
      Out << '&';
575
350
    else if (isLValueOnePastTheEnd())
576
0
      Out << "*(&";
577
758
578
758
    QualType ElemTy;
579
758
    if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
580
599
      Out << *VD;
581
599
      ElemTy = VD->getType();
582
599
    } else 
if (TypeInfoLValue 159
TI159
= Base.dyn_cast<TypeInfoLValue>()) {
583
44
      TI.print(Out, Ctx.getPrintingPolicy());
584
44
      ElemTy = Base.getTypeInfoType();
585
115
    } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
586
16
      Out << "{*new "
587
16
          << Base.getDynamicAllocType().stream(Ctx.getPrintingPolicy()) << "#"
588
16
          << DA.getIndex() << "}";
589
16
      ElemTy = Base.getDynamicAllocType();
590
99
    } else {
591
99
      const Expr *E = Base.get<const Expr*>();
592
99
      assert(E != nullptr && "Expecting non-null Expr");
593
99
      E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
594
99
      // FIXME: This is wrong if E is a MaterializeTemporaryExpr with an lvalue
595
99
      // adjustment.
596
99
      ElemTy = E->getType();
597
99
    }
598
758
599
758
    ArrayRef<LValuePathEntry> Path = getLValuePath();
600
758
    const CXXRecordDecl *CastToBase = nullptr;
601
851
    for (unsigned I = 0, N = Path.size(); I != N; 
++I93
) {
602
93
      if (ElemTy->getAs<RecordType>()) {
603
45
        // The lvalue refers to a class type, so the next path entry is a base
604
45
        // or member.
605
45
        const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer();
606
45
        if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
607
20
          CastToBase = RD;
608
20
          ElemTy = Ctx.getRecordType(RD);
609
25
        } else {
610
25
          const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
611
25
          Out << ".";
612
25
          if (CastToBase)
613
4
            Out << *CastToBase << "::";
614
25
          Out << *VD;
615
25
          ElemTy = VD->getType();
616
25
        }
617
48
      } else {
618
48
        // The lvalue must refer to an array.
619
48
        Out << '[' << Path[I].getAsArrayIndex() << ']';
620
48
        ElemTy = Ctx.getAsArrayType(ElemTy)->getElementType();
621
48
      }
622
93
    }
623
758
624
758
    // Handle formatting of one-past-the-end lvalues.
625
758
    if (isLValueOnePastTheEnd()) {
626
2
      // FIXME: If CastToBase is non-0, we should prefix the output with
627
2
      // "(CastToBase*)".
628
2
      Out << " + 1";
629
2
      if (IsReference)
630
0
        Out << ')';
631
2
    }
632
758
    return;
633
758
  }
634
758
  case APValue::Array: {
635
6
    const ArrayType *AT = Ctx.getAsArrayType(Ty);
636
6
    QualType ElemTy = AT->getElementType();
637
6
    Out << '{';
638
6
    if (unsigned N = getArrayInitializedElts()) {
639
2
      getArrayInitializedElt(0).printPretty(Out, Ctx, ElemTy);
640
20
      for (unsigned I = 1; I != N; 
++I18
) {
641
20
        Out << ", ";
642
20
        if (I == 10) {
643
2
          // Avoid printing out the entire contents of large arrays.
644
2
          Out << "...";
645
2
          break;
646
2
        }
647
18
        getArrayInitializedElt(I).printPretty(Out, Ctx, ElemTy);
648
18
      }
649
2
    }
650
6
    Out << '}';
651
6
    return;
652
758
  }
653
758
  case APValue::Struct: {
654
19
    Out << '{';
655
19
    const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl();
656
19
    bool First = true;
657
19
    if (unsigned N = getStructNumBases()) {
658
2
      const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
659
2
      CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
660
4
      for (unsigned I = 0; I != N; 
++I, ++BI2
) {
661
2
        assert(BI != CD->bases_end());
662
2
        if (!First)
663
0
          Out << ", ";
664
2
        getStructBase(I).printPretty(Out, Ctx, BI->getType());
665
2
        First = false;
666
2
      }
667
2
    }
668
22
    for (const auto *FI : RD->fields()) {
669
22
      if (!First)
670
8
        Out << ", ";
671
22
      if (FI->isUnnamedBitfield()) 
continue0
;
672
22
      getStructField(FI->getFieldIndex()).
673
22
        printPretty(Out, Ctx, FI->getType());
674
22
      First = false;
675
22
    }
676
19
    Out << '}';
677
19
    return;
678
758
  }
679
758
  case APValue::Union:
680
2
    Out << '{';
681
2
    if (const FieldDecl *FD = getUnionField()) {
682
2
      Out << "." << *FD << " = ";
683
2
      getUnionValue().printPretty(Out, Ctx, FD->getType());
684
2
    }
685
2
    Out << '}';
686
2
    return;
687
758
  case APValue::MemberPointer:
688
2
    // FIXME: This is not enough to unambiguously identify the member in a
689
2
    // multiple-inheritance scenario.
690
2
    if (const ValueDecl *VD = getMemberPointerDecl()) {
691
2
      Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
692
2
      return;
693
2
    }
694
0
    Out << "0";
695
0
    return;
696
2
  case APValue::AddrLabelDiff:
697
2
    Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
698
2
    Out << " - ";
699
2
    Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
700
2
    return;
701
0
  }
702
0
  llvm_unreachable("Unknown APValue kind!");
703
0
}
704
705
469
std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const {
706
469
  std::string Result;
707
469
  llvm::raw_string_ostream Out(Result);
708
469
  printPretty(Out, Ctx, Ty);
709
469
  Out.flush();
710
469
  return Result;
711
469
}
712
713
bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy,
714
659
                                 const ASTContext &Ctx) const {
715
659
  if (isInt()) {
716
249
    Result = getInt();
717
249
    return true;
718
249
  }
719
410
720
410
  if (isLValue() && isNullPointer()) {
721
198
    Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy);
722
198
    return true;
723
198
  }
724
212
725
212
  if (isLValue() && !getLValueBase()) {
726
211
    Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy);
727
211
    return true;
728
211
  }
729
1
730
1
  return false;
731
1
}
732
733
1.23M
const APValue::LValueBase APValue::getLValueBase() const {
734
1.23M
  assert(isLValue() && "Invalid accessor");
735
1.23M
  return ((const LV*)(const void*)Data.buffer)->Base;
736
1.23M
}
737
738
422k
bool APValue::isLValueOnePastTheEnd() const {
739
422k
  assert(isLValue() && "Invalid accessor");
740
422k
  return ((const LV*)(const void*)Data.buffer)->IsOnePastTheEnd;
741
422k
}
742
743
433k
CharUnits &APValue::getLValueOffset() {
744
433k
  assert(isLValue() && "Invalid accessor");
745
433k
  return ((LV*)(void*)Data.buffer)->Offset;
746
433k
}
747
748
1.23M
bool APValue::hasLValuePath() const {
749
1.23M
  assert(isLValue() && "Invalid accessor");
750
1.23M
  return ((const LV*)(const char*)Data.buffer)->hasPath();
751
1.23M
}
752
753
811k
ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
754
811k
  assert(isLValue() && hasLValuePath() && "Invalid accessor");
755
811k
  const LV &LVal = *((const LV*)(const char*)Data.buffer);
756
811k
  return llvm::makeArrayRef(LVal.getPath(), LVal.PathLength);
757
811k
}
758
759
0
unsigned APValue::getLValueCallIndex() const {
760
0
  assert(isLValue() && "Invalid accessor");
761
0
  return ((const LV*)(const char*)Data.buffer)->Base.getCallIndex();
762
0
}
763
764
0
unsigned APValue::getLValueVersion() const {
765
0
  assert(isLValue() && "Invalid accessor");
766
0
  return ((const LV*)(const char*)Data.buffer)->Base.getVersion();
767
0
}
768
769
425k
bool APValue::isNullPointer() const {
770
425k
  assert(isLValue() && "Invalid usage");
771
425k
  return ((const LV*)(const char*)Data.buffer)->IsNullPtr;
772
425k
}
773
774
void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
775
3.69k
                        bool IsNullPtr) {
776
3.69k
  assert(isLValue() && "Invalid accessor");
777
3.69k
  LV &LVal = *((LV*)(char*)Data.buffer);
778
3.69k
  LVal.Base = B;
779
3.69k
  LVal.IsOnePastTheEnd = false;
780
3.69k
  LVal.Offset = O;
781
3.69k
  LVal.resizePath((unsigned)-1);
782
3.69k
  LVal.IsNullPtr = IsNullPtr;
783
3.69k
}
784
785
void APValue::setLValue(LValueBase B, const CharUnits &O,
786
                        ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
787
528k
                        bool IsNullPtr) {
788
528k
  assert(isLValue() && "Invalid accessor");
789
528k
  LV &LVal = *((LV*)(char*)Data.buffer);
790
528k
  LVal.Base = B;
791
528k
  LVal.IsOnePastTheEnd = IsOnePastTheEnd;
792
528k
  LVal.Offset = O;
793
528k
  LVal.resizePath(Path.size());
794
528k
  memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry));
795
528k
  LVal.IsNullPtr = IsNullPtr;
796
528k
}
797
798
1.93k
const ValueDecl *APValue::getMemberPointerDecl() const {
799
1.93k
  assert(isMemberPointer() && "Invalid accessor");
800
1.93k
  const MemberPointerData &MPD =
801
1.93k
      *((const MemberPointerData *)(const char *)Data.buffer);
802
1.93k
  return MPD.MemberAndIsDerivedMember.getPointer();
803
1.93k
}
804
805
514
bool APValue::isMemberPointerToDerivedMember() const {
806
514
  assert(isMemberPointer() && "Invalid accessor");
807
514
  const MemberPointerData &MPD =
808
514
      *((const MemberPointerData *)(const char *)Data.buffer);
809
514
  return MPD.MemberAndIsDerivedMember.getInt();
810
514
}
811
812
579
ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
813
579
  assert(isMemberPointer() && "Invalid accessor");
814
579
  const MemberPointerData &MPD =
815
579
      *((const MemberPointerData *)(const char *)Data.buffer);
816
579
  return llvm::makeArrayRef(MPD.getPath(), MPD.PathLength);
817
579
}
818
819
532k
void APValue::MakeLValue() {
820
532k
  assert(isAbsent() && "Bad state change");
821
532k
  static_assert(sizeof(LV) <= DataSize, "LV too big");
822
532k
  new ((void*)(char*)Data.buffer) LV();
823
532k
  Kind = LValue;
824
532k
}
825
826
7.07k
void APValue::MakeArray(unsigned InitElts, unsigned Size) {
827
7.07k
  assert(isAbsent() && "Bad state change");
828
7.07k
  new ((void*)(char*)Data.buffer) Arr(InitElts, Size);
829
7.07k
  Kind = Array;
830
7.07k
}
831
832
void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
833
1.36k
                                ArrayRef<const CXXRecordDecl*> Path) {
834
1.36k
  assert(isAbsent() && "Bad state change");
835
1.36k
  MemberPointerData *MPD = new ((void*)(char*)Data.buffer) MemberPointerData;
836
1.36k
  Kind = MemberPointer;
837
1.36k
  MPD->MemberAndIsDerivedMember.setPointer(Member);
838
1.36k
  MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
839
1.36k
  MPD->resizePath(Path.size());
840
1.36k
  memcpy(MPD->getPath(), Path.data(), Path.size()*sizeof(const CXXRecordDecl*));
841
1.36k
}