Classify variables of a loop/compound statement/etc.

Hi all,

I would like to develop a tool that prints the variables of a concrete loop/compount statement/etc. The information that I need to print for each variable is:

  • read variables, write variables and rw variables
  • And if it is local or global variable.

Any help to start?

Regards,

Luis.

Luis,
My 2 cents. Maybe you can create a recursive visitor and use the VisitStmt method. Using a cast you can understand if it is an if statement and if yes you could call, for example,getThen() method or something similar.

What you mean with read/write variables?

I think you can understand if it is global or not taking the declaration point using something like getDecl() on the DeclRefExpr.

I’m sure that I said something wrong. Any better idea?

Thanks,
Alberto

Alberto: Thanks for your help.

Maybe you can create a recursive visitor and use the VisitStmt method. Using a cast you can understand if it is an if statement and if yes you could call, for example,getThen() method or something similar.

Is there any example code to start?

What you mean with read/write variables?

I’ll explain with a sample:

std::vector A(M);
std::vector B(M);

/* Loop to analyze */
for (int i=0; i<M; ++i) {
A[i] = B[i];
}

I would like to get the following information:

Local: i (rw)
External: A(w),B (r), M(r)

where r means that the variable is read into the code, and w means that the variable is modified (in someway)

Regards,

Luis.