[cfe-dev] Source Location of the return statement.

Alex L via cfe-dev cfe-dev at lists.llvm.org
Mon Sep 11 09:04:44 PDT 2017


Hi,

Stmt::getLocEnd() typically returns a location that points to the start of
the last token. You have to call Lexer::getLocForEndOfToken with the
location of the last token to compute the true end location of a statement
(Please note that ';' won't be included).

I hope this helps,
Alex


On 11 September 2017 at 17:00, Won Tae Joo via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hello,
>
> I was using llvm/clang 3.9 for developement, and something did not work as
> I intended.
>
> Given a code like below, when I do ./clang -cc1 -ast-dump test.cpp,
>
> #include <stdio.h>
> int main(int argc, char ** argv)
> {
>   int mul = 2;
>   mul = 4 * mul;
>   return 0;
> }
>
> it prints an AST like..
>
> `-FunctionDecl 0xd128d48 </home/joo/test.cpp:2:1, line:7:1> line:2:5 main
> 'int (int, char **)'
>   |-ParmVarDecl 0xd128bd0 <col:10, col:14> col:14 argc 'int'
>   |-ParmVarDecl 0xd128c70 <col:20, col:28> col:28 argv 'char **'
>   `-CompoundStmt 0xd128ff0 <line:3:1, line:7:1>
>     |-DeclStmt 0xd128ec8 <line:4:3, col:14>
>     | `-VarDecl 0xd128e48 <col:3, col:13> col:7 used mul 'int' cinit
>     |   `-IntegerLiteral 0xd128ea8 <col:13> 'int' 2
>     |-BinaryOperator 0xd128f90 <line:5:3, col:13> 'int' lvalue '='
>     | |-DeclRefExpr 0xd128ee0 <col:3> 'int' lvalue Var 0xd128e48 'mul'
> 'int'
>     | `-BinaryOperator 0xd128f68 <col:9, col:13> 'int' '*'
>     |   |-IntegerLiteral 0xd128f08 <col:9> 'int' 4
>     |   `-ImplicitCastExpr 0xd128f50 <col:13> 'int' <LValueToRValue>
>     |     `-DeclRefExpr 0xd128f28 <col:13> 'int' lvalue Var 0xd128e48
> 'mul' 'int'
>     `-ReturnStmt 0xd128fd8 <line:6:3, col:10>
>       `-IntegerLiteral 0xd128fb8 <col:10> 'int' 0
> Please note where the end of the BinaryOperator, and the ReturnStmt is
> pointing.
> Either cases was not pointing to the end of the statements but rather
> pointing to the beginning of second 'mul' and the beginning of '0'
> respectively. I've checked in 6.0 for the same thing and happens to point
> to the same location. So I'd assume that I was misunderstanding the usage
> of Stmt::getLocEnd(). May I ask what's the proper way to retrieve the end
> location of a statement?
>
> Thanks,
> Won-Tae
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170911/b1fb4cac/attachment.html>


More information about the cfe-dev mailing list