Пример #1
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;
        }
Пример #2
0
        private Decisions TraverseForStatements(ForStatementSyntax fss, ref int exitPoints, bool nested = false)
        {
            Decisions retDecision = new Decisions();
            ForStatement retFor = new ForStatement();

            if (fss.HasLeadingTrivia)
            {
                SetOuterComments(retFor, fss.GetLeadingTrivia().ToFullString());
            }

            if (fss.HasTrailingTrivia)
            {
                SetInnerComments(retFor, fss.GetTrailingTrivia().ToFullString());
            }

            ExpressionSyntax es = fss.Condition;
            if (es != null)
            {
                var binaryExpressions = from aBinaryExpression in es.DescendantNodesAndSelf().OfType<BinaryExpressionSyntax>() select aBinaryExpression;
                retFor.ConditionCount = binaryExpressions.Count();
                foreach (BinaryExpressionSyntax bes in binaryExpressions)
                {
                    Method tempMethod = TraverseBinaryExpression(bes);
                    retFor.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    retFor.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
            }
            var catches = from aCatch in fss.ChildNodes().OfType<CatchClauseSyntax>() select aCatch;
            foreach (CatchClauseSyntax ccs in catches)
            {
                Decisions tempCatch = TraverseCatchClauses(ccs, ref exitPoints, true);
                retFor.Nested.AddRange(tempCatch.Catches);
            }
            var statements = from aStatement in fss.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);
                    retFor.Nested.AddRange(dwl.DoWhileLoops);
                    //dwl.IsNested = true;
                    //retFor.Nested.Add(dwl);
                }
                //else if (ss is EmptyStatementSyntax)
                //{
                //}
                else if (ss is ExpressionStatementSyntax)
                {
                    Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax);
                    retFor.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    retFor.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                //else if (ss is FixedStatementSyntax)
                //{
                //}
                else if (ss is ForEachStatementSyntax)
                {
                    Decisions fes = TraverseForEachStatements(ss as ForEachStatementSyntax, ref exitPoints, true);
                    retFor.Nested.AddRange(fes.ForEachStatements);
                    //fes.IsNested = true;
                    //retFor.Nested.Add(fes);
                }
                else if (ss is ForStatementSyntax)
                {
                    Decisions fs = TraverseForStatements(ss as ForStatementSyntax, ref exitPoints, true);
                    retFor.Nested.AddRange(fs.ForStatements);
                    //fs.IsNested = true;
                    //retFor.Nested.Add(fs);
                }
                //else if (ss is GotoStatementSyntax)
                //{
                //}
                else if (ss is IfStatementSyntax)
                {
                    Decisions ifstm = TraverseIfStatements(ss as IfStatementSyntax, ref exitPoints, true);
                    retFor.Nested.AddRange(ifstm.IfStatements);
                    //ifstm.IsNested = true;
                    //retFor.Nested.Add(ifstm);
                }
                //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);
                    retFor.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    retFor.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);
                    retFor.Nested.AddRange(switchStm.SwitchStatements);
                    //switchStm.IsNested = true;
                    //retFor.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);
                    retFor.Nested.AddRange(wl.WhileLoops);
                    //wl.IsNested = true;
                    //retFor.Nested.Add(wl);
                }
                //else if (ss is YieldStatementSyntax)
                //{
                //}
            }
            #endregion
            retDecision.ForStatements.Add(retFor);
            return retDecision;
        }