Coverage Report

Created: 2019-07-24 05:18

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