[cfe-dev] source-code representation of an Expr

John McCall rjmccall at apple.com
Tue Jan 4 15:18:10 PST 2011

On Jan 4, 2011, at 1:10 PM, Sam wrote:
> What is the right way to address the first condition in the code you suggested?  I have been banging my head against my monitor long enough that I now admit defeat.  I've been looking mostly at the APIs for clang::Preprocessor and clang::SourceManager.  I've also read through the Clang Internals document.

getCharacterData will give you the data for whatever location you give it.  The problem with macros is that there are multiple locations involved:  there's the spelling location, where the actual token was written/formed, and there's a chain of arbitrarily many instantiation locations, where the name of some macro was written.  Clang preserves this full macro-instantiation stack, and you can walk up from the spelling location (which is what's generally stored in the AST) through the chain of instantiation locations.  SourceManager::getInstantiationLoc() will jump the whole way for you, or you can walk step-by-step, in which case you need to understand a bit more about how Clang's SourceLocation abstraction works.

A SourceLocation is basically just an offset within a FileID.  A FileID is either a specific inclusion of a physical file or it's a macro instantiation buffer;  SourceLocation::isMacroID() tells you which one, although you can also (at much greater expense) ask the SourceManager for a location's FileID, look up the FileID's SourceManager::SLocEntry, and then ask that.  The SLocEntry for a macro location will have a SourceManager::InstantiationInfo that will tell you the range of the expression from which the macro was instantiated, i.e. moving exactly one level up the instantiation stack.

But I can't tell you what your project should do with this information.


More information about the cfe-dev mailing list