Finding Stmt Kind

Hi all,

Is it possible to get the kind of a Stmt object in clang?
The only solution that I know of is using dyn_cast. However, in this solution, I have to check for every kind of Stmt, e.g.
if (CompoundStmt *CS = dyn_cast(d))

else if (IfStmt *IS = dyn_cast(d))

Is there a better solution or trick?

Thanks,
Pardis

Hi Pardis,

Take a look at Stmt::getStmtClass().

18.11.2018 21:20, Pardis Pashakhanloo via cfe-dev пишет:

The usual approach is to use the casting machinery, but you can also
use the statement kind directly by calling Stmt::getStmtClass(),

~Aaron

If your purpose is to make a large switch over the kind of Stmt, i guess i should also recommend StmtVisitor, which is essentially the same thing - a huge switch, but it keeps the additional option to specify the default behavior for intermediate sub-classes of statements without listing all members of such sub-classes explicitly.

For example, yes, VisitCallExpr would be called for every CUDAKernelCallExpr unless VisitCUDAKernelCallExpr is defined explicitly. But you still don't need to explicitly remember that and spell this out in your code, and you (or people after you) don't need to update your switch later every time they add a new kind of CallExpr as long as it truly doesn't require any special behavior.

So it kinda combines the benefits of both the dyn_cast chain approach and the switch approach, and additionally ensures correct control flow and scope separation in your code.