コード例 #1
0
 private IEnumerable <SyntaxNode> VisitForBlock(ForBlockSyntax node)
 {
     if (!node.Statements.Any() && NoCommentsBefore(node.NextStatement))
     {
         yield return(node.ForStatement);
     }
 }
コード例 #2
0
 public override void VisitForBlock(ForBlockSyntax node) =>
 counter.CheckNesting(node.ForStatement.ForKeyword, () => base.VisitForBlock(node));
コード例 #3
0
        private bool ConvertForToSimpleForNextWithoutStatements(CSS.ForStatementSyntax node, out ForBlockSyntax blockWithoutStatements)
        {
            //   ForStatement -> ForNextStatement when for-loop is simple

            // only the following forms of the for-statement are allowed:
            // for (TypeReference name = start; name < oneAfterEnd; name += step)
            // for (name = start; name < oneAfterEnd; name += step)
            // for (TypeReference name = start; name <= end; name += step)
            // for (name = start; name <= end; name += step)
            // for (TypeReference name = start; name > oneAfterEnd; name -= step)
            // for (name = start; name > oneAfterEnd; name -= step)
            // for (TypeReference name = start; name >= end; name -= step)
            // for (name = start; name >= end; name -= step)

            blockWithoutStatements = null;

            // check if the form is valid and collect TypeReference, name, start, end and step
            bool hasVariable = node.Declaration != null && node.Declaration.Variables.Count == 1;

            if (!hasVariable && node.Initializers.Count != 1)
            {
                return(false);
            }
            if (node.Incrementors.Count != 1)
            {
                return(false);
            }
            var iterator = node.Incrementors.FirstOrDefault()?.Accept(_nodesVisitor) as AssignmentStatementSyntax;

            if (iterator == null || !iterator.IsKind(SyntaxKind.AddAssignmentStatement, SyntaxKind.SubtractAssignmentStatement))
            {
                return(false);
            }
            var iteratorIdentifier = iterator.Left as IdentifierNameSyntax;

            if (iteratorIdentifier == null)
            {
                return(false);
            }
            var stepExpression = iterator.Right as LiteralExpressionSyntax;

            if (stepExpression == null || !(stepExpression.Token.Value is int))
            {
                return(false);
            }
            int step = (int)stepExpression.Token.Value;

            if (SyntaxTokenExtensions.IsKind(iterator.OperatorToken, SyntaxKind.MinusEqualsToken))
            {
                step = -step;
            }

            var condition = node.Condition as CSS.BinaryExpressionSyntax;

            if (condition == null || !(condition.Left is CSS.IdentifierNameSyntax))
            {
                return(false);
            }
            if (((CSS.IdentifierNameSyntax)condition.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier))
            {
                return(false);
            }

            ExpressionSyntax end;

            if (iterator.IsKind(SyntaxKind.SubtractAssignmentStatement))
            {
                if (condition.IsKind(CS.SyntaxKind.GreaterThanOrEqualExpression))
                {
                    end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor);
                }
                else if (condition.IsKind(CS.SyntaxKind.GreaterThanExpression))
                {
                    end = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.PlusToken), _commonConversions.Literal(1));
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (condition.IsKind(CS.SyntaxKind.LessThanOrEqualExpression))
                {
                    end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor);
                }
                else if (condition.IsKind(CS.SyntaxKind.LessThanExpression))
                {
                    end = SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.MinusToken), _commonConversions.Literal(1));
                }
                else
                {
                    return(false);
                }
            }

            VisualBasicSyntaxNode variable;
            ExpressionSyntax      start;

            if (hasVariable)
            {
                var v = node.Declaration.Variables[0];
                start = (ExpressionSyntax)v.Initializer?.Value.Accept(_nodesVisitor);
                if (start == null)
                {
                    return(false);
                }
                variable = SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(_commonConversions.ConvertIdentifier(v.Identifier))),
                    node.Declaration.Type.IsVar ? null : SyntaxFactory.SimpleAsClause((TypeSyntax)node.Declaration.Type.Accept(_nodesVisitor)),
                    null
                    );
            }
            else
            {
                var initializer = node.Initializers.FirstOrDefault() as CSS.AssignmentExpressionSyntax;
                if (initializer == null || !initializer.IsKind(CS.SyntaxKind.SimpleAssignmentExpression))
                {
                    return(false);
                }
                if (!(initializer.Left is CSS.IdentifierNameSyntax))
                {
                    return(false);
                }
                if (((CSS.IdentifierNameSyntax)initializer.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier))
                {
                    return(false);
                }
                variable = initializer.Left.Accept(_nodesVisitor);
                start    = (ExpressionSyntax)initializer.Right.Accept(_nodesVisitor);
            }

            blockWithoutStatements = SyntaxFactory.ForBlock(
                SyntaxFactory.ForStatement(variable, start, end, step == 1 ? null : SyntaxFactory.ForStepClause(_commonConversions.Literal(step))),
                SyntaxFactory.List <StatementSyntax>(),
                SyntaxFactory.NextStatement()
                );
            return(true);
        }
