<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Alexander,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I agree that this behavior is rather confusing, especially to first-time users. The cause of the issue is that Expr derives from Stmt so that Exprs can appear directly in, for example, compound statements, rather than being wrapped in an explicit node to represent statements that consist only of an expression. As far as I understand, this choice to avoid an explicit node had some (positive) performance implications for the AST when it was originally designed. While I'd love to see this choice revisited, I think it would be a significant effort and don't expect it to happen.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Instead, we tend to work around the issue. For getting proper ranges of statements, you might find clang::tooling::getExtendedText/getExtendedRange meet your needs:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><a href="https://github.com/llvm/llvm-project/blob/master/clang/include/clang/Tooling/Transformer/SourceCode.h#L56">https://github.com/llvm/llvm-project/blob/master/clang/include/clang/Tooling/Transformer/SourceCode.h#L56</a><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">For declarations, the (just added!) getAssociatedRange might be even better: <a href="https://github.com/llvm/llvm-project/blob/master/clang/include/clang/Tooling/Transformer/SourceCode.h#L39" style="font-family:Arial,Helvetica,sans-serif">https://github.com/llvm/llvm-project/blob/master/clang/include/clang/Tooling/Transformer/SourceCode.h#L39</a></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Cheers,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Yitzhak</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Feb 25, 2020 at 5:15 PM Alexander Lanin via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="DE"><div><p class="MsoNormal"><span lang="EN-GB">Hello,<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-GB">I’m having trouble with the locations returned by Stmt getEndLoc()/getSourceRange() and am wondering whether this is a bug/flaw:<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB">the returned location sometimes includes and sometimes excludes the semicolon in the end.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB">This gets even more interesting when there are (multiple) comments before the semicolon, simply because the difference between the returned values gets even bigger.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB">Here is an example showing how DeclStmt includes the semicolon, while CallExpr and BinaryOperator exclude the semicolon <a href="http://ce.steveire.com/z/TW3IAG" target="_blank">http://ce.steveire.com/z/TW3IAG</a>.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-GB">So Stmt behaves “completely differently” depending on it’s type, which is no way suggested by it’s interface.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB">Wouldn’t it be better for Stmt-Users if it would always be the same?<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB">(Not sure whether it should always be included or excluded. It’s not even always a semicolon as in that 3rd foo() in the example)<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB"><a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle" target="_blank">https://en.wikipedia.org/wiki/Liskov_substitution_principle</a><u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-GB">Here is one of the effects of this complexity for users of Stmt as they struggle to find that semicolon:<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB"><a href="https://bugs.llvm.org/show_bug.cgi?id=25970" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=25970</a> / <a href="https://reviews.llvm.org/D16267" target="_blank">https://reviews.llvm.org/D16267</a><u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB">Of course it’s fixable there, but that would imply working around the issue in multiple places.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-GB">Regards,<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-GB">Alexander Lanin<u></u><u></u></span></p></div></div>_______________________________________________<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="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>