Ejemplo n.º 1
0
        private SwitchStatement TraverseSwitchStatement(SelectBlockSyntax sbs, ref int returnCnt, bool nested = false)
        {
            SwitchStatement switchStm = new SwitchStatement();
            switchStm.IsNested = nested;
            SelectStatementSyntax sss = sbs.SelectStatement;

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

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

            foreach (CaseBlockSyntax cbb in sbs.CaseBlocks)
            {
                CaseStatement aCase = new CaseStatement();

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

                        variable.Name = (sn as IdentifierNameSyntax).Identifier.ValueText;
                        aCase.AccessedVars.Add(variable);
                    }
                }
                foreach (StatementSyntax ss in cbb.Statements)
                {

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

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

            return switchStm;
        }
Ejemplo n.º 2
0
        private Decisions TraverseSwitchStatements(SwitchStatementSyntax sss, ref int exitPoints, bool nested = false)
        {
            Decisions retDecision = new Decisions();
            SwitchStatement retSwitch = new SwitchStatement();

            if (sss.HasLeadingTrivia)
            {
                SetOuterComments(retSwitch, sss.GetLeadingTrivia().ToFullString());
            }

            if (sss.HasTrailingTrivia)
            {
                SetInnerComments(retSwitch, sss.GetTrailingTrivia().ToFullString());
            }

            ExpressionSyntax es = sss.Expression;
            var binaryExprs = from aBinaryExpr in sss.ChildNodes().OfType<BinaryExpressionSyntax>() select aBinaryExpr;
            foreach (BinaryExpressionSyntax bes in binaryExprs)
            {
                Method tempMethod = TraverseBinaryExpression(bes);
                retSwitch.AccessedVars.AddRange(tempMethod.AccessedVariables);
                retSwitch.InvokedMethods.AddRange(tempMethod.InvokedMethods);
            }
            var catches = from aCatch in sss.ChildNodes().OfType<CatchClauseSyntax>() select aCatch;
            foreach (CatchClauseSyntax ccs in catches)
            {
                Decisions tempCatch = TraverseCatchClauses(ccs, ref exitPoints, true);
                retSwitch.Nested.AddRange(tempCatch.Catches);
            }
            var statements = from aStatement in sss.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);
                    retSwitch.Nested.AddRange(dwl.DoWhileLoops);
                    //dwl.Nested.Add(TraverseDoStatements(ss as DoStatementSyntax));
                    //retSwitch.Nested.Add(dwl);
                }
                //else if (ss is EmptyStatementSyntax)
                //{
                //}
                else if (ss is ExpressionStatementSyntax)
                {
                    var vars = from aVar in ss.ChildNodes().OfType<BinaryExpressionSyntax>() select aVar;
                    foreach (BinaryExpressionSyntax bes in vars)
                    {
                        Method tempMethod = TraverseBinaryExpression(bes);
                        retSwitch.AccessedVars.AddRange(tempMethod.AccessedVariables);
                        retSwitch.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                    }
                    var invokedMethods = from aInvokedMethod in ss.ChildNodes().OfType<InvocationExpressionSyntax>() select aInvokedMethod;
                    foreach (InvocationExpressionSyntax ies in invokedMethods)
                    {
                        Method tempMethod = TraverseInvocationExpression(ies);
                        retSwitch.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                        retSwitch.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    }
                }
                //else if (ss is FixedStatementSyntax)
                //{
                //}
                else if (ss is ForEachStatementSyntax)
                {
                    Decisions fes = TraverseForEachStatements(ss as ForEachStatementSyntax, ref exitPoints, true);
                    retSwitch.Nested.AddRange(fes.ForEachStatements);
                    //fes.IsNested = true;
                    //retSwitch.Nested.Add(fes);
                }
                else if (ss is ForStatementSyntax)
                {
                    Decisions fs = TraverseForStatements(ss as ForStatementSyntax, ref exitPoints, true);
                    retSwitch.Nested.AddRange(fs.ForStatements);
                    //fs.IsNested = true;
                    //retSwitch.Nested.Add(fs);
                }
                //else if (ss is GotoStatementSyntax)
                //{
                //}
                else if (ss is IfStatementSyntax)
                {
                    Decisions ifstm = TraverseIfStatements(ss as IfStatementSyntax, ref exitPoints, true);
                    retSwitch.Nested.AddRange(ifstm.IfStatements);
                    //ifstm.IsNested = true;
                    //retSwitch.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);
                    retSwitch.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    retSwitch.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);
                    retSwitch.Nested.AddRange(switchStm.SwitchStatements);
                    //switchStm.IsNested = true;
                    //retSwitch.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);
                    retSwitch.Nested.AddRange(wl.WhileLoops);
                    //wl.IsNested = true;
                    //retSwitch.Nested.Add(wl);
                }
                //else if (ss is YieldStatementSyntax)
                //{
                //}
            }
            #endregion
            retDecision.SwitchStatements.Add(retSwitch);
            return retDecision;
        }