How to determine the corresponding block of a statement?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to determine the corresponding block of a statement?

Sumner, Brian via cfe-dev

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
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to determine the corresponding block of a statement?

Sumner, Brian via cfe-dev
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 <[hidden email]> 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
[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
Loading...