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

John McCall rjmccall at apple.com
Wed Dec 29 13:20:27 PST 2010


On Dec 29, 2010, at 12:53 PM, Sam wrote:
> Yes, I just don't know what to *do* with it ;-) In other words, I don't see a clear use of the SourceManager API once I have the SourceRange to extract the Expr's source-code.  I don't see any API calls that use SourceRange or beginning and ending SourceLocations for source-code extraction.

We should probably make some API for this.

What you can do for now is something like the following:

  SourceRange range = expr->getSourceRange();
  if (range.getBegin().isMacroID() || range.getEnd().isMacroID()) {
    // handle this case
  } else if (!sourceManager.isFromSameFile(range.getBegin(), range.getEnd())) {
    // handle this case
  } else {
    range.setEnd(preprocessor.getLocForEndOfToken(range.getEnd()));
    const char *begin = sourceManager.getCharacterData(range.getBegin());
    const char *end = sourceManager.getCharacterData(range.getEnd());
    llvm::StringRef string(begin, end - begin);
    // now you can do whatever you want
  }

John.



More information about the cfe-dev mailing list