[cfe-commits] r59732 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/Sema.h lib/Sema/SemaExpr.cpp lib/Sema/SemaOverload.cpp test/SemaCXX/overloaded-operator.cpp

Chris Lattner clattner at apple.com
Thu Nov 20 16:44:08 PST 2008


On Nov 20, 2008, at 8:27 AM, Douglas Gregor wrote:
> URL: http://llvm.org/viewvc/llvm-project?rev=59732&view=rev
> Log:
> Add support for overloaded operator-> when used in a member access
> expression (smart_ptr->mem).

Cool.  Another minor coding style nit:  :)

> +/// BuildOverloadedArrowExpr - Build a call to an overloaded @c  
> operator->
> +///  (if one exists), where @c Base is an expression of class type  
> and
> +/// @c Member is the name of the member we're trying to find.
> +Action::ExprResult
> +Sema::BuildOverloadedArrowExpr(Expr *Base, SourceLocation OpLoc,
> +                               SourceLocation MemberLoc,
> +                               IdentifierInfo &Member) {
> +  assert(Base->getType()->isRecordType() && "left-hand side must  
> have class type");
> +
...
>
> +    else
> +      Diag(OpLoc, diag::err_ovl_no_viable_oper)
> +        << "operator->" << Base->getSourceRange();
> +    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
> +    delete Base;
> +    return true;

I think it's awesome you're actually not leaking memory here, we need  
to do more of this in Sema.  As far as patterns go, please using an  
owning pointer to do this.  On entrance to the function, do something  
like:

   OwningPtr<Expr> Base(BaseInput);

this will trivially delete it on the error paths.

> +  // Build the operator call.
> +  Expr *FnExpr = new DeclRefExpr(Method, Method->getType(),  
> SourceLocation());
> +  UsualUnaryConversions(FnExpr);
> +  Base = new CXXOperatorCallExpr(FnExpr, &Base, 1,
> +                                 Method- 
> >getResultType().getNonReferenceType(),
> +                                 OpLoc);
> +  return ActOnMemberReferenceExpr(Base, OpLoc, tok::arrow,  
> MemberLoc, Member);

On the success path, just use ... Base.take() to take ownership from  
the owning ptr

-Chris




More information about the cfe-commits mailing list