Right off, a message elsewhere pointed me to an improvement:
std::string PreprocessorCallbacks::getSourceSnippet(SourceRange sourceRange) {
SourceLocation bLoc(sourceRange.getBegin());
SourceLocation eLoc(sourceRange.getEnd());
// Decompose the locations into FID/Offset pairs.
std::pair<FileID, unsigned> bLocInfo = PP.getSourceManager().getDecomposedLoc(bLoc);
std::pair<FileID, unsigned> eLocInfo = PP.getSourceManager().getDecomposedLoc(eLoc);
FileID FID = bLocInfo.first;
unsigned bFileOffset = bLocInfo.second;
unsigned eFileOffset = eLocInfo.second;
unsigned length = eFileOffset - bFileOffset;
// Get information about the buffer it points into.
bool Invalid = false;
const char *BufStart = PP.getSourceManager().getBufferData(FID, &Invalid).data();
if (Invalid)
return std::string();
return StringRef(BufStart + bFileOffset, length).trim().str();
}
Anything better?