[clang] [Clang][counted_by] Refactor __builtin_dynamic_object_size on FAMs (PR #122198)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 15 15:12:35 PST 2025
================
@@ -1060,236 +1061,358 @@ CodeGenFunction::evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
return ConstantInt::get(ResType, ObjectSize, /*isSigned=*/true);
}
-const FieldDecl *CodeGenFunction::FindFlexibleArrayMemberFieldAndOffset(
- ASTContext &Ctx, const RecordDecl *RD, const FieldDecl *FAMDecl,
- uint64_t &Offset) {
+namespace {
+
+/// StructFieldAccess is a simple visitor class to grab the first MemberExpr
+/// from an Expr. It records any ArraySubscriptExpr we meet along the way.
+class StructFieldAccess
+ : public ConstStmtVisitor<StructFieldAccess, const MemberExpr *> {
+ bool AddrOfSeen = false;
+
+public:
+ const ArraySubscriptExpr *ASE = nullptr;
+
+ const MemberExpr *VisitMemberExpr(const MemberExpr *E) {
+ if (AddrOfSeen && E->getType()->isArrayType())
+ // Avoid forms like '&ptr->array'.
+ return nullptr;
+ return E;
+ }
+
+ const MemberExpr *VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
+ AddrOfSeen = false; // '&ptr->array[idx]' is okay.
+ ASE = E;
+ return Visit(E->getBase());
+ }
+ const MemberExpr *VisitCastExpr(const CastExpr *E) {
+ return Visit(E->getSubExpr());
----------------
efriedma-quic wrote:
Like I've mentioned before, I generally prefer explicitly classifying casts by CastKind, instead of ignoring the casts, then trying to infer what casts happened later. In particular, you do not want to look through lvalue-to-rvalue conversions in this context.
https://github.com/llvm/llvm-project/pull/122198
More information about the cfe-commits
mailing list