public override void Exit() { IfStatementSyntax chooseStatement; ElseClauseSyntax elseClause = _otherwiseClause; if (_whenClauses.Count > 0) { for (int i = _whenClauses.Count - 1; i > 0; i--) { var whenClause = _whenClauses[i]; if (elseClause != null) { whenClause = whenClause.WithElse(elseClause); } elseClause = SF.ElseClause(whenClause); } chooseStatement = _whenClauses[0]; if (elseClause != null) { chooseStatement = chooseStatement.WithElse(elseClause); } } else { chooseStatement = SF.IfStatement(SF.LiteralExpression(SyntaxKind.FalseLiteralExpression), SF.Block()); if (_otherwiseClause != null) { chooseStatement = chooseStatement.WithElse(_otherwiseClause); } } _exitHandler(chooseStatement); }
private IfStatementSyntax SeparateIfConditions(IfStatementSyntax node) { if (node.Condition.IsKind(SK.InvocationExpression)) { return(node); } else { var conditionChilds = node.Condition.ChildNodes().ToArray(); if (conditionChilds.Length == 2) { IfStatementSyntax aStatement, bStatement; ExpressionSyntax aCondition, bCondition = null; BlockSyntax elseBlock = null; ElseClauseSyntax elseClause = null; aCondition = (ExpressionSyntax)conditionChilds[0]; bCondition = (ExpressionSyntax)conditionChilds[1]; var ifBlock = GetStatementAsBlock(node.ChildNodes().OfType <StatementSyntax>().First()); if (node.Else != null) { elseBlock = GetStatementAsBlock(node.Else.ChildNodes().OfType <StatementSyntax>().First()); elseClause = elseBlock != null?SF.ElseClause(elseBlock) : null; } switch (node.Condition.Kind()) { case SK.LogicalAndExpression: bStatement = SF.IfStatement(bCondition, ifBlock, elseClause); aStatement = SF.IfStatement(aCondition, SF.Block(bStatement), elseClause); node = aStatement; break; case SK.LogicalOrExpression: bStatement = SF.IfStatement(bCondition, ifBlock, elseClause); aStatement = SF.IfStatement(aCondition, ifBlock, SF.ElseClause(SF.Block(bStatement))); node = aStatement; break; default: return(SF.IfStatement(node.Condition, ifBlock, elseClause)); } } if (node.Condition.ChildNodes().ToArray().Length == 2) { return(SeparateIfConditions(node)); } return(node); } }
public override SyntaxList <StatementSyntax> VisitSingleLineIfStatement(VBSyntax.SingleLineIfStatementSyntax node) { var condition = (ExpressionSyntax)node.Condition.Accept(_nodesVisitor); var block = SyntaxFactory.Block(node.Statements.SelectMany(s => s.Accept(CommentConvertingVisitor))); ElseClauseSyntax elseClause = null; if (node.ElseClause != null) { var elseBlock = SyntaxFactory.Block(node.ElseClause.Statements.SelectMany(s => s.Accept(CommentConvertingVisitor))); elseClause = SyntaxFactory.ElseClause(elseBlock.UnpackNonNestedBlock()); } return(SingleStatement(SyntaxFactory.IfStatement(condition, block.UnpackNonNestedBlock(), elseClause))); }
public override SyntaxList <StatementSyntax> VisitMultiLineIfBlock(VBSyntax.MultiLineIfBlockSyntax node) { var condition = (ExpressionSyntax)node.IfStatement.Condition.Accept(_nodesVisitor); var block = SyntaxFactory.Block(node.Statements.SelectMany(s => s.Accept(CommentConvertingVisitor))); ElseClauseSyntax elseClause = null; if (node.ElseBlock != null) { var elseBlock = SyntaxFactory.Block(node.ElseBlock.Statements.SelectMany(s => s.Accept(CommentConvertingVisitor))); elseClause = SyntaxFactory.ElseClause(elseBlock.UnpackPossiblyNestedBlock());// so that you get a neat "else if" at the end } foreach (var elseIf in node.ElseIfBlocks.Reverse()) { var elseBlock = SyntaxFactory.Block(elseIf.Statements.SelectMany(s => s.Accept(CommentConvertingVisitor))); var ifStmt = SyntaxFactory.IfStatement((ExpressionSyntax)elseIf.ElseIfStatement.Condition.Accept(_nodesVisitor), elseBlock.UnpackNonNestedBlock(), elseClause); elseClause = SyntaxFactory.ElseClause(ifStmt); } return(SingleStatement(SyntaxFactory.IfStatement(condition, block.UnpackNonNestedBlock(), elseClause))); }
public override SyntaxList <StatementSyntax> VisitMultiLineIfBlock(VBSyntax.MultiLineIfBlockSyntax node) { var condition = (ExpressionSyntax)node.IfStatement.Condition.Accept(nodesVisitor); var block = SyntaxFactory.Block(node.Statements.SelectMany(s => s.Accept(this))); ElseClauseSyntax elseClause = null; if (node.ElseBlock != null) { var elseBlock = SyntaxFactory.Block(node.ElseBlock.Statements.SelectMany(s => s.Accept(this))); elseClause = SyntaxFactory.ElseClause(elseBlock.UnpackBlock()); } foreach (var elseIf in node.ElseIfBlocks.Reverse()) { var elseBlock = SyntaxFactory.Block(elseIf.Statements.SelectMany(s => s.Accept(this))); var ifStmt = SyntaxFactory.IfStatement((ExpressionSyntax)elseIf.ElseIfStatement.Condition.Accept(nodesVisitor), elseBlock.UnpackBlock(), elseClause); elseClause = SyntaxFactory.ElseClause(ifStmt); } return(SingleStatement(SyntaxFactory.IfStatement(condition, block.UnpackBlock(), elseClause))); }
public override ICodeContext CreateContext(CodeContextTypes contextType, string name = null, Bag <string> attributes = null) { switch (contextType) { case CodeContextTypes.Argument: return(new ExpressionContext((expression) => { if (expression == null) { _valueExpression = SF.LiteralExpression(SyntaxKind.TrueKeyword); } else { _valueExpression = expression.ToExpression(); } })); case CodeContextTypes.When: return(new WhenContext(name, _valueExpression, (whenStatement) => { _whenClauses.Add(whenStatement); })); case CodeContextTypes.Otherwise: string elseHookName = name; return(new BlockContext((block) => { if (elseHookName == null) { _otherwiseClause = SF.ElseClause(block); } else { _otherwiseClause = SF.ElseClause(BlockContext.MakeWriteStatement(MethodCallContext.CreateMethodCall(elseHookName, true))); } })); } return(base.CreateContext(contextType, name, attributes)); }
public override ICodeContext CreateContext(CodeContextTypes contextType, string name = null, Bag <string> attributes = null) { switch (contextType) { case CodeContextTypes.Argument: return(new ExpressionContext((expression) => { if (expression == null) { _condition = SF.LiteralExpression(SyntaxKind.FalseKeyword); } else { _condition = expression.ToExpression(); } })); case CodeContextTypes.ElseIf: return(new IfContext(name, (ifStatement) => { _elseClause = SF.ElseClause(ifStatement); })); case CodeContextTypes.Else: string elseHookName = name; return(new BlockContext((block) => { if (elseHookName == null) { _elseClause = SF.ElseClause(block); } else { _elseClause = SF.ElseClause(BlockContext.MakeWriteStatement(MethodCallContext.CreateMethodCall(elseHookName, true))); } })); } return(base.CreateContext(contextType, name, attributes)); }
public override SyntaxNode VisitBlock(BlockSyntax node) { // Step I: rewrite loops var children = node.ChildNodes().OfType <StatementSyntax>().ToArray(); for (int i = 0; i < children.Length; i++) { if (children[i].IsKind(SK.WhileStatement)) { children[i] = (StatementSyntax)VisitWhileStatement(children[i] as WhileStatementSyntax); } if (children[i].IsKind(SK.ForStatement)) { children[i] = (StatementSyntax)VisitForStatement(children[i] as ForStatementSyntax); } } // Step II: join child blocks - {{...}{...}{...}} var childrenList = new List <StatementSyntax>(); for (int i = 0; i < children.Length; i++) { if (children[i].IsKind(SK.Block)) { childrenList.AddRange(children[i].ChildNodes().OfType <StatementSyntax>()); } else { childrenList.Add(children[i]); } } children = childrenList.ToArray(); // Step III: rewrite if statements to if-else var index = GetIndexOfOneBeforeLastStatement(children); while (index != -1) { StatementSyntax newStatement = null; var oneBeforeLastChild = children[index]; // TakeLast(children.Length - 1 - index); var remainingChildren = children.Skip(Math.Max(0, children.Length - (children.Length - 1 - index))); var oneBeforeLastChildStatements = GetStatementsFromBlock(oneBeforeLastChild.ChildNodes().OfType <StatementSyntax>()); IEnumerable <StatementSyntax> statements; if (oneBeforeLastChildStatements.Count() > 0 && (oneBeforeLastChildStatements.Last().IsKind(SK.ThrowStatement) || oneBeforeLastChildStatements.Last().IsKind(SK.ReturnStatement))) { // If last statement is a throw or return, do not append last statement statements = oneBeforeLastChildStatements; } else { var statementsList = oneBeforeLastChildStatements.ToList(); statementsList.AddRange(remainingChildren); statements = statementsList; } if (oneBeforeLastChild.IsKind(SK.IfStatement)) { var ifStatement = (IfStatementSyntax)oneBeforeLastChild; var elseChilds = GetStatementsFromBlock(ifStatement.Else?.ChildNodes().OfType <StatementSyntax>()); IEnumerable <StatementSyntax> elseStatements; if (elseChilds != null) { if (elseChilds.Count() > 0 && (elseChilds.Last().IsKind(SK.ThrowStatement) || elseChilds.Last().IsKind(SK.ReturnStatement))) { elseStatements = elseChilds; } else { var elseStatementsList = elseChilds.ToList(); elseStatementsList.AddRange(remainingChildren); elseStatements = elseStatementsList; } } else { elseStatements = new List <StatementSyntax>(remainingChildren); } var mainBlock = SF.Block(statements); var elseBlock = SF.Block(elseStatements); ElseClauseSyntax elseClause = elseStatements.Count() > 0 ? SF.ElseClause(elseBlock) : null; newStatement = SF.IfStatement(ifStatement.Condition, mainBlock, elseClause); } else { throw new NotImplementedException(oneBeforeLastChild.Kind().ToString()); } if (newStatement != null) { children = children.Take(index).Append(newStatement).ToArray(); } else { children = children.Take(index + 1).ToArray(); } index = GetIndexOfOneBeforeLastStatement(children); } var block = SF.Block(children); var result = base.VisitBlock(block); return(result); }
public override SyntaxNode VisitIfStatement(IfStatementSyntax node) { var timer = new Stopwatch(); var successLogPath = ""; var failureLogPath = ""; BlockSyntax successChildBlock; BlockSyntax failureChildBlock; StatementSyntax successStatement = null; StatementSyntax failureStatement = null; // Separate multi-component conditions node = SeparateIfConditions(node); var condition = VisitExpression(node.Condition); // If - success path SuccessConditions.Add(condition); foreach (var c in SuccessConditions) { if (c.IsKind(SK.LogicalNotExpression)) { successLogPath += $"{c} && "; } else { successLogPath += $"({c}) && "; } } successLogPath = successLogPath.TrimEnd(' ', '&'); string[] successModel = null; var timeout = false; try { timer.Restart(); successModel = GetModel(Parameters, SuccessConditions); timer.Stop(); } catch (TimeoutException) { timeout = true; } // TODO: verify creation of a child block successChildBlock = SF.Block(GetStatementsFromBlock(node.ChildNodes().OfType <StatementSyntax>())); if (successModel != null) { // Check if end of path var ifTrueChild = GetStatementAsBlock(node.Statement).ChildNodes(); if (ifTrueChild.OfType <ReturnStatementSyntax>().ToArray().Length != 0 || ifTrueChild.OfType <ThrowStatementSyntax>().ToArray().Length != 0) { results.Add(successModel); logger?.Info($"SUCCESS PATH: {successLogPath} [{timer.Elapsed.ToString(timeFormat)}]"); } successStatement = (StatementSyntax)RewriterTrue.Visit(successChildBlock); } else { var status = "UNSATISFIABLE"; if (timeout) { status = "TIMEOUT"; } logger?.Trace($"{status}: {successLogPath} [{timer.Elapsed.ToString(timeFormat)}]"); // Do not visit unsatisfiable paths if (VisitUnsatPaths || (timeout && VisitTimeoutPaths)) { successStatement = (StatementSyntax)RewriterTrue.Visit(successChildBlock); } else { successStatement = successChildBlock; } } // Else - failure path var negatedCondition = SF.PrefixUnaryExpression(SK.LogicalNotExpression, SF.ParenthesizedExpression(condition)); FailureConditions.Add(negatedCondition); foreach (var c in FailureConditions) { if (c.IsKind(SK.LogicalNotExpression)) { failureLogPath += $"{c} && "; } else { failureLogPath += $"({c}) && "; } } failureLogPath = failureLogPath.TrimEnd(' ', '&'); string[] failureModel = null; timeout = false; try { timer.Restart(); failureModel = GetModel(Parameters, FailureConditions); timer.Stop(); } catch (TimeoutException) { timeout = true; } if (failureModel != null) { if (node.Else != null) { // Check if end of path var ifFalseChild = GetStatementAsBlock(node.Else.Statement).ChildNodes(); if (ifFalseChild.OfType <ReturnStatementSyntax>().ToArray().Length != 0 || ifFalseChild.OfType <ThrowStatementSyntax>().ToArray().Length != 0) { results.Add(failureModel); logger?.Info($"FAILURE PATH: {failureLogPath} [{timer.Elapsed.ToString(timeFormat)}]"); } failureChildBlock = SF.Block(GetStatementsFromBlock(node.Else.ChildNodes().OfType <StatementSyntax>())); failureStatement = (StatementSyntax)RewriterFalse.Visit(failureChildBlock); } else { // End of path and nothing to return } } else { var status = "UNSATISFIABLE"; if (timeout) { status = "TIMEOUT"; } logger?.Trace($"{status}: {failureLogPath} [{ timer.Elapsed.ToString(timeFormat)}]"); if (node.Else != null) { failureChildBlock = SF.Block(GetStatementsFromBlock(node.Else.ChildNodes().OfType <StatementSyntax>())); // Do not visit unsatisfiable paths if (VisitUnsatPaths || (timeout && VisitTimeoutPaths)) { failureStatement = (StatementSyntax)RewriterFalse.Visit(failureChildBlock); } else { failureStatement = failureChildBlock; } } else { // TODO: needs verification // End of path and nothing to return //return node; } } if (failureStatement != null) { return(SF.IfStatement(node.Condition, successStatement, SF.ElseClause(failureStatement))); } return(SF.IfStatement(node.Condition, successStatement)); }