[cfe-dev] Getting the same begin and end locations from TypeLoc?

Richard Smith richard at metafoo.co.uk
Tue May 15 21:12:49 PDT 2012

On Tue, May 15, 2012 at 8:05 PM, Lukhnos Liu <lukhnos at lukhnos.org> wrote:

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

See Lexer::getLocForEndOfToken and Lexer::getSpelling.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120515/2ed20674/attachment.html>

More information about the cfe-dev mailing list