Skip to content

[Clang][NFC] Move FindCountedByField into FieldDecl #104235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,10 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
/// Set the C++11 in-class initializer for this member.
void setInClassInitializer(Expr *NewInit);

/// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns
/// \p nullptr if either the attribute or the field doesn't exist.
const FieldDecl *findCountedByField() const;

private:
void setLazyInClassInitializer(LazyDeclStmtPtr NewInit);

Expand Down
13 changes: 13 additions & 0 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4678,6 +4678,19 @@ void FieldDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const {
DeclaratorDecl::printName(OS, Policy);
}

const FieldDecl *FieldDecl::findCountedByField() const {
const auto *CAT = getType()->getAs<CountAttributedType>();
if (!CAT)
return nullptr;

const auto *CountDRE = cast<DeclRefExpr>(CAT->getCountExpr());
const auto *CountDecl = CountDRE->getDecl();
if (const auto *IFD = dyn_cast<IndirectFieldDecl>(CountDecl))
CountDecl = IFD->getAnonField();

return dyn_cast<FieldDecl>(CountDecl);
}

//===----------------------------------------------------------------------===//
// TagDecl Implementation
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr *E, unsigned Type,
// attribute.
return nullptr;

const FieldDecl *CountedByFD = FindCountedByField(FAMDecl);
const FieldDecl *CountedByFD = FAMDecl->findCountedByField();
if (!CountedByFD)
// Can't find the field referenced by the "counted_by" attribute.
return nullptr;
Expand Down
18 changes: 1 addition & 17 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,22 +1150,6 @@ llvm::Value *CodeGenFunction::EmitLoadOfCountedByField(
getIntAlign(), "..counted_by.load");
}

const FieldDecl *CodeGenFunction::FindCountedByField(const FieldDecl *FD) {
if (!FD)
return nullptr;

const auto *CAT = FD->getType()->getAs<CountAttributedType>();
if (!CAT)
return nullptr;

const auto *CountDRE = cast<DeclRefExpr>(CAT->getCountExpr());
const auto *CountDecl = CountDRE->getDecl();
if (const auto *IFD = dyn_cast<IndirectFieldDecl>(CountDecl))
CountDecl = IFD->getAnonField();

return dyn_cast<FieldDecl>(CountDecl);
}

void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
llvm::Value *Index, QualType IndexType,
bool Accessed) {
Expand Down Expand Up @@ -4305,7 +4289,7 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel) &&
ME->getMemberDecl()->getType()->isCountAttributedType()) {
const FieldDecl *FAMDecl = dyn_cast<FieldDecl>(ME->getMemberDecl());
if (const FieldDecl *CountFD = FindCountedByField(FAMDecl)) {
if (const FieldDecl *CountFD = FAMDecl->findCountedByField()) {
if (std::optional<int64_t> Diff =
getOffsetDifferenceInBits(*this, CountFD, FAMDecl)) {
CharUnits OffsetDiff = CGM.getContext().toCharUnitsFromBits(*Diff);
Expand Down
4 changes: 0 additions & 4 deletions clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3305,10 +3305,6 @@ class CodeGenFunction : public CodeGenTypeCache {
const FieldDecl *FAMDecl,
uint64_t &Offset);

/// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns
/// \p nullptr if either the attribute or the field doesn't exist.
const FieldDecl *FindCountedByField(const FieldDecl *FD);

/// Build an expression accessing the "counted_by" field.
llvm::Value *EmitLoadOfCountedByField(const Expr *Base,
const FieldDecl *FAMDecl,
Expand Down
Loading