private Decisions AllDecisions(Decisions decisions) { Decisions retDecisions = new Decisions(); foreach (IfStatement bd in decisions.IfStatements) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } foreach (ElseStatement bd in decisions.ElseStatements) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } foreach (ForStatement bd in decisions.ForStatements) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } foreach (ForEachStatement bd in decisions.ForEachStatements) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } foreach (WhileLoop bd in decisions.WhileLoops) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } foreach (DoWhileLoop bd in decisions.DoWhileLoops) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } foreach (SwitchStatement bd in decisions.SwitchStatements) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } foreach (CatchStatements bd in decisions.Catches) { retDecisions.Add(bd); RecursiveDecisions(bd.Nested, ref retDecisions); } return retDecisions; }
private void RecursiveDecisions(List<BaseDecisions> nested, ref Decisions decisions) { foreach (BaseDecisions bd in nested) { decisions.Add(bd); RecursiveDecisions(bd.Nested, ref decisions); } }
//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; }