Ejemplo n.º 1
0
        private static Task <Document> ConvertWhileStatementToForStatementAsync(
            Document document,
            WhileStatementSyntax whileStatement,
            CancellationToken cancellationToken)
        {
            StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(whileStatement);

            SyntaxList <StatementSyntax> statements = statementsInfo.Statements;

            int index = statements.IndexOf(whileStatement);

            SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo((LocalDeclarationStatementSyntax)statements[index - 1]);

            var block = (BlockSyntax)whileStatement.Statement;

            var expressionStatement = (ExpressionStatementSyntax)block.Statements.Last();

            var postIncrementExpression = (PostfixUnaryExpressionSyntax)expressionStatement.Expression;

            BlockSyntax newBlock = block.WithStatements(block.Statements.Remove(expressionStatement));

            ForStatementSyntax forStatement = CSharpFactory.ForStatement(
                declaration: localInfo.Declaration.TrimTrivia(),
                condition: whileStatement.Condition,
                incrementor: postIncrementExpression.TrimTrivia(),
                statement: newBlock);

            forStatement = forStatement
                           .WithLeadingTrivia(localInfo.Statement.GetLeadingTrivia().AddRange(whileStatement.GetLeadingTrivia().EmptyIfWhitespace()))
                           .WithFormatterAnnotation();

            SyntaxList <StatementSyntax> newStatements = statements.ReplaceRange(index - 1, 2, new StatementSyntax[] { forStatement });

            return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken));
        }
Ejemplo n.º 2
0
        private Decisions TraverseWhileLoops(WhileStatementSyntax wss, ref int exitPoints, bool nested = false)
        {
            //TODO
            //get accessed vars, invoked methods, nested
            Decisions retDecision = new Decisions();
            WhileLoop whileStm = new WhileLoop();

            if (wss.HasLeadingTrivia)
            {
                SetOuterComments(whileStm, wss.GetLeadingTrivia().ToFullString());
            }

            if (wss.HasTrailingTrivia)
            {
                SetInnerComments(whileStm, wss.GetTrailingTrivia().ToFullString());
            }

            ExpressionSyntax es = wss.Condition;
            //var binaryExpressions = from aBinaryExpression in wss.Condition.DescendantNodesAndSelf().OfType<BinaryExpressionSyntax>() select aBinaryExpression;
            var binaryExpressions = from abinaryExpression in wss.ChildNodes().OfType<BinaryExpressionSyntax>() select abinaryExpression;
            whileStm.ConditionCount = binaryExpressions.Count();
            if (wss.Condition is BinaryExpressionSyntax)
            {
                //this next line doesn't ever fire
                //binaryExpressions = from aBinaryExpression in wss.Condition.DescendantNodes().OfType<BinaryExpressionSyntax>() select aBinaryExpression;
                foreach (BinaryExpressionSyntax bes in binaryExpressions)
                {
                    Method tempMethod = TraverseBinaryExpression(bes);
                    whileStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    whileStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
            }
            var catches = from aCatch in wss.ChildNodes().OfType<CatchClauseSyntax>() select aCatch;
            foreach (CatchClauseSyntax ccs in catches)
            {
                Decisions tempCatch = TraverseCatchClauses(ccs, ref exitPoints, true);
                whileStm.Nested.AddRange(tempCatch.Catches);
            }
            var statements = from aStatement in wss.Statement.ChildNodes().OfType<StatementSyntax>() select aStatement;
            List<BaseDecisions> retBd = new List<BaseDecisions>();
            List<InvokedMethod> retIm = new List<InvokedMethod>();
            #region Nested and Invoked Methods and accessed vars
            foreach (StatementSyntax ss in statements)
            {

                //if (ss is BreakStatementSyntax)
                //{
                //}
                //else if (ss is CheckedStatementSyntax)
                //{
                //}
                //else if (ss is ContinueStatementSyntax)
                //{
                //}
                if (ss is DoStatementSyntax)
                {
                    Decisions dwl = TraverseDoStatements(ss as DoStatementSyntax, ref exitPoints, true);
                    whileStm.Nested.AddRange(dwl.DoWhileLoops);
                    //dwl.IsNested = true;
                    //whileStm.Nested.Add(dwl);
                }
                //else if (ss is EmptyStatementSyntax)
                //{
                //}
                else if (ss is ExpressionStatementSyntax)
                {
                    Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax);
                    whileStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    whileStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                //else if (ss is FixedStatementSyntax)
                //{
                //}
                else if (ss is ForEachStatementSyntax)
                {
                    Decisions fes = TraverseForEachStatements(ss as ForEachStatementSyntax, ref exitPoints, true);
                    whileStm.Nested.AddRange(fes.ForEachStatements);
                    //fes.IsNested = true;
                    //whileStm.Nested.Add(fes);
                }
                else if (ss is ForStatementSyntax)
                {
                    Decisions fs = TraverseForStatements(ss as ForStatementSyntax, ref exitPoints, true);
                    whileStm.Nested.AddRange(fs.WhileLoops);
                    //fs.IsNested = true;
                    //whileStm.Nested.Add(fs);
                }
                //else if (ss is GotoStatementSyntax)
                //{
                //}
                else if (ss is IfStatementSyntax)
                {
                    Decisions decision = TraverseIfStatements(ss as IfStatementSyntax, ref exitPoints, true);
                    whileStm.Nested.AddRange(decision.IfStatements);
                }
                //else if (ss is LabeledStatementSyntax)
                //{
                //    BaseDecisions bd = new BaseDecisions();
                //    bd.IsNested = true;
                //    bd.Nested.Add(TraverseLabelStatements(ss as LabeledStatementSyntax));
                //    retIf.Nested.Add(bd);
                //}
                else if (ss is LocalDeclarationStatementSyntax)
                {
                    Method tempMethod = TransverseAccessVars(ss as LocalDeclarationStatementSyntax);
                    whileStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    whileStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                //else if (ss is LockStatementSyntax)
                //{
                //}
                //else if (ss is ReturnStatementSyntax)
                //{
                //}
                else if (ss is SwitchStatementSyntax)
                {
                    Decisions switchStm = TraverseSwitchStatements(ss as SwitchStatementSyntax, ref exitPoints, true);
                    whileStm.Nested.AddRange(switchStm.SwitchStatements);
                    //switchStm.IsNested = true;
                    //whileStm.Nested.Add(switchStm);
                }
                //else if (ss is ThrowStatementSyntax)
                //{
                //}
                //else if (ss is TryStatementSyntax)
                //{
                //}
                //else if (ss is UnsafeStatementSyntax)
                //{
                //}
                //else if (ss is UsingStatementSyntax)
                //{
                //    //using list?
                //}
                else if (ss is WhileStatementSyntax)
                {
                    Decisions wl = TraverseWhileLoops(ss as WhileStatementSyntax, ref exitPoints, true);
                    whileStm.Nested.AddRange(wl.WhileLoops);
                    //wl.IsNested = true;
                    //whileStm.Nested.Add(wl);
                }
                //else if (ss is YieldStatementSyntax)
                //{
                //}
            }
            #endregion

            retDecision.WhileLoops.Add(whileStm);

            return retDecision;
        }