Skip to content

Commit 0f4424e

Browse files
committed
Implement TreeTransform for CountAttributedType; address other style suggestions
1 parent 3e40ddf commit 0f4424e

File tree

11 files changed

+61
-30
lines changed

11 files changed

+61
-30
lines changed

clang/include/clang/AST/Type.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2007,14 +2007,14 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
20072007
LLVM_PREFERRED_TYPE(TypeBitfields)
20082008
unsigned : NumTypeBits;
20092009

2010-
/// The limit is 15.
20112010
static constexpr unsigned NumCoupledDeclsBits = 4;
20122011
unsigned NumCoupledDecls : NumCoupledDeclsBits;
20132012
LLVM_PREFERRED_TYPE(bool)
20142013
unsigned CountInBytes : 1;
20152014
LLVM_PREFERRED_TYPE(bool)
20162015
unsigned OrNull : 1;
20172016
};
2017+
static_assert(sizeof(CountAttributedTypeBitfields) <= sizeof(unsigned));
20182018

20192019
union {
20202020
TypeBitfields TypeBits;
@@ -2279,6 +2279,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
22792279
bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); }
22802280
bool isPointerType() const;
22812281
bool isAnyPointerType() const; // Any C pointer or ObjC object pointer
2282+
bool isCountAttributedType() const;
22822283
bool isBlockPointerType() const;
22832284
bool isVoidPointerType() const;
22842285
bool isReferenceType() const;

clang/include/clang/AST/TypeLoc.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,13 +1125,11 @@ class BoundsAttributedTypeLoc
11251125
: public ConcreteTypeLoc<UnqualTypeLoc, BoundsAttributedTypeLoc,
11261126
BoundsAttributedType, BoundsAttributedLocInfo> {
11271127
public:
1128-
TypeLoc getInnerLoc() const { return this->getInnerTypeLoc(); }
1129-
QualType getInnerType() const { return this->getTypePtr()->desugar(); }
1128+
TypeLoc getInnerLoc() const { return getInnerTypeLoc(); }
1129+
QualType getInnerType() const { return getTypePtr()->desugar(); }
11301130
void initializeLocal(ASTContext &Context, SourceLocation Loc) {
11311131
// nothing to do
11321132
}
1133-
unsigned getLocalDataAlignment() const { return 1; }
1134-
unsigned getLocalDataSize() const { return 0; }
11351133
};
11361134

11371135
class CountAttributedTypeLoc final

clang/lib/AST/ASTContext.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13270,9 +13270,9 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
1327013270
return QualType();
1327113271
if (DX->isOrNull() != DY->isOrNull())
1327213272
return QualType();
13273-
const auto CEX = DX->getCountExpr();
13274-
const auto CEY = DY->getCountExpr();
13275-
const auto CDX = DX->getCoupledDecls();
13273+
Expr *CEX = DX->getCountExpr();
13274+
Expr *CEY = DY->getCountExpr();
13275+
llvm::ArrayRef<clang::TypeCoupledDeclRefInfo> CDX = DX->getCoupledDecls();
1327613276
if (Ctx.hasSameExpr(CEX, CEY))
1327713277
return Ctx.getCountAttributedType(Ctx.getQualifiedType(Underlying), CEX,
1327813278
DX->isCountInBytes(), DX->isOrNull(),
@@ -13281,9 +13281,9 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
1328113281
return QualType();
1328213282
// Two declarations with the same integer constant may still differ in their
1328313283
// expression pointers, so we need to evaluate them.
13284-
const auto VX = CEX->getIntegerConstantExpr(Ctx);
13285-
const auto VY = CEY->getIntegerConstantExpr(Ctx);
13286-
if (*VX != *VY)
13284+
llvm::APSInt VX = *CEX->getIntegerConstantExpr(Ctx);
13285+
llvm::APSInt VY = *CEY->getIntegerConstantExpr(Ctx);
13286+
if (VX != VY)
1328713287
return QualType();
1328813288
return Ctx.getCountAttributedType(Ctx.getQualifiedType(Underlying), CEX,
1328913289
DX->isCountInBytes(), DX->isOrNull(),

clang/lib/AST/Type.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,8 @@ void DependentBitIntType::Profile(llvm::FoldingSetNodeID &ID,
386386
}
387387

388388
bool BoundsAttributedType::referencesFieldDecls() const {
389-
for (const auto &Decl : dependent_decls())
390-
if (isa<FieldDecl>(Decl.getDecl()))
391-
return true;
392-
return false;
389+
return llvm::any_of(dependent_decls(), [](const TypeCoupledDeclRefInfo &Info) {
390+
return isa<FieldDecl>(Info.getDecl()); });
393391
}
394392

395393
void CountAttributedType::Profile(llvm::FoldingSetNodeID &ID,
@@ -669,6 +667,10 @@ bool Type::isScopedEnumeralType() const {
669667
return false;
670668
}
671669

670+
bool Type::isCountAttributedType() const {
671+
return getAs<CountAttributedType>();
672+
}
673+
672674
const ComplexType *Type::getAsComplexIntegerType() const {
673675
if (const auto *Complex = getAs<ComplexType>())
674676
if (Complex->getElementType()->isIntegerType())

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ static unsigned CountCountedByAttrs(const RecordDecl *RD) {
858858

859859
for (const Decl *D : RD->decls()) {
860860
if (const auto *FD = dyn_cast<FieldDecl>(D);
861-
FD && FD->getType()->getAs<CountAttributedType>()) {
861+
FD && FD->getType()->isCountAttributedType()) {
862862
return ++Num;
863863
}
864864

@@ -975,7 +975,7 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr *E, unsigned Type,
975975
FindFlexibleArrayMemberField(Ctx, OuterRD, FAMName, Offset);
976976
Offset = Ctx.toCharUnitsFromBits(Offset).getQuantity();
977977

978-
if (!FAMDecl || !FAMDecl->getType()->getAs<CountAttributedType>())
978+
if (!FAMDecl || !FAMDecl->getType()->isCountAttributedType())
979979
// No flexible array member found or it doesn't have the "counted_by"
980980
// attribute.
981981
return nullptr;

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,12 +1139,12 @@ const FieldDecl *CodeGenFunction::FindCountedByField(const FieldDecl *FD) {
11391139
if (!FD)
11401140
return nullptr;
11411141

1142-
auto *CAT = FD->getType()->getAs<CountAttributedType>();
1142+
const auto *CAT = FD->getType()->getAs<CountAttributedType>();
11431143
if (!CAT)
11441144
return nullptr;
11451145

1146-
auto *CountDRE = cast<DeclRefExpr>(CAT->getCountExpr());
1147-
auto *CountDecl = CountDRE->getDecl();
1146+
const auto *CountDRE = cast<DeclRefExpr>(CAT->getCountExpr());
1147+
const auto *CountDecl = CountDRE->getDecl();
11481148
if (const auto *IFD = dyn_cast<IndirectFieldDecl>(CountDecl))
11491149
CountDecl = IFD->getAnonField();
11501150

@@ -4240,7 +4240,7 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
42404240
if (const auto *ME = dyn_cast<MemberExpr>(Array);
42414241
ME &&
42424242
ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel) &&
4243-
ME->getMemberDecl()->getType()->getAs<CountAttributedType>()) {
4243+
ME->getMemberDecl()->getType()->isCountAttributedType()) {
42444244
const FieldDecl *FAMDecl = dyn_cast<FieldDecl>(ME->getMemberDecl());
42454245
if (const FieldDecl *CountFD = FindCountedByField(FAMDecl)) {
42464246
if (std::optional<int64_t> Diff =

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8578,8 +8578,7 @@ static void handleCountedByAttrField(Sema &S, Decl *D, const ParsedAttr &AL) {
85788578
if (CheckCountExpr(S, FD, CountExpr, Decls))
85798579
return;
85808580

8581-
QualType CAT = S.BuildCountAttributedArrayType(
8582-
FD->getType(), CountExpr, llvm::ArrayRef(Decls.begin(), Decls.end()));
8581+
QualType CAT = S.BuildCountAttributedArrayType(FD->getType(), CountExpr);
85838582
FD->setType(CAT);
85848583
}
85858584

clang/lib/Sema/SemaType.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6494,6 +6494,9 @@ namespace {
64946494
void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
64956495
fillAttributedTypeLoc(TL, State);
64966496
}
6497+
void VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
6498+
// nothing
6499+
}
64976500
void VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
64986501
// nothing
64996502
}
@@ -9745,11 +9748,18 @@ QualType Sema::BuildTypeofExprType(Expr *E, TypeOfKind Kind) {
97459748
return Context.getTypeOfExprType(E, Kind);
97469749
}
97479750

9748-
QualType Sema::BuildCountAttributedArrayType(
9749-
QualType WrappedTy, Expr *CountExpr,
9750-
llvm::ArrayRef<TypeCoupledDeclRefInfo> Decls) {
9751+
static void BuildTypeCoupledDecls(Expr *E,
9752+
llvm::SmallVectorImpl<TypeCoupledDeclRefInfo> &Decls) {
9753+
// Currently, 'counted_by' only allows direct DeclRefExpr to FieldDecl.
9754+
auto *CountDecl = cast<DeclRefExpr>(E)->getDecl();
9755+
Decls.push_back(TypeCoupledDeclRefInfo(CountDecl, /*IsDref*/ false));
9756+
}
9757+
9758+
QualType Sema::BuildCountAttributedArrayType(QualType WrappedTy, Expr *CountExpr) {
97519759
assert(WrappedTy->isIncompleteArrayType());
97529760

9761+
llvm::SmallVector<TypeCoupledDeclRefInfo, 1> Decls;
9762+
BuildTypeCoupledDecls(CountExpr, Decls);
97539763
/// When the resulting expression is invalid, we still create the AST using
97549764
/// the original count expression for the sake of AST dump.
97559765
return Context.getCountAttributedType(

clang/lib/Sema/TreeTransform.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7264,8 +7264,29 @@ QualType TreeTransform<Derived>::TransformAttributedType(TypeLocBuilder &TLB,
72647264
template <typename Derived>
72657265
QualType TreeTransform<Derived>::TransformCountAttributedType(
72667266
TypeLocBuilder &TLB, CountAttributedTypeLoc TL) {
7267-
// TODO
7268-
llvm_unreachable("Unexpected TreeTransform for CountAttributedType");
7267+
const CountAttributedType *OldTy = TL.getTypePtr();
7268+
QualType InnerTy = getDerived().TransformType(TLB, TL.getInnerLoc());
7269+
if (InnerTy.isNull())
7270+
return QualType();
7271+
7272+
Expr *OldCount = TL.getCountExpr();
7273+
Expr *NewCount = nullptr;
7274+
if (OldCount) {
7275+
ExprResult CountResult = getDerived().TransformExpr(OldCount);
7276+
if (CountResult.isInvalid())
7277+
return QualType();
7278+
NewCount = CountResult.get();
7279+
}
7280+
7281+
QualType Result = TL.getType();
7282+
if (getDerived().AlwaysRebuild() || InnerTy != OldTy->desugar() ||
7283+
OldCount != NewCount) {
7284+
// Currently, CountAttributedType can only wrap incomplete array types.
7285+
Result = SemaRef.BuildCountAttributedArrayType(InnerTy, NewCount);
7286+
}
7287+
7288+
TLB.push<CountAttributedTypeLoc>(Result);
7289+
return Result;
72697290
}
72707291

72717292
template <typename Derived>

clang/lib/Serialization/ASTReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6996,7 +6996,7 @@ void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
69966996
}
69976997

69986998
void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
6999-
// nothing to do
6999+
// Nothing to do
70007000
}
70017001

70027002
void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
515515
}
516516

517517
void TypeLocWriter::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
518-
// nothing to do
518+
// Nothing to do
519519
}
520520

521521
void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {

0 commit comments

Comments
 (0)