Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 2102223002: ExitDetector: Examine continue when determining a 'do' (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Addressing Feedback Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/all_the_rest_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.src.generated.resolver; 5 library analyzer.src.generated.resolver;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
(...skipping 3481 matching lines...) Expand 10 before | Expand all | Expand 10 after
3492 * expression, or simple infinite loop such as `while(true)`. 3492 * expression, or simple infinite loop such as `while(true)`.
3493 */ 3493 */
3494 class ExitDetector extends GeneralizingAstVisitor<bool> { 3494 class ExitDetector extends GeneralizingAstVisitor<bool> {
3495 /** 3495 /**
3496 * Set to `true` when a `break` is encountered, and reset to `false` when a 3496 * Set to `true` when a `break` is encountered, and reset to `false` when a
3497 * `do`, `while`, `for` or `switch` block is entered. 3497 * `do`, `while`, `for` or `switch` block is entered.
3498 */ 3498 */
3499 bool _enclosingBlockContainsBreak = false; 3499 bool _enclosingBlockContainsBreak = false;
3500 3500
3501 /** 3501 /**
3502 * Set to `true` when a `continue` is encountered, and reset to `false` when a
3503 * `do`, `while`, `for` or `switch` block is entered.
3504 */
3505 bool _enclosingBlockContainsContinue = false;
3506
3507 /**
3502 * Add node when a labelled `break` is encountered. 3508 * Add node when a labelled `break` is encountered.
3503 */ 3509 */
3504 Set<AstNode> _enclosingBlockBreaksLabel = new Set<AstNode>(); 3510 Set<AstNode> _enclosingBlockBreaksLabel = new Set<AstNode>();
3505 3511
3506 @override 3512 @override
3507 bool visitArgumentList(ArgumentList node) => 3513 bool visitArgumentList(ArgumentList node) =>
3508 _visitExpressions(node.arguments); 3514 _visitExpressions(node.arguments);
3509 3515
3510 @override 3516 @override
3511 bool visitAsExpression(AsExpression node) => _nodeExits(node.expression); 3517 bool visitAsExpression(AsExpression node) => _nodeExits(node.expression);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
3599 if (_nodeExits(conditionExpression)) { 3605 if (_nodeExits(conditionExpression)) {
3600 return true; 3606 return true;
3601 } 3607 }
3602 if (thenStatement == null || elseStatement == null) { 3608 if (thenStatement == null || elseStatement == null) {
3603 return false; 3609 return false;
3604 } 3610 }
3605 return thenStatement.accept(this) && elseStatement.accept(this); 3611 return thenStatement.accept(this) && elseStatement.accept(this);
3606 } 3612 }
3607 3613
3608 @override 3614 @override
3609 bool visitContinueStatement(ContinueStatement node) => false; 3615 bool visitContinueStatement(ContinueStatement node) {
3616 _enclosingBlockContainsContinue = true;
Brian Wilkerson 2016/06/28 20:47:33 This is ignoring the presence or absence of a labe
3617 return false;
3618 }
3610 3619
3611 @override 3620 @override
3612 bool visitDoStatement(DoStatement node) { 3621 bool visitDoStatement(DoStatement node) {
3613 bool outerBreakValue = _enclosingBlockContainsBreak; 3622 bool outerBreakValue = _enclosingBlockContainsBreak;
3623 bool outerContinueValue = _enclosingBlockContainsContinue;
3614 _enclosingBlockContainsBreak = false; 3624 _enclosingBlockContainsBreak = false;
3625 _enclosingBlockContainsContinue = false;
3615 try { 3626 try {
3616 if (_nodeExits(node.body) && !_enclosingBlockContainsBreak) { 3627 bool bodyExits = _nodeExits(node.body);
3628 bool containsBreakOrContinue =
3629 _enclosingBlockContainsBreak || _enclosingBlockContainsContinue;
3630 // Even if we determine that the body "exits", there might be break or
3631 // continue statements that actually mean it _doesn't_ always exit.
3632 if (bodyExits && !containsBreakOrContinue) {
3617 return true; 3633 return true;
3618 } 3634 }
3619 Expression conditionExpression = node.condition; 3635 Expression conditionExpression = node.condition;
3620 if (_nodeExits(conditionExpression)) { 3636 if (_nodeExits(conditionExpression)) {
3621 return true; 3637 return true;
3622 } 3638 }
3623 // TODO(jwren) Do we want to take all constant expressions into account? 3639 // TODO(jwren) Do we want to take all constant expressions into account?
3624 if (conditionExpression is BooleanLiteral) { 3640 if (conditionExpression is BooleanLiteral) {
3625 // If do {} while (true), and the body doesn't break, then return true. 3641 // If do {} while (true), and the body doesn't break, then return true.
3626 if (conditionExpression.value && !_enclosingBlockContainsBreak) { 3642 if (conditionExpression.value && !_enclosingBlockContainsBreak) {
3627 return true; 3643 return true;
3628 } 3644 }
3629 } 3645 }
3630 return false; 3646 return false;
3631 } finally { 3647 } finally {
3632 _enclosingBlockContainsBreak = outerBreakValue; 3648 _enclosingBlockContainsBreak = outerBreakValue;
3649 _enclosingBlockContainsContinue = outerContinueValue;
3633 } 3650 }
3634 } 3651 }
3635 3652
3636 @override 3653 @override
3637 bool visitEmptyStatement(EmptyStatement node) => false; 3654 bool visitEmptyStatement(EmptyStatement node) => false;
3638 3655
3639 @override 3656 @override
3640 bool visitExpressionStatement(ExpressionStatement node) => 3657 bool visitExpressionStatement(ExpressionStatement node) =>
3641 _nodeExits(node.expression); 3658 _nodeExits(node.expression);
3642 3659
3643 @override 3660 @override
3644 bool visitForEachStatement(ForEachStatement node) { 3661 bool visitForEachStatement(ForEachStatement node) {
3645 bool outerBreakValue = _enclosingBlockContainsBreak; 3662 bool outerBreakValue = _enclosingBlockContainsBreak;
3663 bool outerContinueValue = _enclosingBlockContainsContinue;
3646 _enclosingBlockContainsBreak = false; 3664 _enclosingBlockContainsBreak = false;
3665 _enclosingBlockContainsContinue = false;
3647 try { 3666 try {
3648 return _nodeExits(node.iterable); 3667 return _nodeExits(node.iterable);
3649 } finally { 3668 } finally {
3650 _enclosingBlockContainsBreak = outerBreakValue; 3669 _enclosingBlockContainsBreak = outerBreakValue;
3670 _enclosingBlockContainsContinue = outerContinueValue;
3651 } 3671 }
3652 } 3672 }
3653 3673
3654 @override 3674 @override
3655 bool visitForStatement(ForStatement node) { 3675 bool visitForStatement(ForStatement node) {
3656 bool outerBreakValue = _enclosingBlockContainsBreak; 3676 bool outerBreakValue = _enclosingBlockContainsBreak;
3677 bool outerContinueValue = _enclosingBlockContainsContinue;
3657 _enclosingBlockContainsBreak = false; 3678 _enclosingBlockContainsBreak = false;
3679 _enclosingBlockContainsContinue = false;
3658 try { 3680 try {
3659 if (node.variables != null && 3681 if (node.variables != null &&
3660 _visitVariableDeclarations(node.variables.variables)) { 3682 _visitVariableDeclarations(node.variables.variables)) {
3661 return true; 3683 return true;
3662 } 3684 }
3663 if (node.initialization != null && _nodeExits(node.initialization)) { 3685 if (node.initialization != null && _nodeExits(node.initialization)) {
3664 return true; 3686 return true;
3665 } 3687 }
3666 Expression conditionExpression = node.condition; 3688 Expression conditionExpression = node.condition;
3667 if (conditionExpression != null && _nodeExits(conditionExpression)) { 3689 if (conditionExpression != null && _nodeExits(conditionExpression)) {
3668 return true; 3690 return true;
3669 } 3691 }
3670 if (_visitExpressions(node.updaters)) { 3692 if (_visitExpressions(node.updaters)) {
3671 return true; 3693 return true;
3672 } 3694 }
3673 bool blockReturns = _nodeExits(node.body); 3695 bool blockReturns = _nodeExits(node.body);
3674 // TODO(jwren) Do we want to take all constant expressions into account? 3696 // TODO(jwren) Do we want to take all constant expressions into account?
3675 // If for(; true; ) (or for(;;)), and the body doesn't return or the body 3697 // If for(; true; ) (or for(;;)), and the body doesn't return or the body
3676 // doesn't have a break, then return true. 3698 // doesn't have a break, then return true.
3677 bool implicitOrExplictTrue = conditionExpression == null || 3699 bool implicitOrExplictTrue = conditionExpression == null ||
3678 (conditionExpression is BooleanLiteral && conditionExpression.value); 3700 (conditionExpression is BooleanLiteral && conditionExpression.value);
3679 if (implicitOrExplictTrue) { 3701 if (implicitOrExplictTrue) {
3680 if (blockReturns || !_enclosingBlockContainsBreak) { 3702 if (blockReturns || !_enclosingBlockContainsBreak) {
3681 return true; 3703 return true;
3682 } 3704 }
3683 } 3705 }
3684 return false; 3706 return false;
3685 } finally { 3707 } finally {
3686 _enclosingBlockContainsBreak = outerBreakValue; 3708 _enclosingBlockContainsBreak = outerBreakValue;
3709 _enclosingBlockContainsContinue = outerContinueValue;
3687 } 3710 }
3688 } 3711 }
3689 3712
3690 @override 3713 @override
3691 bool visitFunctionDeclarationStatement(FunctionDeclarationStatement node) => 3714 bool visitFunctionDeclarationStatement(FunctionDeclarationStatement node) =>
3692 false; 3715 false;
3693 3716
3694 @override 3717 @override
3695 bool visitFunctionExpression(FunctionExpression node) => false; 3718 bool visitFunctionExpression(FunctionExpression node) => false;
3696 3719
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
3902 if (variables[i].accept(this)) { 3925 if (variables[i].accept(this)) {
3903 return true; 3926 return true;
3904 } 3927 }
3905 } 3928 }
3906 return false; 3929 return false;
3907 } 3930 }
3908 3931
3909 @override 3932 @override
3910 bool visitWhileStatement(WhileStatement node) { 3933 bool visitWhileStatement(WhileStatement node) {
3911 bool outerBreakValue = _enclosingBlockContainsBreak; 3934 bool outerBreakValue = _enclosingBlockContainsBreak;
3935 bool outerContinueValue = _enclosingBlockContainsContinue;
3912 _enclosingBlockContainsBreak = false; 3936 _enclosingBlockContainsBreak = false;
3937 _enclosingBlockContainsContinue = false;
3913 try { 3938 try {
3914 Expression conditionExpression = node.condition; 3939 Expression conditionExpression = node.condition;
3915 if (conditionExpression.accept(this)) { 3940 if (conditionExpression.accept(this)) {
3916 return true; 3941 return true;
3917 } 3942 }
3918 node.body.accept(this); 3943 node.body.accept(this);
3919 // TODO(jwren) Do we want to take all constant expressions into account? 3944 // TODO(jwren) Do we want to take all constant expressions into account?
3920 if (conditionExpression is BooleanLiteral) { 3945 if (conditionExpression is BooleanLiteral) {
3921 // If while(true), and the body doesn't have a break, then return true. 3946 // If while(true), and the body doesn't have a break, then return true.
3922 // The body might be found to exit, but if there are any break 3947 // The body might be found to exit, but if there are any break
3923 // statements, then it is a faulty finding. In other words: 3948 // statements, then it is a faulty finding. In other words:
3924 // 3949 //
3925 // * If the body exits, and does not contain a break statement, then 3950 // * If the body exits, and does not contain a break statement, then
3926 // it exits. 3951 // it exits.
3927 // * If the body does not exit, and does not contain a break statement, 3952 // * If the body does not exit, and does not contain a break statement,
3928 // then it loops infinitely (also an exit). 3953 // then it loops infinitely (also an exit).
3929 // 3954 //
3930 // As both conditions forbid any break statements to be found, the logic 3955 // As both conditions forbid any break statements to be found, the logic
3931 // just boils down to checking [_enclosingBlockContainsBreak]. 3956 // just boils down to checking [_enclosingBlockContainsBreak].
3932 if (conditionExpression.value && !_enclosingBlockContainsBreak) { 3957 if (conditionExpression.value && !_enclosingBlockContainsBreak) {
3933 return true; 3958 return true;
3934 } 3959 }
3935 } 3960 }
3936 return false; 3961 return false;
3937 } finally { 3962 } finally {
3938 _enclosingBlockContainsBreak = outerBreakValue; 3963 _enclosingBlockContainsBreak = outerBreakValue;
3964 _enclosingBlockContainsContinue = outerContinueValue;
3939 } 3965 }
3940 } 3966 }
3941 3967
3942 @override 3968 @override
3943 bool visitYieldStatement(YieldStatement node) => _nodeExits(node.expression); 3969 bool visitYieldStatement(YieldStatement node) => _nodeExits(node.expression);
3944 3970
3945 /** 3971 /**
3946 * Return `true` if the given node exits. 3972 * Return `true` if the given node exits.
3947 * 3973 *
3948 * @param node the node being tested 3974 * @param node the node being tested
(...skipping 7091 matching lines...) Expand 10 before | Expand all | Expand 10 after
11040 return null; 11066 return null;
11041 } 11067 }
11042 if (identical(node.staticElement, variable)) { 11068 if (identical(node.staticElement, variable)) {
11043 if (node.inSetterContext()) { 11069 if (node.inSetterContext()) {
11044 result = true; 11070 result = true;
11045 } 11071 }
11046 } 11072 }
11047 return null; 11073 return null;
11048 } 11074 }
11049 } 11075 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/all_the_rest_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698