[clang] [Clang][objectsize] Generate object size calculation for sub-objects (PR #86858)
Bill Wendling via cfe-commits
cfe-commits at lists.llvm.org
Fri May 17 15:29:30 PDT 2024
================
@@ -1052,6 +1053,165 @@ 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());
+ }
+ const Expr *VisitUnaryDeref(const clang::UnaryOperator *E) {
+ return Visit(E->getSubExpr());
----------------
bwendling wrote:
It's a bit aggressive, but I think it's okay. We're interested in only two situations: a __bdos on an array index, and finding the base expression to test if it's a flexible array member. However, it does return `0` for this:
```c
struct foo {
int ***f;
};
extern struct foo *x;
__builtin_dynamic_object_size(*(x->f[37]), 1)
```
I'm not sure if that's correct. I'll make it a bit less aggressive.
https://github.com/llvm/llvm-project/pull/86858
More information about the cfe-commits
mailing list