Getting the same begin and end locations from TypeLoc?

Hi,

In what circumstances would one get the same location by calling the getBeginLoc() and getEndLoc() of a TypeLoc instance?

My setup is like this: I have a SemaConsumer, and from the HandleTranslationUnit method, I descend (via the passed ASTContext's getTranslationUnitDecl()) to the Decl I'm interested in:

  // I is an iterator from a DeclContext's decls_begin()
  if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(*I)) {
    TypeSourceInfo *TSI = DD->getTypeSourceInfo();
    TypeLoc TL = TSI->getTypeLoc();
    SourceLocation B = TL.getBeginLoc();
    SourceLocation E = TL.getEndLoc();

    // want to do something with B and E, e.g. get the characterData from the source manager
  }

And by calling their print(), it turns out that SourceLocations B and E always point to the same place of the source code -- same line, same column, though I expected E to be further down in the source. Did I miss anything in using TypeLoc?

Thanks,
Lukhnos

Hi,

In what circumstances would one get the same location by calling the getBeginLoc() and getEndLoc() of a TypeLoc instance?

An ‘end’ SourceLocation points to the start of the last token in the range. Therefore, you will see the same begin and end locations on all entities which are exactly one token long.

My setup is like this: I have a SemaConsumer, and from the HandleTranslationUnit method, I descend (via the passed ASTContext’s getTranslationUnitDecl()) to the Decl I’m interested in:

// I is an iterator from a DeclContext’s decls_begin()
if (const DeclaratorDecl *DD = dyn_cast(*I)) {
TypeSourceInfo *TSI = DD->getTypeSourceInfo();
TypeLoc TL = TSI->getTypeLoc();
SourceLocation B = TL.getBeginLoc();
SourceLocation E = TL.getEndLoc();

// want to do something with B and E, e.g. get the characterData from the source manager

See Lexer::getLocForEndOfToken and Lexer::getSpelling.