コード例 #4
0
 public override void VisitForBlock(ForBlockSyntax node)
 {
     State.IncreaseComplexityByNestingPlusOne(node.ForStatement.ForKeyword);
     State.VisitWithNesting(node, base.VisitForBlock);
 }
コード例 #5
0
 public override void VisitForBlock(ForBlockSyntax node)
 {
     LogicalLineCount++;
     base.VisitForBlock(node);
 }
コード例 #6
0
        private bool ConvertForToSimpleForNextWithoutStatements(CSS.ForStatementSyntax node, out ForBlockSyntax blockWithoutStatements)
        {
            //   ForStatement -> ForNextStatement when for-loop is simple

            // only the following forms of the for-statement are allowed:
            // for (TypeReference name = start; name < oneAfterEnd; name += step)
            // for (name = start; name < oneAfterEnd; name += step)
            // for (TypeReference name = start; name <= end; name += step)
            // for (name = start; name <= end; name += step)
            // for (TypeReference name = start; name > oneAfterEnd; name -= step)
            // for (name = start; name > oneAfterEnd; name -= step)
            // for (TypeReference name = start; name >= end; name -= step)
            // for (name = start; name >= end; name -= step)

            blockWithoutStatements = null;

            // check if the form is valid and collect TypeReference, name, start, end and step
            bool hasVariable = node.Declaration != null && node.Declaration.Variables.Count == 1;

            if (!hasVariable && node.Initializers.Count != 1)
            {
                return(false);
            }
            if (node.Incrementors.Count != 1)
            {
                return(false);
            }
            var iterator = node.Incrementors.FirstOrDefault()?.Accept(_nodesVisitor) as AssignmentStatementSyntax;

            if (iterator == null || !iterator.IsKind(SyntaxKind.AddAssignmentStatement, SyntaxKind.SubtractAssignmentStatement))
            {
                return(false);
            }
            var iteratorIdentifier = iterator.Left as IdentifierNameSyntax;

            if (iteratorIdentifier == null)
            {
                return(false);
            }
            var stepExpression = iterator.Right as LiteralExpressionSyntax;

            if (stepExpression == null || !(stepExpression.Token.Value is int))
            {
                return(false);
            }
            int step = (int)stepExpression.Token.Value;

            if (SyntaxTokenExtensions.IsKind(iterator.OperatorToken, SyntaxKind.MinusEqualsToken))
            {
                step = -step;
            }

            var condition = node.Condition as CSS.BinaryExpressionSyntax;

            if (condition == null || !(condition.Left is CSS.IdentifierNameSyntax))
            {
                return(false);
            }
            if (((CSS.IdentifierNameSyntax)condition.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier))
            {
                return(false);
            }

            ExpressionSyntax end;
            var rightExpression = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor);
            var rightVal        = (rightExpression is LiteralExpressionSyntax && _semanticModel.GetConstantValue(condition.Right) is { Value : int r }) ? r : default(int?);
