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

Chris Lattner clattner at apple.com
Mon Nov 24 13:32:04 PST 2008


On Nov 23, 2008, at 5:25 AM, Doug Gregor wrote:

> 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).

Sema doesn't need to call DestroyExpr: it knows these are Expr*'s.   
The Parser needs to call Action::DestroyExpr because these are just  
void*'s to it.

> 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.

ExprResult is just a pair of success + pointer.  The actions in Sema  
are only called in the success case.

-Chris



More information about the cfe-dev mailing list