예제 #1
0
        private static void ReadMethodLocations(Dictionary <string, string> methodLocations, string pdbFixturePath, string peFixturePath)
        {
            var host = new PeReader.DefaultHost();

            var pdbFileStream = File.OpenRead(pdbFixturePath);

            var peReader  = new PeReader(host);
            var pdbReader = new PdbReader(pdbFileStream, host);

            var assembly = peReader.OpenAssembly(
                BinaryDocument.GetBinaryDocumentForFile(peFixturePath, host)
                );

            foreach (var type in assembly.GetAllTypes())
            {
                foreach (var member in type.Members)
                {
                    foreach (var sourceLocation in pdbReader.GetPrimarySourceLocationsFor(member.Locations))
                    {
                        var memberName = $"{member}";

                        if (!methodLocations.ContainsKey(memberName))
                        {
                            methodLocations.Add(
                                memberName,
                                $"{sourceLocation.SourceDocument.Name}:{sourceLocation.StartLine}"
                                );
                        }
                    }
                }
            }
        }
예제 #2
0
 public static IEnumerable <OperationLocation> LocatedOperations(this IMethodDefinition method, PdbReader pdb)
 {
     return((from o in method.Body.Operations
             from l in pdb.GetPrimarySourceLocationsFor(o.Location)
             select new OperationLocation {
         Operation = o, Location = l
     }).ToList());
 }
예제 #3
0
            public override void Traverse(IMethodBody methodBody)
            {
                PrintToken(CSharpToken.LeftCurly);

                ISourceMethodBody /*?*/ sourceMethodBody = methodBody as ISourceMethodBody;

                if (sourceMethodBody == null)
                {
                    var options = DecompilerOptions.Loops;
                    if (!printCompilerGeneratedMembers)
                    {
                        options |= (DecompilerOptions.AnonymousDelegates | DecompilerOptions.Iterators);
                    }
                    sourceMethodBody = new SourceMethodBody(methodBody, host, pdbReader, pdbReader, options);
                }
                if (noIL)
                {
                    Traverse(sourceMethodBody.Block.Statements);
                }
                else
                {
                    // this.Traverse(sourceMethodBody.Block);
                    //  PrintToken(CSharpToken.NewLine);

                    if (pdbReader != null)
                    {
                        PrintScopes(methodBody);
                    }
                    else
                    {
                        PrintLocals(methodBody.LocalVariables);
                    }

                    int currentIndex = -1; // a number no index matches
                    foreach (IOperation operation in methodBody.Operations)
                    {
                        if (pdbReader != null)
                        {
                            foreach (IPrimarySourceLocation psloc in pdbReader.GetPrimarySourceLocationsFor(operation.Location))
                            {
                                if (psloc.StartIndex != currentIndex)
                                {
                                    PrintSourceLocation(psloc);
                                    currentIndex = psloc.StartIndex;
                                }
                            }
                        }
                        PrintOperation(operation);
                    }
                }

                PrintToken(CSharpToken.RightCurly);
            }
예제 #4
0
        private void ProcessMethodDefinition(IMethodDefinition methodDefinition, PdbReader pdbReader)
        {
            uint token = GetMethodToken_ByReadingInternals(methodDefinition);

            if (token != 0)
            {
                var locations  = pdbReader.GetPrimarySourceLocationsFor(methodDefinition.Locations);
                var enumerator = locations.GetEnumerator();

                if (enumerator.MoveNext())
                {
                    IPrimarySourceLocation location = enumerator.Current;
                    map.Add(token, new CodeLocation(location.PrimarySourceDocument.Location, location.StartLine, 0));
                }
            }
        }
예제 #5
0
 private static IEnumerable <IPrimarySourceLocation> GetValidLocations(this IObjectWithLocations locatable, PdbReader pdb)
 {
     return(from l in pdb.GetPrimarySourceLocationsFor(locatable.Locations)
            where l.Length != 0
            select l);
 }
예제 #6
0
        public List <TestAssembly> FindTestsInProjects(List <ProjectDirectoryWrapper> assemblies)
        {
            List <TestWrapper> tests = new List <TestWrapper>();

            using (var host = new PeReader.DefaultHost())
            {
                foreach (var pathWrapper in assemblies)
                {
                    if (pathWrapper != null && !string.IsNullOrEmpty(pathWrapper.OutputDirectory))
                    {
                        var module = host.LoadUnitFrom(pathWrapper.OutputDirectory.Replace(" ", "#")) as IModule;

                        foreach (var type in module.GetAllTypes())
                        {
                            foreach (var method in type.Methods)
                            {
                                if (method.Attributes.Any(attribute => attribute.Type.ToString() == typeof(TestAttribute).ToString() || attribute.Type.ToString() == typeof(TestMethodAttribute).ToString()))
                                {
                                    foreach (var location in method.Locations)
                                    {
                                        PdbReader pdbReader = null;
                                        string    pdbFile   = Path.ChangeExtension(module.Location, "pdb");
                                        if (File.Exists(pdbFile))
                                        {
                                            Stream pdbStream = File.OpenRead(pdbFile);
                                            pdbReader = new PdbReader(pdbStream, host);

                                            using (pdbReader)
                                            {
                                                var sourceLocations = pdbReader.GetPrimarySourceLocationsFor(location);

                                                foreach (var sourceLocation in sourceLocations)
                                                {
                                                    tests.Add(new TestWrapper()
                                                    {
                                                        AssemblyPath   = module.Location,
                                                        AssemblyName   = module.Name.Value,
                                                        ClassName      = type.Name.Value,
                                                        ClassNamespace = type.ToString().Substring(0, type.ToString().Length - type.Name.Value.Length - 1),
                                                        LineNumber     = sourceLocation.StartLine,
                                                        Filename       = sourceLocation.SourceDocument.Location,
                                                        MethodName     = method.Name.Value
                                                    });
                                                    // Yay!!
                                                }
                                            }
                                        }
                                        else
                                        {
                                            // Just add the class, no line numbers
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(ExtractAssembliesFromWrapper(tests));
        }