Iterating over a CFGBlock excluding subexpressions

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

Iterating over a CFGBlock excluding subexpressions

suyash singh via cfe-dev
Hi all,

Is there some way to iterate over a CFGBlock excluding subexpressions?

Currently I iterate over a CFGBlock as e.g.

   const CFGBlock *block;
   /* ... */
   for (const auto &I : *block) {
     if (Optional<CFGStmt> S = I.getAs<CFGStmt>())
       foo(const_cast<Stmt*>(S->getStmt()));
   }

where function foo *recursively* descends into statements.  This has the
drawback that I visit some (sub)expressions twice: one time via the loop and
another time by recursively descending into subexpressions via function foo.
For example, a CFGBlock for a single statement e1=e2 looks as follows:

   <begin>  (e2)  (e1)  (e1=e2)  <end>

Therefore, I would first call foo for e2, then for e1, and in the end for e1=e2
where I would recursively call foo for e1 and e2 again. Although they have been
visited previously.

Maybe this is not the supposed way to iterate over a CFGBlock?

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

Re: Iterating over a CFGBlock excluding subexpressions

suyash singh via cfe-dev
How did you get the CFG? There is a build option called setAlwaysAdd that you probably set before the CFG was built.

On Mon, 16 Mar 2020 at 17:13, Stefan Schulze Frielinghaus via cfe-dev <[hidden email]> wrote:
Hi all,

Is there some way to iterate over a CFGBlock excluding subexpressions?

Currently I iterate over a CFGBlock as e.g.

   const CFGBlock *block;
   /* ... */
   for (const auto &I : *block) {
     if (Optional<CFGStmt> S = I.getAs<CFGStmt>())
       foo(const_cast<Stmt*>(S->getStmt()));
   }

where function foo *recursively* descends into statements.  This has the
drawback that I visit some (sub)expressions twice: one time via the loop and
another time by recursively descending into subexpressions via function foo.
For example, a CFGBlock for a single statement e1=e2 looks as follows:

   <begin>  (e2)  (e1)  (e1=e2)  <end>

Therefore, I would first call foo for e2, then for e1, and in the end for e1=e2
where I would recursively call foo for e1 and e2 again. Although they have been
visited previously.

Maybe this is not the supposed way to iterate over a CFGBlock?

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

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

Re: Iterating over a CFGBlock excluding subexpressions

suyash singh via cfe-dev
Yes, that's it! I was reusing an AnalysisDeclContext from which I
derived the CFG. However, previous analyses set some build options.

Thanks for your help!

On Mon, Mar 16, 2020 at 05:44:38PM +0100, Gábor Horváth wrote:

> How did you get the CFG? There is a build option called setAlwaysAdd that
> you probably set before the CFG was built.
>
> On Mon, 16 Mar 2020 at 17:13, Stefan Schulze Frielinghaus via cfe-dev <
> [hidden email]> wrote:
>
> > Hi all,
> >
> > Is there some way to iterate over a CFGBlock excluding subexpressions?
> >
> > Currently I iterate over a CFGBlock as e.g.
> >
> >    const CFGBlock *block;
> >    /* ... */
> >    for (const auto &I : *block) {
> >      if (Optional<CFGStmt> S = I.getAs<CFGStmt>())
> >        foo(const_cast<Stmt*>(S->getStmt()));
> >    }
> >
> > where function foo *recursively* descends into statements.  This has the
> > drawback that I visit some (sub)expressions twice: one time via the loop
> > and
> > another time by recursively descending into subexpressions via function
> > foo.
> > For example, a CFGBlock for a single statement e1=e2 looks as follows:
> >
> >    <begin>  (e2)  (e1)  (e1=e2)  <end>
> >
> > Therefore, I would first call foo for e2, then for e1, and in the end for
> > e1=e2
> > where I would recursively call foo for e1 and e2 again. Although they have
> > been
> > visited previously.
> >
> > Maybe this is not the supposed way to iterate over a CFGBlock?
> >
> > Cheers,
> > Stefan
> > _______________________________________________
> > cfe-dev mailing list
> > [hidden email]
> > https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
> >
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev