FW: get the text in aC file between 2 SourceLocations

Thanks for your quick response Snaroff.

I choose to change the function Lexer::LexNumericConstant and generate my own kind of token if I find an s at the end of an numerical_constant.

Now I want to separate the number from the s.

Is there an easy way of getting the text of a token in the C file? I searched for this a while ago too but couldn’t find it. I found the SourceManager.GetCharacterData(Sourcelocation ) function, but this returns me all characters starting from the Sourceloaction.

Is there a way to get a char * of text between 2 SourceLocations ?

Thanks for all the help,

Bas

Thanks for your quick response Snaroff.
I choose to change the function Lexer::LexNumericConstant and generate my own kind of token if I find an s at the end of an numerical_constant.
Now I want to separate the number from the s.

Is there an easy way of getting the text of a token in the C file?

Not directly. You need to go through the SourceManager as follows…

const char *sourceText = SM->getCharacterData(Tok.getLocation());

If you want the token type name, this will suffice…

const char *tokenName = Tok.getName();

I searched for this a while ago too but couldn’t find it. I found the SourceManager.GetCharacterData(Sourcelocation ) function, but this returns me all characters starting from the Sourceloaction.
Is there a way to get a char * of text between 2 SourceLocations ?

These should do the trick…

// converts SourceLocation’s into “char *'s”
const char *startBuf = SM->getCharacterData(LocStart);
const char *endBuf = SM->getCharacterData(LocEnd);

// converts a “char *” offset into a SourceLocation

SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);

snaroff