@@ -132,15 +132,15 @@ private module Cached {
132132 or
133133 nodeFrom .asExpr ( ) = nodeTo .asExpr ( ) .( CfgNodes:: ExprNodes:: CaseExprCfgNode ) .getBranch ( _)
134134 or
135- exists ( CfgNodes:: ExprCfgNode exprTo , ExprReturnNode n |
135+ exists ( CfgNodes:: ExprCfgNode exprTo , ReturningStatementNode n |
136136 nodeFrom = n and
137137 exprTo = nodeTo .asExpr ( ) and
138- n .getKind ( ) instanceof BreakReturnKind and
138+ n .getReturningNode ( ) . getNode ( ) instanceof BreakStmt and
139139 exprTo .getNode ( ) instanceof Loop and
140- nodeTo .asExpr ( ) .getAPredecessor ( any ( SuccessorTypes:: BreakSuccessor s ) ) = n .getExprNode ( )
140+ nodeTo .asExpr ( ) .getAPredecessor ( any ( SuccessorTypes:: BreakSuccessor s ) ) = n .getReturningNode ( )
141141 )
142142 or
143- nodeFrom .asExpr ( ) = nodeTo .( ExprReturnNode ) . getExprNode ( ) .getReturnedValueNode ( )
143+ nodeFrom .asExpr ( ) = nodeTo .( ReturningStatementNode ) . getReturningNode ( ) .getReturnedValueNode ( )
144144 or
145145 nodeTo .asExpr ( ) =
146146 any ( CfgNodes:: ExprNodes:: ForExprCfgNode for |
@@ -182,6 +182,28 @@ class SsaDefinitionNode extends NodeImpl, TSsaDefinitionNode {
182182 override string toStringImpl ( ) { result = def .toString ( ) }
183183}
184184
185+ /**
186+ * A value returning statement, viewed as a node in a data flow graph.
187+ *
188+ * Note that because of control-flow splitting, one `ReturningStmt` may correspond
189+ * to multiple `ReturningStatementNode`s, just like it may correspond to multiple
190+ * `ControlFlow::Node`s.
191+ */
192+ class ReturningStatementNode extends NodeImpl , TReturningNode {
193+ private CfgNodes:: ReturningCfgNode n ;
194+
195+ ReturningStatementNode ( ) { this = TReturningNode ( n ) }
196+
197+ /** Gets the expression corresponding to this node. */
198+ CfgNodes:: ReturningCfgNode getReturningNode ( ) { result = n }
199+
200+ override CfgScope getCfgScope ( ) { result = n .getScope ( ) }
201+
202+ override Location getLocationImpl ( ) { result = n .getLocation ( ) }
203+
204+ override string toStringImpl ( ) { result = n .toString ( ) }
205+ }
206+
185207private module ParameterNodes {
186208 abstract private class ParameterNodeImpl extends ParameterNode , NodeImpl { }
187209
@@ -241,29 +263,49 @@ abstract class ReturnNode extends Node {
241263}
242264
243265private module ReturnNodes {
266+ private predicate isValid ( CfgNodes:: ReturningCfgNode node ) {
267+ exists ( ReturningStmt stmt , Callable scope |
268+ stmt = node .getNode ( ) and
269+ scope = node .getScope ( )
270+ |
271+ stmt instanceof ReturnStmt and
272+ ( scope instanceof Method or scope instanceof SingletonMethod or scope instanceof Lambda )
273+ or
274+ stmt instanceof NextStmt and
275+ ( scope instanceof Block or scope instanceof Lambda )
276+ or
277+ stmt instanceof BreakStmt and
278+ ( scope instanceof Block or scope instanceof Lambda )
279+ )
280+ }
281+
244282 /**
245283 * A data-flow node that represents an expression returned by a callable,
246284 * either using an explict `return` statement or as the expression of a method body.
247285 */
248- class ExprReturnNode extends ReturnNode , NodeImpl , TReturningNode {
286+ class ExplicitReturnNode extends ReturnNode , ReturningStatementNode {
249287 private CfgNodes:: ReturningCfgNode n ;
250288
251- ExprReturnNode ( ) { this = TReturningNode ( n ) }
252-
253- /** Gets the statement corresponding to this node. */
254- CfgNodes:: ReturningCfgNode getExprNode ( ) { result = n }
289+ ExplicitReturnNode ( ) {
290+ isValid ( this .getReturningNode ( ) ) and
291+ n .getASuccessor ( ) .( CfgNodes:: AnnotatedExitNode ) .isNormal ( ) and
292+ n .getScope ( ) instanceof Callable
293+ }
255294
256295 override ReturnKind getKind ( ) {
257296 if n .getNode ( ) instanceof BreakStmt
258297 then result instanceof BreakReturnKind
259298 else result instanceof NormalReturnKind
260299 }
300+ }
261301
262- override CfgScope getCfgScope ( ) { result = n .getScope ( ) }
263-
264- override Location getLocationImpl ( ) { result = n .getLocation ( ) }
302+ class ExprReturnNode extends ReturnNode , ExprNode {
303+ ExprReturnNode ( ) {
304+ this .getExprNode ( ) .getASuccessor ( ) .( CfgNodes:: AnnotatedExitNode ) .isNormal ( ) and
305+ this .getEnclosingCallable ( ) instanceof Callable
306+ }
265307
266- override string toStringImpl ( ) { result = n . toString ( ) }
308+ override ReturnKind getKind ( ) { result instanceof NormalReturnKind }
267309 }
268310}
269311
0 commit comments