[cfe-dev] Ownership of Stmts and Exprs, and destroying them

Doug Gregor doug.gregor at gmail.com
Sun Nov 23 05:25:23 PST 2008


On Sun, Nov 23, 2008 at 2:54 AM, Chris Lattner <clattner at apple.com> wrote:
>
> On Nov 22, 2008, at 3:36 PM, Sebastian Redl wrote:
>> Is this correct? Because I want to clean this up. I want to make the
>> Sema routines (and later the parser, too) not leak. So I need to know
>> exactly how this works.
>
> This would be great.  Probably the first place to start is to use
> audit the parser for places where early exits cause parsed stmts to be
> leaked.  Once that is done, start going through sema.  The pattern we
> want to use in Sema for statements is using OwningPtr to hold incoming
> arguments.  For example, ActOnBinOp should look like:
>
>                                     ExprTy *LHS, ExprTy *RHS) {
>   BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
>   OwningPtr<Expr> lhs = (Expr *)LHS, rhs = (Expr*)RHS;

This is too late. We want the arguments to the ActOnBinOp to be some
kind of smart pointer that provides transfer-of-ownership semantics
(basically, a C++'0x move_ptr) and knows how to delete expressions
properly (via Action::DestroyExpr). Similarly for ExprResult and
friends. That way, the type system enforces our ownership model
(transfer into Sema for the parameters of ActOn*, transfer out of Sema
on return), and we're safe from early exits.

  - Doug



More information about the cfe-dev mailing list