<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">I also wrote my own but I realize today that a function already exists: Lexer::getSourceText()</p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "> </p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">> llvm::StringRef s = Lexer::getSourceText(CharSourceRange::getTokenRange(Range),</p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">> SM, LangOptions);</p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br /><br />On Wednesday, June 19, 2013 07:31:58 PM Thompson, John wrote:<br /></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Is there a function somewhere for getting a source string from a SourceRange using a SourceManager?</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"> </p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">I couldn’t find one so I wrote:</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"> </p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;">std::string PreprocessorCallbacks::getSourceSnippet(SourceRange sourceRange) {</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> SourceLocation bLoc(sourceRange.getBegin());</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> SourceLocation eLoc(sourceRange.getEnd());</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#008000;">// Decompose the locations into FID/Offset pairs.</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> std::pair<FileID, </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">unsigned</span><span style=" font-family:'Consolas'; font-size:10pt;">> bLocInfo = PP.getSourceManager().getDecomposedLoc(bLoc);</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> std::pair<FileID, </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">unsigned</span><span style=" font-family:'Consolas'; font-size:10pt;">> eLocInfo = PP.getSourceManager().getDecomposedLoc(eLoc);</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> FileID FID = bLocInfo.first;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">unsigned</span><span style=" font-family:'Consolas'; font-size:10pt;"> bFileOffset = bLocInfo.second;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">unsigned</span><span style=" font-family:'Consolas'; font-size:10pt;"> eFileOffset = eLocInfo.second;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">unsigned</span><span style=" font-family:'Consolas'; font-size:10pt;"> length = eFileOffset - bFileOffset;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#008000;">// Get information about the buffer it points into.</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">bool</span><span style=" font-family:'Consolas'; font-size:10pt;"> Invalid = </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">false</span><span style=" font-family:'Consolas'; font-size:10pt;">;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">const</span><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">char</span><span style=" font-family:'Consolas'; font-size:10pt;"> *BufStart = PP.getSourceManager().getBufferData(FID, &Invalid).data();</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">if</span><span style=" font-family:'Consolas'; font-size:10pt;"> (Invalid)</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">return</span><span style=" font-family:'Consolas'; font-size:10pt;"> std::string();</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#008000;">// Rewind from the current position to the start of the line.</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">const</span><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">char</span><span style=" font-family:'Consolas'; font-size:10pt;"> *bPtr = BufStart + bFileOffset;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#008000;">// Trim snippet.</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">while</span><span style=" font-family:'Consolas'; font-size:10pt;"> ((*bPtr <= </span><span style=" font-family:'Consolas'; font-size:10pt; color:#a31515;">' '</span><span style=" font-family:'Consolas'; font-size:10pt;">) && (length != 0)) {</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> bPtr++;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> length--;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> }</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">while</span><span style=" font-family:'Consolas'; font-size:10pt;"> ((length != 0) && (bPtr[length - 1] <= </span><span style=" font-family:'Consolas'; font-size:10pt; color:#a31515;">' '</span><span style=" font-family:'Consolas'; font-size:10pt;">))</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> length--;</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span><span style=" font-family:'Consolas'; font-size:10pt; color:#0000ff;">return</span><span style=" font-family:'Consolas'; font-size:10pt;"> std::string(bPtr, length);</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;">}</span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Consolas'; font-size:10pt;"> </span></p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Is there something better?</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"> </p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Thanks.</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"> </p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">-John</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:40px; margin-right:40px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"> </p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br /><br /></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">-- </p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Guillaume Papin</p></body></html>