示例#1
0
        private void constructReachingDef(SyntaxNode node)
        {
            if (node == null)
            {
                return;
            }

            if (!reachingDefDictionary.ContainsKey(node))
            {
                reachingDefDictionary[node] = new InOutData <SyntaxNode>();
                reachingRunAgain            = true;
            }

            ISet <SyntaxNode> previousNodes = cfg.GetPredecessors(node);

            if (previousNodes.Count != 0)
            {
                foreach (SyntaxNode previousNode in previousNodes)
                {
                    if (reachingDefDictionary.ContainsKey(previousNode))
                    {
                        if (!reachingDefDictionary[previousNode].outSet.IsSubsetOf(reachingDefDictionary[node].inSet))
                        {
                            reachingDefDictionary[node].inSet.UnionWith(reachingDefDictionary[previousNode].outSet);
                            reachingRunAgain = true;
                        }
                    }
                }
            }

            AssignmentExpressionSyntax      assignmentExpressionSyntax      = node.DescendantNodesAndSelf().OfType <AssignmentExpressionSyntax>().FirstOrDefault();
            LocalDeclarationStatementSyntax localDeclarationStatementSyntax = node as LocalDeclarationStatementSyntax;
            VariableDeclarationSyntax       variableDeclarationSyntax       = node as VariableDeclarationSyntax;

            if (assignmentExpressionSyntax != null)
            {
                ISet <SyntaxNode> toRemove = new HashSet <SyntaxNode>();
                foreach (SyntaxNode inDef in reachingDefDictionary[node].inSet)
                {
                    String toTest = getAssignmentOrLocalVarName(inDef);
                    if (toTest != null && toTest.Equals(getAssignmentOrLocalVarName(assignmentExpressionSyntax)))
                    {
                        toRemove.Add(inDef);
                    }
                }
                ISet <SyntaxNode> InWithRemovedNodesRemoved = copySet(reachingDefDictionary[node].inSet);
                InWithRemovedNodesRemoved.ExceptWith(toRemove);
                InWithRemovedNodesRemoved.Add(assignmentExpressionSyntax);

                if (!InWithRemovedNodesRemoved.SetEquals(reachingDefDictionary[node].inSet) && !InWithRemovedNodesRemoved.SetEquals(reachingDefDictionary[node].outSet))
                {
                    reachingDefDictionary[node].outSet = InWithRemovedNodesRemoved;
                    reachingRunAgain = true;
                }
            }
            else if (localDeclarationStatementSyntax != null)
            {
                ISet <SyntaxNode> toRemove = new HashSet <SyntaxNode>();
                foreach (SyntaxNode inDef in reachingDefDictionary[node].inSet)
                {
                    String toTest = getAssignmentOrLocalVarName(inDef);
                    if (toTest != null && toTest.Equals(getAssignmentOrLocalVarName(localDeclarationStatementSyntax)))
                    {
                        toRemove.Add(inDef);
                    }
                }
                ISet <SyntaxNode> InWithRemovedNodesRemoved = copySet(reachingDefDictionary[node].inSet);
                InWithRemovedNodesRemoved.ExceptWith(toRemove);
                InWithRemovedNodesRemoved.Add(localDeclarationStatementSyntax);

                if (!InWithRemovedNodesRemoved.SetEquals(reachingDefDictionary[node].inSet) && !InWithRemovedNodesRemoved.SetEquals(reachingDefDictionary[node].outSet))
                {
                    reachingDefDictionary[node].outSet = InWithRemovedNodesRemoved;
                    reachingRunAgain = true;
                }
            }
            else if (variableDeclarationSyntax != null)
            {
                ISet <SyntaxNode> toRemove = new HashSet <SyntaxNode>();
                foreach (SyntaxNode inDef in reachingDefDictionary[node].inSet)
                {
                    String toTest = getAssignmentOrLocalVarName(inDef);
                    if (toTest != null && toTest.Equals(getAssignmentOrLocalVarName(variableDeclarationSyntax)))
                    {
                        toRemove.Add(inDef);
                    }
                }
                ISet <SyntaxNode> InWithRemovedNodesRemoved = copySet(reachingDefDictionary[node].inSet);
                InWithRemovedNodesRemoved.ExceptWith(toRemove);
                InWithRemovedNodesRemoved.Add(variableDeclarationSyntax);

                if (!InWithRemovedNodesRemoved.SetEquals(reachingDefDictionary[node].inSet) && !InWithRemovedNodesRemoved.SetEquals(reachingDefDictionary[node].outSet))
                {
                    reachingDefDictionary[node].outSet = InWithRemovedNodesRemoved;
                    reachingRunAgain = true;
                }
            }
            else
            {
                if (!reachingDefDictionary[node].outSet.SetEquals(reachingDefDictionary[node].inSet))
                {
                    reachingDefDictionary[node].outSet.UnionWith(reachingDefDictionary[node].inSet);
                    reachingRunAgain = true;
                }
            }

            visitedNodes.Add(node);

            foreach (SyntaxNode successor in cfg.GetSuccessors(node))
            {
                if (!visitedNodes.Contains(successor))
                {
                    constructReachingDef(successor);
                }
            }
        }
示例#2
0
        private void constructLiveVar(SyntaxNode node)
        {
            if (node == null)
            {
                return;
            }

            if (!liveVarDictionary.ContainsKey(node))
            {
                liveVarDictionary[node] = new InOutData <IdentifierNameSyntax>();
                liveVarRunAgain         = true;
            }

            //Set up out set
            ISet <SyntaxNode> subsequentNodes = cfg.GetSuccessors(node);

            if (subsequentNodes.Count != 0)
            {
                foreach (SyntaxNode subsequentNode in subsequentNodes)
                {
                    if (liveVarDictionary.ContainsKey(subsequentNode))
                    {
                        if (!liveVarDictionary[subsequentNode].inSet.IsSubsetOf(liveVarDictionary[node].outSet))
                        {
                            liveVarDictionary[node].outSet.UnionWith(liveVarDictionary[subsequentNode].inSet);
                            liveVarRunAgain = true;
                        }
                    }
                }
            }

            //Set up in set
            AssignmentExpressionSyntax      assignmentExpressionSyntax      = node.DescendantNodesAndSelf().OfType <AssignmentExpressionSyntax>().FirstOrDefault();
            LocalDeclarationStatementSyntax localDeclarationStatementSyntax = node as LocalDeclarationStatementSyntax;
            VariableDeclarationSyntax       variableDeclarationSyntax       = node as VariableDeclarationSyntax;

            if (assignmentExpressionSyntax != null)
            {
                ISet <IdentifierNameSyntax> toRemove = new HashSet <IdentifierNameSyntax>();
                foreach (IdentifierNameSyntax identifierNameSyntax in liveVarDictionary[node].outSet)
                {
                    if (getAssignmentOrLocalVarName(assignmentExpressionSyntax).Equals(identifierNameSyntax.ToString()))
                    {
                        toRemove.Add(identifierNameSyntax);
                    }
                }
                ISet <IdentifierNameSyntax> OutWithRemovedNodesRemoved = copySet(liveVarDictionary[node].outSet);
                OutWithRemovedNodesRemoved.ExceptWith(toRemove);

                liveVarHelper(node, OutWithRemovedNodesRemoved);

                if (!OutWithRemovedNodesRemoved.SetEquals(liveVarDictionary[node].inSet) && !OutWithRemovedNodesRemoved.SetEquals(liveVarDictionary[node].outSet))
                {
                    liveVarDictionary[node].inSet = OutWithRemovedNodesRemoved;
                    liveVarRunAgain = true;
                }
            }
            else if (localDeclarationStatementSyntax != null)
            {
                ISet <IdentifierNameSyntax> toRemove = new HashSet <IdentifierNameSyntax>();
                foreach (IdentifierNameSyntax identifierNameSyntax in liveVarDictionary[node].outSet)
                {
                    if (getAssignmentOrLocalVarName(localDeclarationStatementSyntax).Equals(identifierNameSyntax.ToString()))
                    {
                        toRemove.Add(identifierNameSyntax);
                    }
                }
                ISet <IdentifierNameSyntax> OutWithRemovedNodesRemoved = copySet(liveVarDictionary[node].outSet);
                OutWithRemovedNodesRemoved.ExceptWith(toRemove);

                liveVarHelper(node, OutWithRemovedNodesRemoved);

                if (!OutWithRemovedNodesRemoved.SetEquals(liveVarDictionary[node].inSet) && !OutWithRemovedNodesRemoved.SetEquals(liveVarDictionary[node].outSet))
                {
                    liveVarDictionary[node].inSet = OutWithRemovedNodesRemoved;
                    liveVarRunAgain = true;
                }
            }
            else if (variableDeclarationSyntax != null)
            {
                ISet <IdentifierNameSyntax> toRemove = new HashSet <IdentifierNameSyntax>();
                foreach (IdentifierNameSyntax identifierNameSyntax in liveVarDictionary[node].outSet)
                {
                    if (getAssignmentOrLocalVarName(variableDeclarationSyntax).Equals(identifierNameSyntax.ToString()))
                    {
                        if (liveVarDictionary[node].inSet.Contains(identifierNameSyntax))
                        {
                            toRemove.Add(identifierNameSyntax);
                        }
                    }
                }
                ISet <IdentifierNameSyntax> OutWithRemovedNodesRemoved = copySet(liveVarDictionary[node].outSet);
                OutWithRemovedNodesRemoved.ExceptWith(toRemove);

                liveVarHelper(node, OutWithRemovedNodesRemoved);

                if (!OutWithRemovedNodesRemoved.SetEquals(liveVarDictionary[node].inSet) && !OutWithRemovedNodesRemoved.SetEquals(liveVarDictionary[node].outSet))
                {
                    liveVarDictionary[node].inSet = OutWithRemovedNodesRemoved;
                    liveVarRunAgain = true;
                }
            }
            else
            {
                ISet <IdentifierNameSyntax> outCopy = copySet(liveVarDictionary[node].outSet);

                liveVarHelper(node, outCopy);

                if (!outCopy.SetEquals(liveVarDictionary[node].inSet))
                {
                    liveVarDictionary[node].inSet = outCopy;
                    liveVarRunAgain = true;
                }
            }

            visitedNodes.Add(node);

            foreach (SyntaxNode predecessor in cfg.GetPredecessors(node))
            {
                if (!visitedNodes.Contains(predecessor))
                {
                    constructLiveVar(predecessor);
                }
            }
        }