public TypeHandler(Assembly[] assemblies,
                           NamespaceFilter nameFilter,
                           NamespaceFilter namespaceFollowFilter,
                           DataCollector dataCollector,
                           PdbLocator pdbLocator)
        {
            this.assemblies            = assemblies;
            this.nameFilter            = nameFilter;
            this.namespaceFollowFilter = namespaceFollowFilter;
            this.dataCollector         = dataCollector;
            this.pdbLocator            = pdbLocator;

            foreach (var assembly in assemblies)
            {
                Cache.CollectedAssemblies.Add(assembly, 1);
            }
        }
Esempio n. 2
0
        public void Index(string outputFileName)
        {
            // create the Sourcetrail data collector
            var dataCollector = new DataCollector(outputFileName);

            var pdbLocator = new PdbLocator();
            // set up the type handler
            var typeHandler = new TypeHandler(assemblies, nameFilter, namespaceFollowFilter, dataCollector, pdbLocator);

            typeHandler.MethodCollected += (sender, args) => collectedMethods.Add(args.CollectedMethod);

            foreach (var assembly in assemblies)
            {
                Console.WriteLine("Indexing assembly {0}{1}", assembly.Location, Environment.NewLine);

                pdbLocator.AddAssembly(assembly);
                try
                {
                    Console.WriteLine("Collecting types...");
                    // collect all types first
                    foreach (var type in assembly.GetTypes())
                    {
                        typeHandler.AddToDbIfValid(type);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception indexing assemby '{0}'\r\n{1}", assembly.Location, ex);
                }
            }

            Console.WriteLine("{1}Collected {0} types{1}", Cache.CollectedTypes.Count, Environment.NewLine);

            // set up the visitor for parsed methods
            var referenceVisitor = new MethodReferenceVisitor(typeHandler, dataCollector, pdbLocator);
            var ilParser         = new ILParser(referenceVisitor);

            referenceVisitor.ParseMethod += (sender, args) => CollectReferencesFromILCode(
                ilParser,
                args.CollectedMethod.Method, args.CollectedMethod.MethodId, args.CollectedMethod.ClassId);
            // parse IL of colected methods
            HandleCollectedMethods(ilParser);

            dataCollector.Dispose();
        }
        private void CollectReferenceLocation(MethodBase referencingMethod, int referenceId, int ilOffsetOfReference)
        {
            var pdbReader = pdbLocator.GetPdbReaderForAssembly(referencingMethod.DeclaringType.Assembly);

            if (pdbReader != null)
            {
                var pdbMethod = pdbReader.GetMethod(referencingMethod.MetadataToken);
                if (pdbMethod != null)
                {
                    var seq = pdbMethod.GetSequenceForILOffset(ilOffsetOfReference);
                    if (seq != null)
                    {
                        var fileId = dataCollector.CollectFile(pdbMethod.DocumentName, pdbMethod.LanguageName);
                        // had to subtract 1 from EndColumn, otherwise text disappears in Sourcetrail
                        DataCollector.CollectReferenceLocation(referenceId, fileId,
                                                               seq.StartLine, seq.StartColumn, seq.EndLine, seq.EndColumn - 1);
                    }
                }
            }
        }