[cfe-dev] Finding Stmt Kind

Artem Dergachev via cfe-dev cfe-dev at lists.llvm.org
Sun Nov 18 11:58:56 PST 2018


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.

On 11/18/18 10:20 AM, Pardis Pashakhanloo via cfe-dev wrote:
> 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<CompoundStmt>(d))
>   ....
> else if (IfStmt *IS = dyn_cast<IfStmt>(d))
>   ....
>
> Is there a better solution or trick?
>
> Thanks,
> Pardis
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list