[cfe-dev] How to determine the corresponding block of a statement?

Alex L via cfe-dev cfe-dev at lists.llvm.org
Mon Jul 31 07:58:52 PDT 2017


Something like this could work:

If you have a Stmt * that corresponds to the return statement in your
example, you can use the `getParents()` method from `ASTContext` to get the
list of its parents. Then you can check whether the `if` has a body with
braces by iterating over the parents until you find the `if`, and checking
if the `if`s body is a CompoundStmt.

On 31 July 2017 at 15:49, Marcel Schaible via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hi everyone,
>
> what is the proper way to determine the surrounding block/scope of a
> statement?
>
> I want to insert in each branch some diagnostic code like e.g.
>
> int foo(void) {
>
>     if ( ...)  return 0;
>
>     else {
>
>         return 1;
>
>     }
>
> }
>
> transform into:
>
> int foo(void) {
>
>     if ( ...) {
>
>         somediagnostic();
>
>         return 0;
>
>     } else {
>
>         somediagnostic();
>
>         return 1;
>
>     }
>
> }
>
>
> Thanks
>
> Marcel
>
> _______________________________________________
> 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/20170731/465ac363/attachment.html>


More information about the cfe-dev mailing list