Example #1
0
        public Tuple <IEnumerable <IMethodDefinition>, IEnumerable <IMethodReference> > ComputeDelegate(IVariable delegateArgument, SimplePointsToGraph ptg)
        {
            var resolvedCallees     = new HashSet <IMethodDefinition>();
            var unresolvedCallees   = new HashSet <IMethodReference>();
            var potentialDelegates  = ptg.GetTargets(delegateArgument);
            var resolvedInvocations = potentialDelegates.OfType <DelegateNode>()
                                      .Select(d => d.Instance.Type.FindMethodImplementation(d.Method));

            resolvedCallees.UnionWith(resolvedInvocations.OfType <IMethodDefinition>());
            unresolvedCallees.UnionWith(resolvedInvocations.Where(c => !resolvedCallees.Contains(c)));
            return(new Tuple <IEnumerable <IMethodDefinition>, IEnumerable <IMethodReference> >(resolvedCallees, unresolvedCallees));
        }
Example #2
0
        public Tuple <IEnumerable <IMethodDefinition>, IEnumerable <IMethodReference> > ComputePotentialCallees(MethodCallInstruction instruction, SimplePointsToGraph ptg)
        {
            var resolvedCallees      = new HashSet <IMethodDefinition>();
            var unresolvedCallees    = new HashSet <IMethodReference>();
            var potentalDelegateCall = instruction.Method;

            // If it is a delegate
            if (potentalDelegateCall.Name.Value == "Invoke")
            {
                var classDef = (potentalDelegateCall.ContainingType.ResolvedType) as INamedTypeDefinition;
                if (classDef != null && classDef.IsDelegate)
                {
                    var delegateVar = instruction.Arguments[0];
                    return(ComputeDelegate(delegateVar, ptg));
                    //var potentialDelegates = ptg.GetTargets(delegateVar);
                    //var resolvedInvocations = potentialDelegates.OfType<DelegateNode>()
                    //    .Select(d => this.host.FindMethodImplementation(d.Instance.Type as BasicType, d.Method) as IMethodReference);
                    //resolvedCallees.UnionWith(resolvedInvocations.OfType<MethodDefinition>());
                    //unresolvedCallees.UnionWith(resolvedInvocations.Where(c => !resolvedCallees.Contains(c)));
                }
            }
            else
            {
                if (!instruction.Method.IsStatic && instruction.Method.Name.Value != ".ctor")
                {
                    var receiver            = instruction.Arguments[0];
                    var types               = ptg.GetTargets(receiver, false).Where(n => n.Kind != SimplePTGNodeKind.Null && n.Type != null).Select(n => n.Type);
                    var candidateCalless    = types.Select(t => t.FindMethodImplementation(instruction.Method));
                    var resolvedInvocations = candidateCalless.Select(c => c.ResolvedMethod as IMethodReference);
                    resolvedCallees.UnionWith(resolvedInvocations.OfType <IMethodDefinition>());
                    unresolvedCallees.UnionWith(candidateCalless.Where(c => !resolvedInvocations.Contains(c) &&
                                                                       (c.Name.Value != "Dispose" && c.ContainingType.ContainingAssembly() == "mscorlib")));
                    //unresolvedCallees.UnionWith(resolvedInvocations.Where(c => !(c is MethodDefinition)));
                }
                else
                {
                    var candidateCalee = instruction.Method.ContainingType.FindMethodImplementation(instruction.Method);
                    var resolvedCalle  = candidateCalee.ResolvedMethod;
                    if (resolvedCalle != null)
                    {
                        resolvedCallees.Add(resolvedCalle);
                    }
                }
            }
            return(new Tuple <IEnumerable <IMethodDefinition>, IEnumerable <IMethodReference> >(resolvedCallees, unresolvedCallees));
        }
        public static string Serialize(SimplePointsToGraph ptg)
        {
            using (var stringWriter = new StringWriter())
                using (var xmlWriter = new XmlTextWriter(stringWriter))
                {
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteStartElement("DirectedGraph");
                    xmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/vs/2009/dgml");
                    xmlWriter.WriteStartElement("Nodes");

                    foreach (var variable in ptg.Roots)
                    {
                        var label = variable.Name;

                        xmlWriter.WriteStartElement("Node");
                        xmlWriter.WriteAttributeString("Id", label);
                        xmlWriter.WriteAttributeString("Label", label);
                        xmlWriter.WriteAttributeString("Shape", "None");
                        xmlWriter.WriteEndElement();
                    }

                    foreach (var node in ptg.Nodes)
                    {
                        var nodeId = Convert.ToString(node.Id);
                        var label  = nodeId + ":" + Serialize(node);

                        xmlWriter.WriteStartElement("Node");
                        xmlWriter.WriteAttributeString("Id", nodeId);
                        xmlWriter.WriteAttributeString("Label", label);

                        if (node.Kind == SimplePTGNodeKind.Null)
                        {
                            xmlWriter.WriteAttributeString("Background", "Black");
                        }
                        else if (node.Kind == SimplePTGNodeKind.Global)
                        {
                            xmlWriter.WriteAttributeString("Background", "Blue");
                        }
                        else if (node.Kind == SimplePTGNodeKind.Delegate)
                        {
                            xmlWriter.WriteAttributeString("Background", "Cyan");
                        }
                        else if (node.Kind == SimplePTGNodeKind.Parameter)
                        {
                            xmlWriter.WriteAttributeString("Background", "Red");
                            xmlWriter.WriteAttributeString("StrokeDashArray", "6,6");
                        }
                        else if (node.Kind == SimplePTGNodeKind.Unknown)
                        {
                            xmlWriter.WriteAttributeString("Background", "Yellow");
                            xmlWriter.WriteAttributeString("StrokeDashArray", "6,6");
                        }

                        xmlWriter.WriteEndElement();
                    }

                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteStartElement("Links");

                    foreach (var variable in ptg.Roots)
                    {
                        var sourceId = variable.Name;
                        foreach (var target in ptg.GetTargets(variable))
                        {
                            var targetId = Convert.ToString(target.Id);

                            xmlWriter.WriteStartElement("Link");
                            xmlWriter.WriteAttributeString("Source", sourceId);
                            xmlWriter.WriteAttributeString("Target", targetId);
                            xmlWriter.WriteEndElement();
                        }
                    }

                    foreach (var node in ptg.Nodes)
                    {
                        var sourceId = Convert.ToString(node.Id);

                        foreach (var targetMap in ptg.GetTargets(node))
                        {
                            foreach (var target in targetMap.Value)
                            {
                                var targetId = Convert.ToString(target.Id);
                                var label    = targetMap.Key.Name;

                                xmlWriter.WriteStartElement("Link");
                                xmlWriter.WriteAttributeString("Source", sourceId);
                                xmlWriter.WriteAttributeString("Target", targetId);
                                xmlWriter.WriteAttributeString("Label", label.Value);
                                xmlWriter.WriteEndElement();
                            }
                        }
                    }

                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteStartElement("Styles");
                    xmlWriter.WriteStartElement("Style");
                    xmlWriter.WriteAttributeString("TargetType", "Node");

                    xmlWriter.WriteStartElement("Setter");
                    xmlWriter.WriteAttributeString("Property", "FontFamily");
                    xmlWriter.WriteAttributeString("Value", "Consolas");
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("Setter");
                    xmlWriter.WriteAttributeString("Property", "NodeRadius");
                    xmlWriter.WriteAttributeString("Value", "5");
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("Setter");
                    xmlWriter.WriteAttributeString("Property", "MinWidth");
                    xmlWriter.WriteAttributeString("Value", "0");
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndElement();
                    xmlWriter.Flush();
                    return(stringWriter.ToString());
                }
        }