Getting all variable declaration which is inside a loop

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting all variable declaration which is inside a loop

Nat! via cfe-dev
Hi everyone,

I want to get all variable declaration which is inside a loop ( whose
scope limited to that loop only). Can someone please tell me how can i do
this?

Ex

int a,bl
for(){
int c,d;
}
Output should be c,d

--
Thanks and Regards
Awanish Pandey
PhD, CSE
IIT Kanpur

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Getting all variable declaration which is inside a loop

Nat! via cfe-dev
I'm reasonably sure there is no "magic" way to find this. You can use an AST Matcher to find loops, but you then have to step through the content of the loop. Or you can use a RecursiveASTVisitor, and find the for-statements, then find variable declarations within that, for example using a "map" to track which loop it is that contains what declarations.

RecursiveASTVisitor example:
https://clang.llvm.org/docs/RAVFrontendAction.html

A stack overflow question on using AST Matcher to find loops:
http://stackoverflow.com/questions/36880574/finding-nested-loops-with-clang-ast-statementmatcher

--
Mats

On 3 May 2017 at 13:47, via cfe-dev <[hidden email]> wrote:
Hi everyone,

I want to get all variable declaration which is inside a loop ( whose
scope limited to that loop only). Can someone please tell me how can i do
this?

Ex

int a,bl
for(){
int c,d;
}
Output should be c,d

--
Thanks and Regards
Awanish Pandey
PhD, CSE
IIT Kanpur

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Getting all variable declaration which is inside a loop

Nat! via cfe-dev
The following matchers should works

varDecl(hasAncestor(whileStmt()))
varDecl(hasAncestor(forStmt())) 
On 03/05/2017 15:27, mats petersson via cfe-dev wrote:
I'm reasonably sure there is no "magic" way to find this. You can use an AST Matcher to find loops, but you then have to step through the content of the loop. Or you can use a RecursiveASTVisitor, and find the for-statements, then find variable declarations within that, for example using a "map" to track which loop it is that contains what declarations.

RecursiveASTVisitor example:
https://clang.llvm.org/docs/RAVFrontendAction.html

A stack overflow question on using AST Matcher to find loops:
http://stackoverflow.com/questions/36880574/finding-nested-loops-with-clang-ast-statementmatcher

--
Mats

On 3 May 2017 at 13:47, via cfe-dev <[hidden email]> wrote:
Hi everyone,

I want to get all variable declaration which is inside a loop ( whose
scope limited to that loop only). Can someone please tell me how can i do
this?

Ex

int a,bl
for(){
int c,d;
}
Output should be c,d

--
Thanks and Regards
Awanish Pandey
PhD, CSE
IIT Kanpur

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev