[cfe-dev] [patch] Fix source range of CXXNewExprs

Nico Weber thakis at chromium.org
Wed Nov 17 15:35:10 PST 2010


On Thu, Nov 18, 2010 at 12:29 AM, John McCall <rjmccall at apple.com> wrote:
>
> On Nov 17, 2010, at 3:22 PM, Nico Weber wrote:
>
>> Hi,
>>
>> the attached patch fixes the source range on CXXNewExprs. For code like
>>
>>  foo:A* a = new foo::A;
>>
>> the CXXNewExpr's source range's end would point to `foo` instead of
>> `A`. This was because Sema::BuildCXXNew() uses the allocated type's
>> TypeLoc's source range's end as end location for CXXNewExpr if no
>> parens are present (new foo::A() is handled correctly already), and
>> TypeSpecTypeLoc has only a start location.
>
> This should be an ElaboratedTypeLoc.  If it isn't, that's the bug, not changing
> TypeSpecTypeLoc.

It is an ElaboratedTypeLoc, but TypeLoc::getEndLoc() skips the
ElaboratedTypeLoc's position and moves on the next loc:

SourceLocation TypeLoc::getEndLoc() const {
  TypeLoc Cur = *this;
  while (true) {
    switch (Cur.getTypeLocClass()) {
    default:
      break;
    case Qualified:
    case Elaborated:
      Cur = Cur.getNextTypeLoc();
      continue;
    }
    break;
  }
  return Cur.getLocalSourceRange().getEnd();
}

Nico




More information about the cfe-dev mailing list