[clang] [Clang][objectsize] Generate object size calculation for sub-objects (PR #86858)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Thu May 30 15:06:51 PDT 2024


================
@@ -1062,6 +1063,159 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr *E, unsigned Type,
   return Builder.CreateSelect(Cmp, Res, ConstantInt::get(ResType, 0, IsSigned));
 }
 
+namespace {
+
+class ObjectSizeVisitor
+    : public ConstStmtVisitor<ObjectSizeVisitor, const Expr *> {
+  bool SkipASE;
+
+public:
+  ObjectSizeVisitor(bool SkipASE = false) : SkipASE(SkipASE) {}
+
+  const Expr *Visit(const Expr *E) {
+    return ConstStmtVisitor<ObjectSizeVisitor, const Expr *>::Visit(E);
+  }
+
+  const Expr *VisitStmt(const Stmt *S) { return nullptr; }
+
+  const Expr *VisitDeclRefExpr(const DeclRefExpr *E) { return E; }
+  const Expr *VisitMemberExpr(const MemberExpr *E) { return E; }
+  const Expr *VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
+    return SkipASE ? Visit(E->getBase()) : E;
+  }
+
+  const Expr *VisitCastExpr(const CastExpr *E) {
+    return Visit(E->getSubExpr());
+  }
+  const Expr *VisitParenExpr(const ParenExpr *E) {
+    return Visit(E->getSubExpr());
+  }
+  const Expr *VisitUnaryAddrOf(const clang::UnaryOperator *E) {
+    return Visit(E->getSubExpr());
----------------
efriedma-quic wrote:

If you have an array of pointers `int* a[10];`, and someone writes `__bdos(a[b])`, that's equivalent to `void* p = a[b]; __bdos(p)`... and this code can't resolve that kind of construct.  So you need to first look for an "&" (or the equivalent array-to-pointer decay), then look for an array subscript expression inside of that.

If you restrict things enough so each expression you handle is inherently either an lvalue or an rvalue, the result would be logically sound, I guess.  This requires ensuring you don't IgnoreParenImpCasts anywhere because that can look through an lvalue-to-rvalue conversion.  I think it would be more clear with two separate visitors, though.

https://github.com/llvm/llvm-project/pull/86858


More information about the cfe-commits mailing list