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

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Fri May 31 15:37: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:

With the latest version of your patch, with the following code, both __builtin_dynamic_object_size() calls fold to 319, which is pretty clearly wrong.  (gcc folds the first call to 312, and the second to -1.)

```
void report(long);
int main(int argc, char **argv) {
char *bar[40];
argc = 1;
report(__builtin_dynamic_object_size(&bar[argc], 1));
report(__builtin_dynamic_object_size(bar[argc], 1));
return 0;
}
```

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


More information about the cfe-commits mailing list