コード例 #7
0
        private Decisions TraverseForStatement(ForBlockSyntax fbs, ref int returnCnt, bool nested = false)
        {
            Decisions retDecision = new Decisions();
            ForStatement forStm = new ForStatement();
            forStm.IsNested = nested;
            if (fbs.Begin is ForEachStatementSyntax)
            {
                retDecision.ForEachStatements.Add(TraverseForeachStatement(fbs.Begin as ForEachStatementSyntax, ref returnCnt));
            }
            else
            {
                ForStatementSyntax fss = fbs.Begin as ForStatementSyntax;

                forStm.ConditionCount = 1;
                foreach (SyntaxNode sn in fss.DescendantNodesAndSelf())
                {
                    if (sn is BinaryExpressionSyntax)
                    {
                        forStm.ConditionCount++;
                    }
                    else if (sn is IdentifierNameSyntax)
                    {
                        Variables variable = new Variables();
                        variable.IsReferenced = true;

                        variable.Name = (sn as IdentifierNameSyntax).Identifier.ValueText;
                        forStm.AccessedVars.Add(variable);
                    }
                }

                foreach (StatementSyntax ss in fbs.Statements)
                {
                    if (ss is AssignmentStatementSyntax)
                    {
                        //TODO: need to look at more than just then name!
                        Method tempMethod = TraverseAccessVars(ss as AssignmentStatementSyntax);

                        forStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                        forStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                    }
                    else if (ss is LocalDeclarationStatementSyntax)
                    {
                        Method tempMethod = TraverseVarDecls(ss as LocalDeclarationStatementSyntax);
                        forStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                        forStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                    }
                    else if (ss is SingleLineIfStatementSyntax)
                    {
                        Decisions decision = TraverseIfStatement(ss as SingleLineIfStatementSyntax, ref returnCnt, true);
                        forStm.Nested.AddRange(decision.IfStatements);
                        forStm.Nested.AddRange(decision.ElseStatements);
                    }
                    else if (ss is MultiLineIfBlockSyntax)
                    {
                        Decisions decisions = TraverseMultiIfStatement(ss as MultiLineIfBlockSyntax, ref returnCnt, true);
                        forStm.Nested.AddRange(decisions.IfStatements);
                        forStm.Nested.AddRange(decisions.ElseStatements);
                    }
                    else if (ss is ElseStatementSyntax)
                    {
                        forStm.Nested.Add(TravsereElseStatement(ss as ElseStatementSyntax, ref returnCnt, true));
                    }
                    else if (ss is ForBlockSyntax)
                    {
                        Decisions tempDecision = TraverseForStatement(ss as ForBlockSyntax, ref returnCnt, true);
                        forStm.Nested.AddRange(tempDecision.IfStatements);
                        forStm.Nested.AddRange(tempDecision.ElseStatements);
                        forStm.Nested.AddRange(tempDecision.ForEachStatements);
                        forStm.Nested.AddRange(tempDecision.ForStatements);
                        forStm.Nested.AddRange(tempDecision.WhileLoops);
                        forStm.Nested.AddRange(tempDecision.DoWhileLoops);
                        forStm.Nested.AddRange(tempDecision.Catches);
                        forStm.Nested.AddRange(tempDecision.SwitchStatements);
                    }
                    else if (ss is SelectBlockSyntax)
                    {
                        forStm.Nested.Add(TraverseSwitchStatement(ss as SelectBlockSyntax, ref returnCnt, true));
                    }
                    else if (ss is DoLoopBlockSyntax)
                    {
                        forStm.Nested.Add(TraverseDoWhileLoop(ss as DoLoopBlockSyntax, ref returnCnt, true));
                    }
                    else if (ss is WhileBlockSyntax)
                    {
                        forStm.Nested.Add(TraverseWhileLoop(ss as WhileBlockSyntax, ref returnCnt, true));
                    }
                    else if (ss is CallStatementSyntax)
                    {
                        forStm.InvokedMethods.Add(TraverseInvokedMethod(ss as CallStatementSyntax));
                    }
                    else if (ss is ReturnStatementSyntax)
                    {
                        Method tempMethod = TraverseReturnStatement(ss as ReturnStatementSyntax);
                        forStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                        forStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                        returnCnt++;
                    }
                }
            }
            retDecision.ForStatements.Add(forStm);
            return retDecision;
        }