https://github.com/bwendling updated https://github.com/llvm/llvm-project/pull/104235
>From a87bc65687b687073e8ff37606ca9e783f1edcf9 Mon Sep 17 00:00:00 2001 From: Bill Wendling <mo...@google.com> Date: Wed, 14 Aug 2024 13:44:56 -0700 Subject: [PATCH 1/2] [Clang][NFC] Move FindCountedByField into FieldDecl FindCountedByField can be used in more places than CodeGen. Move it into FieldDecl to avoid layering issues. --- clang/include/clang/AST/Decl.h | 4 ++++ clang/lib/AST/Decl.cpp | 13 +++++++++++++ clang/lib/CodeGen/CGBuiltin.cpp | 2 +- clang/lib/CodeGen/CGExpr.cpp | 18 +----------------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 561a9d872acfb0..5535703ef906f4 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -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); diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index e125143bc1b270..767fd732a515b6 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -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 //===----------------------------------------------------------------------===// diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index b5e5240e55be3f..4fb88909c644ca 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -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; diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index f93f8dda0bd29a..10b1050876decc 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -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) { @@ -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); >From b3d6c08fe18943156331a9378d4d0a40cf46f8d1 Mon Sep 17 00:00:00 2001 From: Bill Wendling <mo...@google.com> Date: Wed, 14 Aug 2024 22:23:50 -0700 Subject: [PATCH 2/2] Use camelcase. --- clang/include/clang/AST/Decl.h | 2 +- clang/lib/AST/Decl.cpp | 2 +- clang/lib/CodeGen/CGBuiltin.cpp | 2 +- clang/lib/CodeGen/CGExpr.cpp | 2 +- clang/lib/CodeGen/CodeGenFunction.h | 4 ---- 5 files changed, 4 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 5535703ef906f4..6d84bd03de810a 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -3208,7 +3208,7 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> { /// 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; + const FieldDecl *findCountedByField() const; private: void setLazyInClassInitializer(LazyDeclStmtPtr NewInit); diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 767fd732a515b6..90caf81757ac96 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -4678,7 +4678,7 @@ void FieldDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { DeclaratorDecl::printName(OS, Policy); } -const FieldDecl *FieldDecl::FindCountedByField() const { +const FieldDecl *FieldDecl::findCountedByField() const { const auto *CAT = getType()->getAs<CountAttributedType>(); if (!CAT) return nullptr; diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 4fb88909c644ca..1c0baeaee03632 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -987,7 +987,7 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr *E, unsigned Type, // attribute. return nullptr; - const FieldDecl *CountedByFD = FAMDecl->FindCountedByField(); + const FieldDecl *CountedByFD = FAMDecl->findCountedByField(); if (!CountedByFD) // Can't find the field referenced by the "counted_by" attribute. return nullptr; diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 10b1050876decc..0672861790633b 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -4289,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 = FAMDecl->FindCountedByField()) { + if (const FieldDecl *CountFD = FAMDecl->findCountedByField()) { if (std::optional<int64_t> Diff = getOffsetDifferenceInBits(*this, CountFD, FAMDecl)) { CharUnits OffsetDiff = CGM.getContext().toCharUnitsFromBits(*Diff); diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 19a7feeb69d820..57e0b7f91e9bf8 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -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, _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits