Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a using block.
        /// </summary>
        private void ConstructUsingBlock(List <StatementSyntax> stmtList, ControlFlowNode successor,
                                         LoopHeadControlFlowNode innerLoopHead)
        {
            var usingStmt = stmtList[0] as UsingStatementSyntax;

            stmtList.RemoveAt(0);

            var usingNode = new ControlFlowNode(this.Graph, this.Summary);

            if (usingStmt.Declaration != null)
            {
                usingNode.Statements.Add(Statement.Create(usingStmt.Declaration, usingNode, this.Summary));
            }
            else
            {
                usingNode.Statements.Add(Statement.Create(usingStmt.Expression, usingNode, this.Summary));
            }

            var afterUsingNode = new ControlFlowNode(this.Graph, this.Summary);

            afterUsingNode.Construct(stmtList, successor, innerLoopHead);

            var bodyNode = new ControlFlowNode(this.Graph, this.Summary);

            bodyNode.Construct(GetStatements(usingStmt.Statement), afterUsingNode, innerLoopHead);

            this.ISuccessors.Add(usingNode);
            usingNode.IPredecessors.Add(this);

            usingNode.ISuccessors.Add(bodyNode);
            bodyNode.IPredecessors.Add(usingNode);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs a switch block.
        /// </summary>
        private void ConstructSwitchBlock(List <StatementSyntax> stmtList, ControlFlowNode successor,
                                          LoopHeadControlFlowNode innerLoopHead)
        {
            var switchStmt = stmtList[0] as SwitchStatementSyntax;

            stmtList.RemoveAt(0);

            var switchNode = new ControlFlowNode(this.Graph, this.Summary);

            switchNode.Statements.Add(Statement.Create(switchStmt.Expression, switchNode, this.Summary));

            var afterSwitchNode = new ControlFlowNode(this.Graph, this.Summary);

            afterSwitchNode.Construct(stmtList, successor, innerLoopHead);

            foreach (var section in switchStmt.Sections)
            {
                var sectionNode = new ControlFlowNode(this.Graph, this.Summary);
                sectionNode.Construct(section.Statements.ToList(), afterSwitchNode, null);

                switchNode.ISuccessors.Add(sectionNode);
                sectionNode.IPredecessors.Add(switchNode);
            }

            this.ISuccessors.Add(switchNode);
            switchNode.IPredecessors.Add(this);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates the control-flow graph nodes of the specified method summary.
        /// </summary>
        internal static IControlFlowNode Create(ControlFlowGraph cfg, MethodSummary summary)
        {
            var entryNode = new ControlFlowNode(cfg, summary);

            entryNode.Statements.Add(Statement.Create(summary.Method.ParameterList, entryNode, summary));
            entryNode.Construct(GetStatements(summary.Method.Body), null, null);
            return(entryNode);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructs a naked code block.
        /// </summary>
        private void ConstructNakedCodeBlock(List <StatementSyntax> stmtList, ControlFlowNode successor,
                                             LoopHeadControlFlowNode innerLoopHead)
        {
            var blockStmt = stmtList[0] as BlockSyntax;

            stmtList.RemoveAt(0);

            var afterBlockNode = new ControlFlowNode(this.Graph, this.Summary);

            afterBlockNode.Construct(stmtList, successor, innerLoopHead);

            var blockNode = new ControlFlowNode(this.Graph, this.Summary);

            blockNode.Construct(GetStatements(blockStmt), afterBlockNode, innerLoopHead);

            this.ISuccessors.Add(blockNode);
            blockNode.IPredecessors.Add(this);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructs a loop from the specified loop guard and body.
        /// </summary>
        private void ConstructLoop(ExpressionSyntax loopGuard, StatementSyntax loopBody,
                                   List <StatementSyntax> stmtList, ControlFlowNode successor,
                                   LoopHeadControlFlowNode innerLoopHead)
        {
            var falseNode    = new ControlFlowNode(this.Graph, this.Summary);
            var trueNode     = new ControlFlowNode(this.Graph, this.Summary);
            var loopHeadNode = new LoopHeadControlFlowNode(this.Graph, this.Summary, falseNode);

            loopHeadNode.Statements.Add(Statement.Create(loopGuard, loopHeadNode, this.Summary));
            falseNode.Construct(stmtList, successor, innerLoopHead);
            trueNode.Construct(GetStatements(loopBody), loopHeadNode, loopHeadNode);

            this.ISuccessors.Add(loopHeadNode);
            loopHeadNode.IPredecessors.Add(this);

            loopHeadNode.ISuccessors.Add(falseNode);
            falseNode.IPredecessors.Add(loopHeadNode);

            loopHeadNode.ISuccessors.Add(trueNode);
            trueNode.IPredecessors.Add(loopHeadNode);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructs an if-then-else branch.
        /// </summary>
        private void ConstructIfThenElseBranch(List <StatementSyntax> stmtList, ControlFlowNode successor, LoopHeadControlFlowNode innerLoopHead)
        {
            var ifStmt = stmtList[0] as IfStatementSyntax;

            stmtList.RemoveAt(0);

            var ifBranchNode = new ControlFlowNode(this.Graph, this.Summary);

            ifBranchNode.Statements.Add(Statement.Create(ifStmt.Condition, ifBranchNode, this.Summary));
            this.ISuccessors.Add(ifBranchNode);
            ifBranchNode.IPredecessors.Add(this);

            ControlFlowNode elseBranchNode = null;
            ControlFlowNode falseNode      = null;

            if (ifStmt.Else != null)
            {
                elseBranchNode = new ControlFlowNode(this.Graph, this.Summary);
                falseNode      = new ControlFlowNode(this.Graph, this.Summary);

                falseNode.Construct(stmtList, successor, innerLoopHead);
                elseBranchNode.Construct(GetStatements(ifStmt.Else.Statement), falseNode, innerLoopHead);
                ifBranchNode.ISuccessors.Add(elseBranchNode);
                elseBranchNode.IPredecessors.Add(ifBranchNode);
            }
            else
            {
                falseNode = new ControlFlowNode(this.Graph, this.Summary);
                falseNode.Construct(stmtList, successor, innerLoopHead);
                ifBranchNode.ISuccessors.Add(falseNode);
                falseNode.IPredecessors.Add(ifBranchNode);
            }

            var trueNode = new ControlFlowNode(this.Graph, this.Summary);

            trueNode.Construct(GetStatements(ifStmt.Statement), falseNode, innerLoopHead);
            ifBranchNode.ISuccessors.Add(trueNode);
            trueNode.IPredecessors.Add(ifBranchNode);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Constructs a do-while loop.
        /// </summary>
        private void ConstructDoWhileLoop(List <StatementSyntax> stmtList, ControlFlowNode successor,
                                          LoopHeadControlFlowNode innerLoopHead)
        {
            var loopHeadStmt = stmtList[0] as DoStatementSyntax;

            stmtList.RemoveAt(0);

            var falseNode    = new ControlFlowNode(this.Graph, this.Summary);
            var trueNode     = new ControlFlowNode(this.Graph, this.Summary);
            var loopHeadNode = new LoopHeadControlFlowNode(this.Graph, this.Summary, falseNode);

            trueNode.Construct(GetStatements(loopHeadStmt.Statement), loopHeadNode, loopHeadNode);
            loopHeadNode.Statements.Add(Statement.Create(loopHeadStmt.Condition, loopHeadNode, this.Summary));
            falseNode.Construct(stmtList, successor, innerLoopHead);

            this.ISuccessors.Add(trueNode);
            trueNode.IPredecessors.Add(this);

            loopHeadNode.ISuccessors.Add(falseNode);
            falseNode.IPredecessors.Add(loopHeadNode);

            loopHeadNode.ISuccessors.Add(trueNode);
            trueNode.IPredecessors.Add(loopHeadNode);
        }