[cfe-dev] SourceLocation::isFileID() and isMacroID() ?

Chris Lattner clattner at apple.com
Fri Jan 16 10:14:35 PST 2009


On Jan 16, 2009, at 1:07 AM, Paolo Bolzoni wrote:
> dear cfe-dev list,
>
> When visiting the clang AST of a C source, I often get  
> SourceLocations that
> aren't valid or aren't FileID. Why some locations are not valid?

Invalid source locations come from two things 1) synthesized AST  
objects that don't exist in the source code, and 2) bugs in the parser.

> And what does it mean that a location isn't a file ID?

This currently means it is coming from a macro expansion.

> It seems to happen almost always in asm file scope declarations.

That is probably a bug in the parser, not preserving location info  
correctly.

> Moreover, what does isMacroID() false or true mean?

By example, consider this file:

#define x y

x z

The first token will be a SourceLocation that is a macro ID.   
SourceManager will be able to tell you it's "spelling" location (which  
is "y") as well as its "instantiation" location (which is the x before  
the z).  The second location is a file ID.  SourceManager will tell  
you that it's spelling and instantiation location are the same, both  
the z.

I plan to make some major changes in this area today and over the  
weekend.  You might want to wait until the dust settles before  
grinding on details of SourceLocation itself.  In any case, I'd  
recommend using the SourceManager methods like "getInstantiationLoc"  
and "getSpellingLoc" rather than poking at the SourceLocation itself.

-Chris



More information about the cfe-dev mailing list