Skip to content

Commit 5b267fb

Browse files
committed
ADT: Stop peeking inside AlignedCharArrayUnion, NFC
Update all the users of `AlignedCharArrayUnion` to stop peeking inside (to look at `buffer`) so that a follow-up patch can replace it with an alias to `std::aligned_union_t`. This was reviewed as part of https://reviews.llvm.org/D92512, but I'm splitting this bit out to commit first to reduce churn in case the change to `AlignedCharArrayUnion` needs to be reverted for some unexpected reason.
1 parent f9c3954 commit 5b267fb

File tree

14 files changed

+125
-134
lines changed

14 files changed

+125
-134
lines changed

clang/include/clang/AST/APValue.h

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class APValue {
402402

403403
APSInt &getInt() {
404404
assert(isInt() && "Invalid accessor");
405-
return *(APSInt*)(char*)Data.buffer;
405+
return *(APSInt *)(char *)&Data;
406406
}
407407
const APSInt &getInt() const {
408408
return const_cast<APValue*>(this)->getInt();
@@ -416,47 +416,47 @@ class APValue {
416416

417417
APFloat &getFloat() {
418418
assert(isFloat() && "Invalid accessor");
419-
return *(APFloat*)(char*)Data.buffer;
419+
return *(APFloat *)(char *)&Data;
420420
}
421421
const APFloat &getFloat() const {
422422
return const_cast<APValue*>(this)->getFloat();
423423
}
424424

425425
APFixedPoint &getFixedPoint() {
426426
assert(isFixedPoint() && "Invalid accessor");
427-
return *(APFixedPoint *)(char *)Data.buffer;
427+
return *(APFixedPoint *)(char *)&Data;
428428
}
429429
const APFixedPoint &getFixedPoint() const {
430430
return const_cast<APValue *>(this)->getFixedPoint();
431431
}
432432

433433
APSInt &getComplexIntReal() {
434434
assert(isComplexInt() && "Invalid accessor");
435-
return ((ComplexAPSInt*)(char*)Data.buffer)->Real;
435+
return ((ComplexAPSInt *)(char *)&Data)->Real;
436436
}
437437
const APSInt &getComplexIntReal() const {
438438
return const_cast<APValue*>(this)->getComplexIntReal();
439439
}
440440

441441
APSInt &getComplexIntImag() {
442442
assert(isComplexInt() && "Invalid accessor");
443-
return ((ComplexAPSInt*)(char*)Data.buffer)->Imag;
443+
return ((ComplexAPSInt *)(char *)&Data)->Imag;
444444
}
445445
const APSInt &getComplexIntImag() const {
446446
return const_cast<APValue*>(this)->getComplexIntImag();
447447
}
448448

449449
APFloat &getComplexFloatReal() {
450450
assert(isComplexFloat() && "Invalid accessor");
451-
return ((ComplexAPFloat*)(char*)Data.buffer)->Real;
451+
return ((ComplexAPFloat *)(char *)&Data)->Real;
452452
}
453453
const APFloat &getComplexFloatReal() const {
454454
return const_cast<APValue*>(this)->getComplexFloatReal();
455455
}
456456

457457
APFloat &getComplexFloatImag() {
458458
assert(isComplexFloat() && "Invalid accessor");
459-
return ((ComplexAPFloat*)(char*)Data.buffer)->Imag;
459+
return ((ComplexAPFloat *)(char *)&Data)->Imag;
460460
}
461461
const APFloat &getComplexFloatImag() const {
462462
return const_cast<APValue*>(this)->getComplexFloatImag();
@@ -477,20 +477,20 @@ class APValue {
477477
APValue &getVectorElt(unsigned I) {
478478
assert(isVector() && "Invalid accessor");
479479
assert(I < getVectorLength() && "Index out of range");
480-
return ((Vec*)(char*)Data.buffer)->Elts[I];
480+
return ((Vec *)(char *)&Data)->Elts[I];
481481
}
482482
const APValue &getVectorElt(unsigned I) const {
483483
return const_cast<APValue*>(this)->getVectorElt(I);
484484
}
485485
unsigned getVectorLength() const {
486486
assert(isVector() && "Invalid accessor");
487-
return ((const Vec*)(const void *)Data.buffer)->NumElts;
487+
return ((const Vec *)(const void *)&Data)->NumElts;
488488
}
489489

490490
APValue &getArrayInitializedElt(unsigned I) {
491491
assert(isArray() && "Invalid accessor");
492492
assert(I < getArrayInitializedElts() && "Index out of range");
493-
return ((Arr*)(char*)Data.buffer)->Elts[I];
493+
return ((Arr *)(char *)&Data)->Elts[I];
494494
}
495495
const APValue &getArrayInitializedElt(unsigned I) const {
496496
return const_cast<APValue*>(this)->getArrayInitializedElt(I);
@@ -501,35 +501,35 @@ class APValue {
501501
APValue &getArrayFiller() {
502502
assert(isArray() && "Invalid accessor");
503503
assert(hasArrayFiller() && "No array filler");
504-
return ((Arr*)(char*)Data.buffer)->Elts[getArrayInitializedElts()];
504+
return ((Arr *)(char *)&Data)->Elts[getArrayInitializedElts()];
505505
}
506506
const APValue &getArrayFiller() const {
507507
return const_cast<APValue*>(this)->getArrayFiller();
508508
}
509509
unsigned getArrayInitializedElts() const {
510510
assert(isArray() && "Invalid accessor");
511-
return ((const Arr*)(const void *)Data.buffer)->NumElts;
511+
return ((const Arr *)(const void *)&Data)->NumElts;
512512
}
513513
unsigned getArraySize() const {
514514
assert(isArray() && "Invalid accessor");
515-
return ((const Arr*)(const void *)Data.buffer)->ArrSize;
515+
return ((const Arr *)(const void *)&Data)->ArrSize;
516516
}
517517

518518
unsigned getStructNumBases() const {
519519
assert(isStruct() && "Invalid accessor");
520-
return ((const StructData*)(const char*)Data.buffer)->NumBases;
520+
return ((const StructData *)(const char *)&Data)->NumBases;
521521
}
522522
unsigned getStructNumFields() const {
523523
assert(isStruct() && "Invalid accessor");
524-
return ((const StructData*)(const char*)Data.buffer)->NumFields;
524+
return ((const StructData *)(const char *)&Data)->NumFields;
525525
}
526526
APValue &getStructBase(unsigned i) {
527527
assert(isStruct() && "Invalid accessor");
528-
return ((StructData*)(char*)Data.buffer)->Elts[i];
528+
return ((StructData *)(char *)&Data)->Elts[i];
529529
}
530530
APValue &getStructField(unsigned i) {
531531
assert(isStruct() && "Invalid accessor");
532-
return ((StructData*)(char*)Data.buffer)->Elts[getStructNumBases() + i];
532+
return ((StructData *)(char *)&Data)->Elts[getStructNumBases() + i];
533533
}
534534
const APValue &getStructBase(unsigned i) const {
535535
return const_cast<APValue*>(this)->getStructBase(i);
@@ -540,11 +540,11 @@ class APValue {
540540

541541
const FieldDecl *getUnionField() const {
542542
assert(isUnion() && "Invalid accessor");
543-
return ((const UnionData*)(const char*)Data.buffer)->Field;
543+
return ((const UnionData *)(const char *)&Data)->Field;
544544
}
545545
APValue &getUnionValue() {
546546
assert(isUnion() && "Invalid accessor");
547-
return *((UnionData*)(char*)Data.buffer)->Value;
547+
return *((UnionData *)(char *)&Data)->Value;
548548
}
549549
const APValue &getUnionValue() const {
550550
return const_cast<APValue*>(this)->getUnionValue();
@@ -556,24 +556,24 @@ class APValue {
556556

557557
const AddrLabelExpr* getAddrLabelDiffLHS() const {
558558
assert(isAddrLabelDiff() && "Invalid accessor");
559-
return ((const AddrLabelDiffData*)(const char*)Data.buffer)->LHSExpr;
559+
return ((const AddrLabelDiffData *)(const char *)&Data)->LHSExpr;
560560
}
561561
const AddrLabelExpr* getAddrLabelDiffRHS() const {
562562
assert(isAddrLabelDiff() && "Invalid accessor");
563-
return ((const AddrLabelDiffData*)(const char*)Data.buffer)->RHSExpr;
563+
return ((const AddrLabelDiffData *)(const char *)&Data)->RHSExpr;
564564
}
565565

566566
void setInt(APSInt I) {
567567
assert(isInt() && "Invalid accessor");
568-
*(APSInt *)(char *)Data.buffer = std::move(I);
568+
*(APSInt *)(char *)&Data = std::move(I);
569569
}
570570
void setFloat(APFloat F) {
571571
assert(isFloat() && "Invalid accessor");
572-
*(APFloat *)(char *)Data.buffer = std::move(F);
572+
*(APFloat *)(char *)&Data = std::move(F);
573573
}
574574
void setFixedPoint(APFixedPoint FX) {
575575
assert(isFixedPoint() && "Invalid accessor");
576-
*(APFixedPoint *)(char *)Data.buffer = std::move(FX);
576+
*(APFixedPoint *)(char *)&Data = std::move(FX);
577577
}
578578
void setVector(const APValue *E, unsigned N) {
579579
MutableArrayRef<APValue> InternalElts = setVectorUninit(N);
@@ -584,15 +584,15 @@ class APValue {
584584
assert(R.getBitWidth() == I.getBitWidth() &&
585585
"Invalid complex int (type mismatch).");
586586
assert(isComplexInt() && "Invalid accessor");
587-
((ComplexAPSInt *)(char *)Data.buffer)->Real = std::move(R);
588-
((ComplexAPSInt *)(char *)Data.buffer)->Imag = std::move(I);
587+
((ComplexAPSInt *)(char *)&Data)->Real = std::move(R);
588+
((ComplexAPSInt *)(char *)&Data)->Imag = std::move(I);
589589
}
590590
void setComplexFloat(APFloat R, APFloat I) {
591591
assert(&R.getSemantics() == &I.getSemantics() &&
592592
"Invalid complex float (type mismatch).");
593593
assert(isComplexFloat() && "Invalid accessor");
594-
((ComplexAPFloat *)(char *)Data.buffer)->Real = std::move(R);
595-
((ComplexAPFloat *)(char *)Data.buffer)->Imag = std::move(I);
594+
((ComplexAPFloat *)(char *)&Data)->Real = std::move(R);
595+
((ComplexAPFloat *)(char *)&Data)->Imag = std::move(I);
596596
}
597597
void setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
598598
bool IsNullPtr);
@@ -602,59 +602,59 @@ class APValue {
602602
void setUnion(const FieldDecl *Field, const APValue &Value);
603603
void setAddrLabelDiff(const AddrLabelExpr* LHSExpr,
604604
const AddrLabelExpr* RHSExpr) {
605-
((AddrLabelDiffData*)(char*)Data.buffer)->LHSExpr = LHSExpr;
606-
((AddrLabelDiffData*)(char*)Data.buffer)->RHSExpr = RHSExpr;
605+
((AddrLabelDiffData *)(char *)&Data)->LHSExpr = LHSExpr;
606+
((AddrLabelDiffData *)(char *)&Data)->RHSExpr = RHSExpr;
607607
}
608608

609609
private:
610610
void DestroyDataAndMakeUninit();
611611
void MakeInt() {
612612
assert(isAbsent() && "Bad state change");
613-
new ((void*)Data.buffer) APSInt(1);
613+
new ((void *)&Data) APSInt(1);
614614
Kind = Int;
615615
}
616616
void MakeFloat() {
617617
assert(isAbsent() && "Bad state change");
618-
new ((void*)(char*)Data.buffer) APFloat(0.0);
618+
new ((void *)(char *)&Data) APFloat(0.0);
619619
Kind = Float;
620620
}
621621
void MakeFixedPoint(APFixedPoint &&FX) {
622622
assert(isAbsent() && "Bad state change");
623-
new ((void *)(char *)Data.buffer) APFixedPoint(std::move(FX));
623+
new ((void *)(char *)&Data) APFixedPoint(std::move(FX));
624624
Kind = FixedPoint;
625625
}
626626
void MakeVector() {
627627
assert(isAbsent() && "Bad state change");
628-
new ((void*)(char*)Data.buffer) Vec();
628+
new ((void *)(char *)&Data) Vec();
629629
Kind = Vector;
630630
}
631631
void MakeComplexInt() {
632632
assert(isAbsent() && "Bad state change");
633-
new ((void*)(char*)Data.buffer) ComplexAPSInt();
633+
new ((void *)(char *)&Data) ComplexAPSInt();
634634
Kind = ComplexInt;
635635
}
636636
void MakeComplexFloat() {
637637
assert(isAbsent() && "Bad state change");
638-
new ((void*)(char*)Data.buffer) ComplexAPFloat();
638+
new ((void *)(char *)&Data) ComplexAPFloat();
639639
Kind = ComplexFloat;
640640
}
641641
void MakeLValue();
642642
void MakeArray(unsigned InitElts, unsigned Size);
643643
void MakeStruct(unsigned B, unsigned M) {
644644
assert(isAbsent() && "Bad state change");
645-
new ((void*)(char*)Data.buffer) StructData(B, M);
645+
new ((void *)(char *)&Data) StructData(B, M);
646646
Kind = Struct;
647647
}
648648
void MakeUnion() {
649649
assert(isAbsent() && "Bad state change");
650-
new ((void*)(char*)Data.buffer) UnionData();
650+
new ((void *)(char *)&Data) UnionData();
651651
Kind = Union;
652652
}
653653
void MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
654654
ArrayRef<const CXXRecordDecl*> Path);
655655
void MakeAddrLabelDiff() {
656656
assert(isAbsent() && "Bad state change");
657-
new ((void*)(char*)Data.buffer) AddrLabelDiffData();
657+
new ((void *)(char *)&Data) AddrLabelDiffData();
658658
Kind = AddrLabelDiff;
659659
}
660660

@@ -664,7 +664,7 @@ class APValue {
664664
/// filled in by those steps.
665665
MutableArrayRef<APValue> setVectorUninit(unsigned N) {
666666
assert(isVector() && "Invalid accessor");
667-
Vec *V = ((Vec *)(char *)Data.buffer);
667+
Vec *V = ((Vec *)(char *)&Data);
668668
V->Elts = new APValue[N];
669669
V->NumElts = N;
670670
return {V->Elts, V->NumElts};

clang/include/clang/AST/ASTTypeTraits.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,16 @@ class DynTypedNode {
246246
/// in the \c DynTypedNode, and the returned pointer points at
247247
/// the storage inside DynTypedNode. For those nodes, do not
248248
/// use the pointer outside the scope of the DynTypedNode.
249-
template <typename T>
250-
const T *get() const {
251-
return BaseConverter<T>::get(NodeKind, Storage.buffer);
249+
template <typename T> const T *get() const {
250+
return BaseConverter<T>::get(NodeKind, &Storage);
252251
}
253252

254253
/// Retrieve the stored node as type \c T.
255254
///
256255
/// Similar to \c get(), but asserts that the type is what we are expecting.
257256
template <typename T>
258257
const T &getUnchecked() const {
259-
return BaseConverter<T>::getUnchecked(NodeKind, Storage.buffer);
258+
return BaseConverter<T>::getUnchecked(NodeKind, &Storage);
260259
}
261260

262261
ASTNodeKind getNodeKind() const { return NodeKind; }
@@ -268,7 +267,7 @@ class DynTypedNode {
268267
/// method returns NULL.
269268
const void *getMemoizationData() const {
270269
return NodeKind.hasPointerIdentity()
271-
? *reinterpret_cast<void *const *>(Storage.buffer)
270+
? *reinterpret_cast<void *const *>(&Storage)
272271
: nullptr;
273272
}
274273

@@ -390,59 +389,59 @@ class DynTypedNode {
390389

391390
/// Converter that uses dyn_cast<T> from a stored BaseT*.
392391
template <typename T, typename BaseT> struct DynCastPtrConverter {
393-
static const T *get(ASTNodeKind NodeKind, const char Storage[]) {
392+
static const T *get(ASTNodeKind NodeKind, const void *Storage) {
394393
if (ASTNodeKind::getFromNodeKind<T>().isBaseOf(NodeKind))
395394
return &getUnchecked(NodeKind, Storage);
396395
return nullptr;
397396
}
398-
static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) {
397+
static const T &getUnchecked(ASTNodeKind NodeKind, const void *Storage) {
399398
assert(ASTNodeKind::getFromNodeKind<T>().isBaseOf(NodeKind));
400399
return *cast<T>(static_cast<const BaseT *>(
401400
*reinterpret_cast<const void *const *>(Storage)));
402401
}
403402
static DynTypedNode create(const BaseT &Node) {
404403
DynTypedNode Result;
405404
Result.NodeKind = ASTNodeKind::getFromNode(Node);
406-
new (Result.Storage.buffer) const void *(&Node);
405+
new (&Result.Storage) const void *(&Node);
407406
return Result;
408407
}
409408
};
410409

411410
/// Converter that stores T* (by pointer).
412411
template <typename T> struct PtrConverter {
413-
static const T *get(ASTNodeKind NodeKind, const char Storage[]) {
412+
static const T *get(ASTNodeKind NodeKind, const void *Storage) {
414413
if (ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind))
415414
return &getUnchecked(NodeKind, Storage);
416415
return nullptr;
417416
}
418-
static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) {
417+
static const T &getUnchecked(ASTNodeKind NodeKind, const void *Storage) {
419418
assert(ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind));
420419
return *static_cast<const T *>(
421420
*reinterpret_cast<const void *const *>(Storage));
422421
}
423422
static DynTypedNode create(const T &Node) {
424423
DynTypedNode Result;
425424
Result.NodeKind = ASTNodeKind::getFromNodeKind<T>();
426-
new (Result.Storage.buffer) const void *(&Node);
425+
new (&Result.Storage) const void *(&Node);
427426
return Result;
428427
}
429428
};
430429

431430
/// Converter that stores T (by value).
432431
template <typename T> struct ValueConverter {
433-
static const T *get(ASTNodeKind NodeKind, const char Storage[]) {
432+
static const T *get(ASTNodeKind NodeKind, const void *Storage) {
434433
if (ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind))
435434
return reinterpret_cast<const T *>(Storage);
436435
return nullptr;
437436
}
438-
static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) {
437+
static const T &getUnchecked(ASTNodeKind NodeKind, const void *Storage) {
439438
assert(ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind));
440439
return *reinterpret_cast<const T *>(Storage);
441440
}
442441
static DynTypedNode create(const T &Node) {
443442
DynTypedNode Result;
444443
Result.NodeKind = ASTNodeKind::getFromNodeKind<T>();
445-
new (Result.Storage.buffer) T(Node);
444+
new (&Result.Storage) T(Node);
446445
return Result;
447446
}
448447
};

0 commit comments

Comments
 (0)