context of file given start and end StartLocation

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

context of file given start and end StartLocation

manavender reddy
Hi,

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


Thanks
Manavender

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: context of file given start and end StartLocation

Jan Bierbaum-2
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 ;-)


Regards, Jan.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: context of file given start and end StartLocation

Chris Lattner
In reply to this post by manavender reddy

On Aug 15, 2010, at 7:25 PM, manavender reddy wrote:

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

Sure, use the SourceManager::getCharacterData method.

-Chris


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev