Adds a traversal matcher hasFinalExpr to match the last statement in a gnu statement expression if the last statement is an expression.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
I don't think this is a natural fit for the functionality. A statement expression doesn't have a return value so much as it is a value expression that can contain multiple statements. To me, at least, this is a bit like saying the comma operator has a return value because you can do int i; i = 1, 2, 3; and it "returns" 3.
Can you explain a bit about why you need this matcher functionality?
I wasn't sure where to best place this and I was well aware of the comma operator acting similarly. The idea behind it is the first N-1 statements in a StmtExpr with N aren't as relevant as the final Stmt if its an Expr Maybe hasFinalExpression(InnerMatcher<Expr>) would be a better way WDYT?
I'm still not understanding your use case, so it's a bit hard for me to tell whether this approach is good or not. Do you have a situation where you cannot match on the expression itself, you also have to know its position? I'm asking because we don't currently have support for querying positional statements (for instance, you cannot ask what the final statement in a CompoundStmt) and statement expressions are a bit strange. For instance, what would the final "expression" be in this legal-but-bizarre statement expression: ({int j = 12; j; int x;});?
It would be the int x; but not matchable as its not an expression. The way StmtExpr works is it evaluates each Stmt in order and returns the result of the last Stmt, if it is an expression. otherwise it basically returns void. Possible use cases are if you want to match on say an if condition, but the condition is inside a StmtExpr. I feel like there could be a better way to get this behaviour but I'm not exactly sure what it is