[cfe-dev] Getting all variable declaration which is inside a loop

David Come via cfe-dev cfe-dev at lists.llvm.org
Wed May 3 06:38:14 PDT 2017


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 <cfe-dev at lists.llvm.org 
> <mailto:cfe-dev at lists.llvm.org>> 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
>     cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
>     http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>     <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>
>
>
>
>
> _______________________________________________
> 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/20170503/23d0c04f/attachment.html>


More information about the cfe-dev mailing list