Skip to content

Commit 94b8b11

Browse files
authored
[Clang][NFC] Move FindCountedByField into FieldDecl (#104235)
FindCountedByField can be used in more places than CodeGen. Move it into FieldDecl to avoid layering issues.
1 parent 6e2d9df commit 94b8b11

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,10 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
32063206
/// Set the C++11 in-class initializer for this member.
32073207
void setInClassInitializer(Expr *NewInit);
32083208

3209+
/// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns
3210+
/// \p nullptr if either the attribute or the field doesn't exist.
3211+
const FieldDecl *findCountedByField() const;
3212+
32093213
private:
32103214
void setLazyInClassInitializer(LazyDeclStmtPtr NewInit);
32113215

clang/lib/AST/Decl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4678,6 +4678,19 @@ void FieldDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const {
46784678
DeclaratorDecl::printName(OS, Policy);
46794679
}
46804680

4681+
const FieldDecl *FieldDecl::findCountedByField() const {
4682+
const auto *CAT = getType()->getAs<CountAttributedType>();
4683+
if (!CAT)
4684+
return nullptr;
4685+
4686+
const auto *CountDRE = cast<DeclRefExpr>(CAT->getCountExpr());
4687+
const auto *CountDecl = CountDRE->getDecl();
4688+
if (const auto *IFD = dyn_cast<IndirectFieldDecl>(CountDecl))
4689+
CountDecl = IFD->getAnonField();
4690+
4691+
return dyn_cast<FieldDecl>(CountDecl);
4692+
}
4693+
46814694
//===----------------------------------------------------------------------===//
46824695
// TagDecl Implementation
46834696
//===----------------------------------------------------------------------===//

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr *E, unsigned Type,
987987
// attribute.
988988
return nullptr;
989989

990-
const FieldDecl *CountedByFD = FindCountedByField(FAMDecl);
990+
const FieldDecl *CountedByFD = FAMDecl->findCountedByField();
991991
if (!CountedByFD)
992992
// Can't find the field referenced by the "counted_by" attribute.
993993
return nullptr;

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,22 +1150,6 @@ llvm::Value *CodeGenFunction::EmitLoadOfCountedByField(
11501150
getIntAlign(), "..counted_by.load");
11511151
}
11521152

1153-
const FieldDecl *CodeGenFunction::FindCountedByField(const FieldDecl *FD) {
1154-
if (!FD)
1155-
return nullptr;
1156-
1157-
const auto *CAT = FD->getType()->getAs<CountAttributedType>();
1158-
if (!CAT)
1159-
return nullptr;
1160-
1161-
const auto *CountDRE = cast<DeclRefExpr>(CAT->getCountExpr());
1162-
const auto *CountDecl = CountDRE->getDecl();
1163-
if (const auto *IFD = dyn_cast<IndirectFieldDecl>(CountDecl))
1164-
CountDecl = IFD->getAnonField();
1165-
1166-
return dyn_cast<FieldDecl>(CountDecl);
1167-
}
1168-
11691153
void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
11701154
llvm::Value *Index, QualType IndexType,
11711155
bool Accessed) {
@@ -4305,7 +4289,7 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
43054289
ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel) &&
43064290
ME->getMemberDecl()->getType()->isCountAttributedType()) {
43074291
const FieldDecl *FAMDecl = dyn_cast<FieldDecl>(ME->getMemberDecl());
4308-
if (const FieldDecl *CountFD = FindCountedByField(FAMDecl)) {
4292+
if (const FieldDecl *CountFD = FAMDecl->findCountedByField()) {
43094293
if (std::optional<int64_t> Diff =
43104294
getOffsetDifferenceInBits(*this, CountFD, FAMDecl)) {
43114295
CharUnits OffsetDiff = CGM.getContext().toCharUnitsFromBits(*Diff);

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,10 +3305,6 @@ class CodeGenFunction : public CodeGenTypeCache {
33053305
const FieldDecl *FAMDecl,
33063306
uint64_t &Offset);
33073307

3308-
/// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns
3309-
/// \p nullptr if either the attribute or the field doesn't exist.
3310-
const FieldDecl *FindCountedByField(const FieldDecl *FD);
3311-
33123308
/// Build an expression accessing the "counted_by" field.
33133309
llvm::Value *EmitLoadOfCountedByField(const Expr *Base,
33143310
const FieldDecl *FAMDecl,

0 commit comments

Comments
 (0)