private VariableState VisitSingleLineIfStatement(SingleLineIfStatementSyntax singleLineIfStatementSyntax, ExecutionState state)
        {
            var condition = VisitExpression(singleLineIfStatementSyntax.Condition, state);

            var ifState   = new ExecutionState(state);
            var lastState = new VariableState(singleLineIfStatementSyntax, VariableTaint.Unset);

            lastState = VisitStatements(singleLineIfStatementSyntax.Statements, ifState, lastState);
            condition.MergeTaint(lastState.Taint);
            state.Merge(ifState);
            return(condition);
        }
        SyntaxNode ExtendIfConditionWithNullCheck(SyntaxNode statement, ExpressionSyntax identifier)
        {
            MultiLineIfBlockSyntax multiLineIfStatement = statement as MultiLineIfBlockSyntax;

            if (multiLineIfStatement != null)
            {
                return(multiLineIfStatement.WithIfStatement(
                           multiLineIfStatement.IfStatement.WithCondition(
                               SyntaxFactory.BinaryExpression(SyntaxKind.AndAlsoExpression,
                                                              ParenthizeIfNeeded(CreateIsNotNothingBinaryExpression(identifier)),
                                                              SyntaxFactory.Token(SyntaxKind.AndAlsoKeyword),
                                                              ParenthizeIfNeeded(multiLineIfStatement.IfStatement.Condition)
                                                              ).WithAdditionalAnnotations(Formatter.Annotation)
                               )));
            }
            SingleLineIfStatementSyntax singleLineIfStatement = statement as SingleLineIfStatementSyntax;

            if (singleLineIfStatement != null)
            {
                return(singleLineIfStatement.WithCondition(
                           SyntaxFactory.BinaryExpression(SyntaxKind.AndAlsoExpression,
                                                          ParenthizeIfNeeded(CreateIsNotNothingBinaryExpression(identifier)),
                                                          SyntaxFactory.Token(SyntaxKind.AndAlsoKeyword),
                                                          ParenthizeIfNeeded(singleLineIfStatement.Condition)
                                                          ).WithAdditionalAnnotations(Formatter.Annotation)
                           ));
            }
            ElseIfBlockSyntax elseIfBlock = statement as ElseIfBlockSyntax;

            if (elseIfBlock != null)
            {
                return(elseIfBlock.WithElseIfStatement(
                           elseIfBlock.ElseIfStatement.WithCondition(
                               SyntaxFactory.BinaryExpression(SyntaxKind.AndAlsoExpression,
                                                              ParenthizeIfNeeded(CreateIsNotNothingBinaryExpression(identifier)),
                                                              SyntaxFactory.Token(SyntaxKind.AndAlsoKeyword),
                                                              ParenthizeIfNeeded(elseIfBlock.ElseIfStatement.Condition)
                                                              ).WithAdditionalAnnotations(Formatter.Annotation)
                               )));
            }

            return(null);
        }
예제 #3
0
 public override void VisitSingleLineIfStatement(SingleLineIfStatementSyntax node)
 {
     LogicalLineCount++;
     base.VisitSingleLineIfStatement(node);
 }
예제 #4
0
 public override void VisitSingleLineIfStatement(SingleLineIfStatementSyntax node)
 {
     State.IncreaseComplexityByNestingPlusOne(node.IfKeyword);
     State.VisitWithNesting(node, base.VisitSingleLineIfStatement);
 }
예제 #5
0
        //TODO: Make sure we recursively go through each decision
        private Decisions TraverseIfStatement(SingleLineIfStatementSyntax sliss, ref int returnCnt, bool nested = false)
        {
            Decisions retDecision = new Decisions();
            IfStatement ifStm = new IfStatement();
            ifStm.IsNested = nested;
            //!!!!!!!!!!!!!!!!!!!!Start Here!!!!!!!!!!!!!!!!!!!
            SingleLineIfPartSyntax ifPart = sliss.IfPart;

            IfStatementSyntax iss = ifPart.Begin;

            BinaryExpressionSyntax bes = iss.Condition as BinaryExpressionSyntax;

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

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

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

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

            retDecision.IfStatements.Add(ifStm);

            if (sliss.ElsePart != null)
            {
                SingleLineElsePartSyntax sleps = sliss.ElsePart;
                ElseStatement elseStm = new ElseStatement();

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

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

            return retDecision;
        }