[clang] [Clang] Generate the GEP instead of adding AST nodes (PR #73730)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 5 14:06:35 PST 2023


efriedma-quic wrote:

> > And ideally, the recursive visit should list those expressions explicitly, instead aborting on ones we know are bad.
> 
> I'm sorry, I don't understand what you're talking about here. The whole point of the recursive visit is to find a suitable base pointer. If we run across any undesirable expressions (e.g. pointer arithmetic) then we return `nullptr` to signify this. What do you mean we should list those expressions explicitly?

I mean, the base case should be "return nullptr", and you should only explicitly list out expressions you know we need to handle.  We shouldn't need to explicitly mention VisitUnaryPostDec etc.

> > So you don't recurse; you just look for that exact AST structure.
> 
> The information about what LValue was generated for that base pointer is no longer available to us.

No, I mean, you do the check before you emit the lvalue for the base.  So in `ArraySubscriptExpr(MemberExpr(StructBase), ArrayIndex)`, instead of calling EmitLValue on `MemberExpr(StructBase)` like we normally would, you call EmitLValue on StructBase.  So we have the LValue.  Then we do the indexing corresponding to the MemberExpr explicitly.  So you don't care what `StructBase` refers to, and you don't need a map to look up expressions.

For __builtin_dynamic_object_size, my understanding is that this approach doesn't work because the argument isn't actually supposed to be evaluated.  So we need a different approach that looks for specific constructs we want to allow.  And then we need a mini-codegen that specifically handles those constructs (or a mini-verifier that ensures when we call generic codegen, we don't have side-effects, but that's harder to get right).  And this is where you need the full recursive visit.

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


More information about the cfe-commits mailing list