Hello All;
Given:
void f() {
int a, b, c;
}
I’m wondering if there’s a way to find the SourceLocation and/or SourceRange of a, b and c?
Using VarDecl::getSourceRange(), VarDecl::getStartLoc(), etc I get the same values for all instance between the type (int) and the ;.
Using the Lexer::LexFromRawLexer() is tedious and error prone. Any suggestions?
Thanks in advance.
-Uri
Given:
void f() {
int a, b, c;
}
I’m wondering if there’s a way to find the SourceLocation and/or SourceRange
of a, b and c?
Using VarDecl::getSourceRange(), VarDecl::getStartLoc(), etc I get the same
values for all instance between the type (int) and the ;.
Looking at the ast dump, it seems like a, b and c have the same start
loc, but different end locations. Maybe you can use that?
$ echo 'void f() { int a, b, c; }' | clang -cc1 -ast-dump -
TranslationUnitDecl 0x5fa3fe0 <<invalid sloc>> <invalid sloc>
-TypedefDecl 0x5fa44e0 <<invalid sloc>> <invalid sloc> implicit
__int128_t '__int128'
-TypedefDecl 0x5fa4540 <<invalid sloc>> <invalid sloc> implicit
__uint128_t 'unsigned __int128'
-TypedefDecl 0x5fa4890 <<invalid sloc>> <invalid sloc> implicit
__builtin_va_list '__va_list_tag [1]'
`-FunctionDecl 0x5fa4930 <<stdin>:1:1, col:25> col:6 f 'void ()'
`-CompoundStmt 0x5fa4b50 <col:10, col:25>
`-DeclStmt 0x5fa4b38 <col:12, col:23>
>-VarDecl 0x5fa49e0 <col:12, col:16> col:16 a 'int'
>-VarDecl 0x5fa4a50 <col:12, col:19> col:19 b 'int'
`-VarDecl 0x5fa4ac0 <col:12, col:22> col:22 c 'int'
- Hans
Hi Uri, start locations should be the same but as Hans said ranges aren’t. Doublecheck using -cc1 -ast-dump, and tell us what version of clang are you using.
Hi Uri,
On top of my head, I think you should use VarDecl::getLocation() which points
to the location of the name token. And since it is AFAIK always only one
token, it should be enough. (The end of the range may include the
initializers).