This is an archive of the discontinued LLVM Phabricator instance.

[ASTMatchers] WIP add hisFinalExpr to StmtExpr
AbandonedPublic

Authored by njames93 on Mar 15 2020, 7:36 AM.

Details

Summary

Adds a traversal matcher hasFinalExpr to match the last statement in a gnu statement expression if the last statement is an expression.

Diff Detail

Event Timeline

njames93 created this revision.Mar 15 2020, 7:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2020, 7:36 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

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 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 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;});?

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

njames93 updated this revision to Diff 250569.Mar 16 2020, 8:34 AM
  • Rename matcher to hasFinalExpr
  • Don't review it yet
njames93 planned changes to this revision.Mar 16 2020, 8:35 AM
njames93 retitled this revision from [ASTMatchers] Extend hasReturnValue to GNU StmtExpr to [ASTMatchers] WIP add hisFinalExpr to StmtExpr.Mar 17 2020, 7:09 AM
njames93 edited the summary of this revision. (Show Details)
njames93 abandoned this revision.Nov 24 2022, 4:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 24 2022, 4:40 AM