[cfe-dev] SourceRange not as expected for arithmetic types with direct initialization

Firat Kasmis via cfe-dev cfe-dev at lists.llvm.org
Tue Nov 8 11:12:11 PST 2016


The implementation of getSourceRange() reveals, that it returns the end
location of the init-expression. For the arithmetic types, it is just a
Literal, e.g. IntegerLiteral. For the others, it might be InitListExpr,
CXXConstructExpr etc:

int x(123);
int y{456};

    | DeclStmt 0x17c4488 <line:7:5, col:15>
    | `-VarDecl 0x17c43d8 <col:5, col:11> col:9 x 'int' callinit
    |   `-IntegerLiteral 0x17c4438 <col:11> 'int' 123
    |-DeclStmt 0x17c45c0 <line:8:5, col:15>
    | `-VarDecl 0x17c44b0 <col:5, col:14> col:9 y 'int' listinit
    |   `-InitListExpr 0x17c4578 <col:10, col:14> 'int'
    |     `-IntegerLiteral 0x17c4510 <col:11> 'int' 456

Output:
int x(123
int y{456}

Obviously, the literal ends at 3 and the list expression at }. Something
like

    |-DeclStmt 0x17c4488 <line:7:5, col:15>
    | `-VarDecl 0x17c43d8 <col:5, col:14> col:9 x 'int' callinit
    |   `-InitCallExpr 0xdeadbeef <col:10, col:14> 'int'
    |     `-IntegerLiteral 0x17c4438 <col:11> 'int' 123
    |-DeclStmt 0x17c45c0 <line:8:5, col:15>
    | `-VarDecl 0x17c44b0 <col:5, col:14> col:9 y 'int' listinit
    |   `-InitListExpr 0x17c4578 <col:10, col:14> 'int'
    |     `-IntegerLiteral 0x17c4510 <col:11> 'int' 456

would have helped me, but there is probably no benefit with that.

2016-11-07 0:08 GMT+01:00 Firat Kasmis <firat.kasmis at gmail.com>:

> Hi,
>
> following code:
>
> int v(8);
> bool b(false);
> double d(0.5);
> Structy s(23);
>
> int v1, v2(3), v3, v4(4);
> Structy s1, s2(23), s3, s4(3);
>
> if(const auto *vardecl = Result.Nodes.getNodeAs<clang::
> VarDecl>("vardecl"))
> {
>     llvm::outs() << Lexer::getSourceText(CharSourceRange::
> getTokenRange(vardecl->getSourceRange()), *SM, LO) << "\n";
> }
>
> outputs:
>
> int v(8
> bool b(false
> double d(0.5
> Structy s(23)
>
> int v1
> int v1, v2(3
> int v1, v2(3), v3
> int v1, v2(3), v3, v4(4
>
> Structy s1
> Structy s1, s2(23)
> Structy s1, s2(23), s3
> Structy s1, s2(23), s3, s4(3)
>
>
> As can be seen, the last parenthesis is missing for the arithmetic types.
> The SourceRange is for any other kind of initialization right, except for
> this case.
>
> Regards,
> Firat
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161108/a89f6879/attachment.html>


More information about the cfe-dev mailing list