<div dir="ltr">Looks like semicolon is not considered a token, so it does not have any representation in AST. <div><br></div><div>So for example to find location after semicolon in this IfStmt:</div><div><div><div style="font-size:12.8px"><span style="font-family:monospace,monospace;font-size:12.8px">    if (a)</span><br></div><div style="font-size:12.8px"><font face="monospace, monospace">       a--;</font></div></div></div><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.8px"><font face="arial, helvetica, sans-serif">I have to search for semicolon myself:</font></div><div style="font-size:12.8px"><font face="arial, helvetica, sans-serif"><br></font></div><div style="font-size:12.8px"><font face="arial, helvetica, sans-serif"><br></font></div><div><div><span style="font-size:12.8px"><font face="monospace, monospace">    const char* endCharPtr = sourceManager.getCharacterData(locEnd);</font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace">    size_t offsetSemicolon = 0;</font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace"><br></font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace">    while (endCharPtr[offsetSemicolon-1]) != ';' )</font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace">        offsetSemicolon ++;</font></span></div><div style="font-family:arial,helvetica,sans-serif;font-size:12.8px"><br></div></div><div style="font-family:arial,helvetica,sans-serif;font-size:12.8px"><br></div><div style="font-family:arial,helvetica,sans-serif;font-size:12.8px">if there is a CompoundStmt in Then body, like</div><div style="font-size:12.8px"><div style="font-size:small"><div style="font-size:12.8px"><span style="font-family:monospace,monospace;font-size:12.8px">    if (a)</span><br></div><div style="font-size:12.8px"><font face="monospace, monospace">       { a--; }</font></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><span style="font-family:arial,helvetica,sans-serif;font-size:12.8px">Location of final curly brace will be detected correctly. </span><br></div><div><font face="monospace, monospace"><br></font></div></div></div><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-10-22 10:09 GMT-07:00 Roman Popov <span dir="ltr"><<a href="mailto:ripopov@gmail.com" target="_blank">ripopov@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Thanks you!<div dir="auto">So I've misunderstood the meaning of API. </div><div dir="auto">Here is also related discussion on stackoverflow : <a href="https://stackoverflow.com/questions/11083066/getting-the-source-behind-clangs-ast" target="_blank">https://stackoverflow.com/<wbr>questions/11083066/getting-<wbr>the-source-behind-clangs-ast</a></div><div dir="auto"><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Oct 22, 2017 7:41 AM, "Don Hinton" <<a href="mailto:hintonda@gmail.com" target="_blank">hintonda@gmail.com</a>> wrote:<br type="attribution"><blockquote class="m_-2786114651091217190quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>You're assuming that the length of the end token is 1, but you should use its actual length instead.</div><div><br></div><div>So, instead of getting the end location of the statement, you should get the end token, then use its location and length, e.g., <font face="monospace, monospace">tok.getLocation().<span style="font-size:12.8px">getLocWithOf<wbr>fset(tok.getLength())</span></font><span style="font-family:monospace,monospace;font-size:12.8px">.</span></div><div><br></div><div>hth...</div><div>don</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div class="m_-2786114651091217190elided-text">On Sat, Oct 21, 2017 at 9:28 PM, Roman Popov via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_-2786114651091217190elided-text"><div dir="ltr">Hello, <div>I want to emit some text after each if statement in source code. When testing a tool I've found that ifStmt->getSourceRange() sometimes returns wrong end location. </div><div><br></div><div>In RecursiveASTVisitor I have following code to insert "$" before and after each if:</div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">    bool VisitIfStmt(clang::IfStmt* ifStmt)</font></div><div><font face="monospace, monospace">    {</font></div><div><font face="monospace, monospace">        clang::tooling::Replacement rep0(compInst.getSourceManager<wbr>(), ifStmt->getLocStart(), 0, "$");</font></div><div><font face="monospace, monospace">        handleAllErrors ( replacements.add(rep0) );</font></div><div><font face="monospace, monospace">        clang::tooling::Replacement rep1(compInst.getSourceManager<wbr>(), ifStmt->getLocEnd().getLocWith<wbr>Offset(1), 0, "$");</font></div><div><font face="monospace, monospace">        handleAllErrors ( replacements.add(rep1) );</font></div><div><font face="monospace, monospace">        return true;</font></div><div><font face="monospace, monospace">    }</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><div>    Replacements &replacements;</div><div>    ASTContext *Context;</div><div>    clang::CompilerInstance &compInst;</div><div><br></div></font></div><div><div>When testing on following code:</div></div><div><br></div><div><font face="monospace, monospace">    int a;</font></div><div><font face="monospace, monospace">    if (a)<br></font></div><div><div><font face="monospace, monospace">       a--;</font></div></div><div><br></div><div><div>Result is:</div></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">    $if (a)</font></div><div><font face="monospace, monospace">       a-$-;</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><div>So Clang thinks that if statement ends inside decrement operator. Is it a bug, or I misuse API?</div></div><div><br></div><div>Thanks,</div><div>-Roman</div></div>
<br></div>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>