Getting out body of a while Statement

i need help to get out the body of a while statement.

While( a > 3) {

cout << “hello” <<endl; << – I wan to copy out this line and store into a variable.


This is a example , i want to take out whats inside of the while statement, and if its possible store it into a variable so i can print the result out somewhere.

Much appreciated

John Tan.

Really depends on what you want to achieve [in the big picture, not “I want a variable holding the content inside the while”, but what you are actually planning to do beyond getting it into a variable - do you want to edit the source file to add or remove something, check that the body does/doesn’t do something]

Something involving the ASTMatcher would be a starting point:

If you want the actual source-code, then you’ll also need to get out the source location, and use sourcemanager to get the “section of source code within the body into a string”, but consider that you can have really “interesting” code:

while( a > 3 )

#include “mycode.h”

while( a > 3 )
#include “mycode.h”

[where the content in mycode.h contains not just the loop body, but also further code that continues AFTER the loop.]


#define SOME_MACRO(x) while(a < (x))




while( a > 3 )


where SOME_MACRO expands to some rather large chunk of code - and knowing the “source” is not really helpful in either of these cases. And of course, like the #include sample, you can have a the loop body end part way through the macro, so you probably don’t really want to rely on the “string contains the body of the loop” if you want to do something with the content of the loop that is of any importance. These are of course simple examples of “unusual programming”, but I guarantee that if you look at enoug code, you’ll find SOMETHING like that.

So, depending on what you actualy want to achieve, you may want to NOT try to deal with this as files/text strings, but as AST-code.

Please: unless there are specific reasons to do so (e.g. discussing personal things), always reply to the mailing list and all personal participants taking part in the thread. It helps other people being able to chime in, if they have better/different suggestions, as well as someone else seeing the thread understanding what the outcome was.

while (a>3){

goto label1;



cout << “hello” << endl;

goto label2;


This would be the final result. all the labels would be outside of the while loop.

i know while Stmt has a getBody(). But i am unsure on how to use to it together with the astmatcher

i use the rewrite functionality to write to the body - i use this.

But now , i am stumped on how to use the rewriter functionality to get the body.

i did this in my method thats binded to the AstMatcher for while loop :

const WhileStmt *WS = Result.Nodes.getNodeAsclang::WhileStmt(“whileStmt”)

stmt s1 = WS->getBody(); ← i am not able to print out this with the rewriter functionlity.

my astMatcher: Matcher.addMatcher(whileStmt(hasDescendant(compoundStmt())).bind(“whileStmt”), &HandlerForWhile);

Update on what i have done :

const WhileStmt *WS = Result.Nodes.getNodeAsclang::WhileStmt(“whileStmt”);

Stmt *s = WS->getBody(); ← i am assuming this is can get the body of while statment

As for the rewriter functionality , i am not able to print out Stmt.

Any advices ???

Not sure I can help on the rewriter - I only know of it’s existence.

However, I’m reasonably sure you:

  1. Should make absolutely sure this is worth doing, doing it by hand on a real file that is part of or resembles the real project.

  2. Need to take into account what to do with cases like


T1 x;
while( … ) { T2 x; … use x … }


while(…) { … break; … }


while(…) { … continue; … }


while(…) { … goto …; … }


while(…) { … while( … ) { … } … }


try { … while () … } catch(…) { … }


while { std::lock(…) … }

These are just a few examples I can think of that will “make things difficult” - and I’m pretty sure there are MANY others, unless your programming style is sufficiently restrictive to forbid the majority of those.

And of course, if you plan to do this for for, do - while and switch, you’d better cover the same sort of problems there too.