context of file given start and end StartLocation

Hi,

I was wondering is it possible to get text from file given start and end locations (SourceLocation).

Thanks
Manavender

manavender reddy meinte am 16.08.2010 04:25:

I was wondering is it possible to get text from file given start and end
locations (SourceLocation).

std::pair<clang::FileID, unsigned>
  startLocPair = SM.getDecomposedLoc(startLocation),
  endLocPair = SM.getDecomposedLoc(endLocation);

if (startLocPair.first != endLocPair.first)
  //do something

bool invalid;
const char *buffer = SM.getBufferData(startLocPair.first,
  &invalid).data();

if (invalid)
  //do something

const char *codeStart = buffer + startLocPair.second,
           *codeEnd = buffer + endLocPair.second;

I did not test this and probably there's a better solution anyway, but
it should work :wink:

Regards, Jan.

Sure, use the SourceManager::getCharacterData method.

-Chris