[patch] Fix source range of CXXNewExprs

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.

My patch adds an end location to TypeSpecTypeLoc and fills it in in
TypeSpecLocFiller::VisitTagTypeLoc(). The rest of the patch consists
of copying this new TypeSpecTypeLoc field every time a TypeSpecTypeLoc
is copied, which happens in surprisingly many places.

Acurate sourceranges on all ast nodes are important for writing rewriters.

Nico

newrange.patch (10.6 KB)

This should be an ElaboratedTypeLoc. If it isn't, that's the bug, not changing
TypeSpecTypeLoc.

John.

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

Ah. So the problem is that the TypeSpecTypeLoc's location should be
the location of the qualified identifier, not the location of the start of the
qualification.

John.

Like this? If so, I guess the other calls to SetTypeSpecType() where a
annot_typename is parsed need this change, too?

Nico

sourceranges2.patch (1.32 KB)

On SemaType.cpp:1561, try changing
  if (Keyword == ETK_Typename) {
to
  if (DS.getTypeSpecType() == TST_typename)

If that works, please do the same thing to SemaType.cpp:1579.

John.

That doesn't seem to help.

Nico

Ah yes, I see the problem. You're right, we need to use getAnnotationEndLoc(),
both here and elsewhere. I think we probably also need the change to
SemaType.cpp, though.

John.

Third time's the charm? Please take a look :slight_smile:

Nico

sourceranges3.patch (4.39 KB)

Looks good, thanks!

John.

r119966. Thanks!