[clang] [Clang][NFC] Move FindCountedByField into FieldDecl (PR #104235)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 14 13:55:02 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Bill Wendling (bwendling)
<details>
<summary>Changes</summary>
FindCountedByField can be used in more places than CodeGen. Move it into FieldDecl to avoid layering issues.
---
Full diff: https://github.com/llvm/llvm-project/pull/104235.diff
4 Files Affected:
- (modified) clang/include/clang/AST/Decl.h (+4)
- (modified) clang/lib/AST/Decl.cpp (+13)
- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+1-1)
- (modified) clang/lib/CodeGen/CGExpr.cpp (+1-17)
``````````diff
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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/104235
More information about the cfe-commits
mailing list