Exemple #1
0
        private static ImmutableArray <ISymbol> GetMembers(
            INamespaceOrTypeSymbol container,
            string qualifiedName,
            out INamespaceOrTypeSymbol lastContainer
            )
        {
            var parts = SplitMemberName(qualifiedName);

            lastContainer = container;
            for (int i = 0; i < parts.Length - 1; i++)
            {
                var nestedContainer = (INamespaceOrTypeSymbol)lastContainer.GetMember(parts[i]);
                if (nestedContainer == null)
                {
                    // If there wasn't a nested namespace or type with that name, assume it's a
                    // member name that includes dots (e.g. explicit interface implementation).
                    return(lastContainer.GetMembers(string.Join(".", parts.Skip(i))));
                }
                else
                {
                    lastContainer = nestedContainer;
                }
            }

            return(lastContainer.GetMembers(parts[parts.Length - 1]));
        }
Exemple #2
0
 private void dfs_through_members(TreeNodeCollection treenode, INamespaceOrTypeSymbol symbol)
 {
     if (symbol != null && symbol.GetMembers() != null)
     foreach (var el in symbol.GetMembers()) {
         var cur = el as INamespaceOrTypeSymbol;
         if (cur != null) {
             treenode.Add(cur.Name.ToString());
             dfs_through_members(treenode[treenode.Count - 1].Nodes, cur);
         }
     }
 }
        private void Bind(
            int index, INamespaceOrTypeSymbol rootContainer, ArrayBuilder <ISymbol> results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var node = _nodes[index];

            if (node.IsRoot)
            {
                return;
            }

            if (_nodes[node.ParentIndex].IsRoot)
            {
                results.AddRange(rootContainer.GetMembers(GetName(node)));
            }
            else
            {
                using var _ = ArrayBuilder <ISymbol> .GetInstance(out var containerSymbols);

                Bind(node.ParentIndex, rootContainer, containerSymbols, cancellationToken);

                foreach (var containerSymbol in containerSymbols)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (containerSymbol is INamespaceOrTypeSymbol nsOrType)
                    {
                        results.AddRange(nsOrType.GetMembers(GetName(node)));
                    }
                }
            }
        }
 private void Visit(INamespaceOrTypeSymbol type)
 {
     foreach (var method in type.GetMembers().OfType <IMethodSymbol>())
     {
         Visit(method);
     }
 }
Exemple #5
0
 static void AddInheritedMembers(
     ITypeSymbol symbol,
     INamespaceOrTypeSymbol type,
     Dictionary <string, string?> dict,
     IReadOnlyList <string> typeParameterNames,
     IApiFilter apiFilter
     )
 {
     foreach (var m in type.GetMembers()
              .Where(m => !(m is INamedTypeSymbol))
              .Where(
                  m => apiFilter.CanVisitApi(
                      m,
                      symbol.Equals(type) ||
                      !symbol.IsSealed ||
                      symbol.TypeKind != TypeKind.Struct
                      )
                  )
              .Where(x => x.IsInheritable()))
     {
         var id = symbol.ToString()?.Trim();
         if (id != null)
         {
             dict.TryAdd(id, type.Equals(symbol) ? null : m.ToDisplayString());
         }
     }
 }
 private void VisitNamespaceOrType(INamespaceOrTypeSymbol symbol)
 {
     foreach (ISymbol member in symbol.GetMembers())
     {
         Visit(member);
     }
 }
        // returns all the symbols in the container corresponding to the node
        private void Bind(int index, INamespaceOrTypeSymbol rootContainer, List <ISymbol> results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var node = this.nodes[index];

            if (this.nodes[node.ParentIndex].IsRoot)
            {
                results.AddRange(rootContainer.GetMembers(node.Name));
            }
            else
            {
                var containerSymbols = SharedPools.Default <List <ISymbol> >().AllocateAndClear();
                try
                {
                    Bind(node.ParentIndex, rootContainer, containerSymbols, cancellationToken);

                    foreach (var containerSymbol in containerSymbols.OfType <INamespaceOrTypeSymbol>())
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        results.AddRange(containerSymbol.GetMembers(node.Name));
                    }
                }
                finally
                {
                    SharedPools.Default <List <ISymbol> >().ClearAndFree(containerSymbols);
                }
            }
        }
Exemple #8
0
        private static IEnumerable <(ISymbol Original, string NewName)> GetSymbolsWithNewNames(INamespaceOrTypeSymbol containerSymbol, Compilation compilation)
        {
            var members    = containerSymbol.GetMembers();
            var symbolSets = GetLocalSymbolSets(containerSymbol, compilation, members).Concat(members.AsEnumerable().Yield());

            return(symbolSets.SelectMany(GetUniqueNamesForSymbolSet));
        }
Exemple #9
0
        private static void GetSourceMemberSymbols(INamespaceOrTypeSymbol symbol, List <ISymbol> list, LocalSymbolDumper localDumper)
        {
            foreach (var memberSymbol in symbol.GetMembers())
            {
                list.Add(memberSymbol);

                switch (memberSymbol.Kind)
                {
                case SymbolKind.NamedType:
                case SymbolKind.Namespace:
                    GetSourceMemberSymbols((INamespaceOrTypeSymbol)memberSymbol, list, localDumper);
                    break;

                case SymbolKind.Method:
                    var method = (IMethodSymbol)memberSymbol;
                    foreach (var parameter in method.Parameters)
                    {
                        list.Add(parameter);
                    }

                    localDumper?.GetLocalSymbols(method.GetSymbol(), list);

                    break;

                case SymbolKind.Field:
                    localDumper?.GetLocalSymbols(memberSymbol.GetSymbol <FieldSymbol>(), list);

                    break;
                }
            }
        }
        private void AddNodeIfNotExists(INamespaceOrTypeSymbol namedType, IntertypeRelationGraph graph, List <INamedTypeSymbol> typeSymbols)
        {
            if (namedType == null)
            {
                return;
            }

            if (namedType.IsType)
            {
                typeSymbols.Add((INamedTypeSymbol)namedType);

                var typeName = GetTypeIdentifier(namedType);
                if (!graph.Nodes.Contains(typeName))
                {
                    graph.Nodes.Add(typeName);
                }
            }
            else
            {
                foreach (var type in namedType.GetMembers())
                {
                    AddNodeIfNotExists(type as INamespaceOrTypeSymbol, graph, typeSymbols);
                }
            }
        }
Exemple #11
0
 protected void ProcessChildren(INamespaceOrTypeSymbol namespaceOrTypeSymbol, PolicyConfig policyConfig, IViolationReporter violationReporter)
 {
     foreach (ISymbol childSymbol in namespaceOrTypeSymbol.GetMembers())
     {
         new CodeCommentsFacade().GetSymbolProcessorFactory().CreateSymbolProcessor(childSymbol).Process(childSymbol, policyConfig, violationReporter);
     }
 }
 private void VisitMembers(INamespaceOrTypeSymbol symbol)
 {
     foreach (var member in symbol.GetMembers())
     {
         member.Accept(this);
     }
 }
        private static IEnumerable <ITypeSymbol> GetAwaitableTypes(INamespaceOrTypeSymbol namespaceOrTypeSymbol)
        {
            if (namespaceOrTypeSymbol == null || !namespaceOrTypeSymbol.DeclaredAccessibility.HasFlag(Accessibility.Public))
            {
                yield break;
            }

            foreach (var member in namespaceOrTypeSymbol.GetMembers())
            {
                switch (member)
                {
                case INamespaceOrTypeSymbol nsOrType:
                    foreach (var nested in GetAwaitableTypes(nsOrType))
                    {
                        yield return(nested);
                    }

                    break;

                case IMethodSymbol method:
                    if (method.DeclaredAccessibility.HasFlag(Accessibility.Public) &&
                        method.IsExtensionMethod &&
                        method.Name == GetAwaiterMethodName &&
                        !method.Parameters.IsEmpty &&
                        ConformsToAwaiterPattern(method.ReturnType))
                    {
                        yield return(method.Parameters[0].Type);
                    }

                    break;
                }
            }
        }
Exemple #14
0
        // returns all the symbols in the container corresponding to the node
        private void Bind(int index, INamespaceOrTypeSymbol rootContainer, List <ISymbol> results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var node = _nodes[index];

            if (node.IsRoot)
            {
                return;
            }

            if (_nodes[node.ParentIndex].IsRoot)
            {
                results.AddRange(rootContainer.GetMembers(node.Name));
            }
            else
            {
                using (var containerSymbols = SharedPools.Default <List <ISymbol> >().GetPooledObject())
                {
                    Bind(node.ParentIndex, rootContainer, containerSymbols.Object, cancellationToken);

                    foreach (var containerSymbol in containerSymbols.Object.OfType <INamespaceOrTypeSymbol>())
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        results.AddRange(containerSymbol.GetMembers(node.Name));
                    }
                }
            }
        }
        private static IEnumerable <(ISymbol Original, string NewName)> GetSymbolsWithNewNames(INamespaceOrTypeSymbol containerSymbol, Compilation compilation)
        {
            var members    = containerSymbol.GetMembers().Where(m => m.Locations.Any(loc => compilation.ContainsSyntaxTree(loc.SourceTree))).ToArray();
            var symbolSets = GetLocalSymbolSets(containerSymbol, compilation, members).Concat(members.AsEnumerable().Yield());

            return(symbolSets.SelectMany(GetUniqueNamesForSymbolSet));
        }
        public static IEnumerable <INamespaceOrTypeSymbol> EnumAllNamespaces(this INamespaceOrTypeSymbol source)
        {
            yield return(source);

            foreach (var namespaceSymbolSub in source.GetMembers().OfType <INamespaceOrTypeSymbol>().SelectMany(EnumAllNamespaces))
            {
                yield return(namespaceSymbolSub);
            }
        }
Exemple #17
0
        private static IEnumerable <MappingInfo> AnalyzeModule(SemanticModel semanticModel,
                                                               INamespaceOrTypeSymbol moduleToAnalyze)
        {
            var loadMethods = moduleToAnalyze.GetMembers(ExternalLibraryInformation.Module.Load.Name)
                              .OfType <IMethodSymbol>()
                              .Where(loadMethod => loadMethod.Parameters.IsEmpty);

            return(AnalyzeLoadMethods(semanticModel, loadMethods));
        }
        internal static async Task <ImmutableArray <UnusedSymbolInfo> > FindUnusedSymbolsAsync(
            Project project,
            Compilation compilation,
            Func <ISymbol, bool> predicate         = null,
            IImmutableSet <ISymbol> ignoredSymbols = null,
            IImmutableSet <Document> documents     = null,
            CancellationToken cancellationToken    = default)
        {
            ImmutableArray <UnusedSymbolInfo> .Builder unusedSymbols = null;

            var namespaceOrTypeSymbols = new Stack <INamespaceOrTypeSymbol>();

            namespaceOrTypeSymbols.Push(compilation.Assembly.GlobalNamespace);

            while (namespaceOrTypeSymbols.Count > 0)
            {
                INamespaceOrTypeSymbol namespaceOrTypeSymbol = namespaceOrTypeSymbols.Pop();

                foreach (ISymbol symbol in namespaceOrTypeSymbol.GetMembers())
                {
                    bool isUnused = false;

                    if (symbol.Kind != SymbolKind.Namespace &&
                        !symbol.IsImplicitlyDeclared &&
                        !symbol.IsOverride &&
                        (predicate == null || predicate(symbol)) &&
                        IsAnalyzable(symbol) &&
                        ignoredSymbols?.Contains(symbol) != true)
                    {
                        isUnused = await IsUnusedSymbolAsync(symbol, project.Solution, documents, cancellationToken).ConfigureAwait(false);

                        if (isUnused)
                        {
                            string id = symbol.GetDocumentationCommentId();

                            WriteLine($"  {GetUnusedSymbolKind(symbol).ToString()} {symbol.ToDisplayString()}", ConsoleColor.Yellow, Verbosity.Normal);
                            WriteLine($"    {symbol.GetSyntax(cancellationToken).SyntaxTree.FilePath}", ConsoleColor.DarkGray, Verbosity.Detailed);
                            WriteLine($"    {id}", ConsoleColor.DarkGray, Verbosity.Diagnostic);

                            var unusedSymbolInfo = new UnusedSymbolInfo(symbol, id, project.Id);

                            (unusedSymbols ?? (unusedSymbols = ImmutableArray.CreateBuilder <UnusedSymbolInfo>())).Add(unusedSymbolInfo);
                        }
                    }

                    if (!isUnused &&
                        symbol is INamespaceOrTypeSymbol namespaceOrTypeSymbol2)
                    {
                        namespaceOrTypeSymbols.Push(namespaceOrTypeSymbol2);
                    }
                }
            }

            return(unusedSymbols?.ToImmutableArray() ?? ImmutableArray <UnusedSymbolInfo> .Empty);
        }
        private IEnumerable <ISymbol> GetMembers(ISymbol parent)
        {
            INamespaceOrTypeSymbol container = parent as INamespaceOrTypeSymbol;

            if (container != null)
            {
                return(container.GetMembers().AsEnumerable());
            }

            return(Enumerable.Empty <ISymbol>());
        }
Exemple #20
0
        private void GetDeclaredSymbols(INamespaceOrTypeSymbol container, List <ISymbol> symbols)
        {
            foreach (var member in container.GetMembers())
            {
                symbols.Add(member);

                if (member is INamespaceOrTypeSymbol nsOrType)
                {
                    GetDeclaredSymbols(nsOrType, symbols);
                }
            }
        }
            private static void GetMatchingNamespaces(INamespaceOrTypeSymbol container, string memberName, List <ISymbol> results)
            {
                var members = container.GetMembers(memberName);

                foreach (var symbol in members)
                {
                    if (symbol.Kind == SymbolKind.Namespace)
                    {
                        results.Add(symbol);
                    }
                }
            }
        public static IEnumerable <INamedTypeSymbol> GetNamedTypes(this INamespaceOrTypeSymbol sym)
        {
            if (sym is INamedTypeSymbol type)
            {
                yield return(type);
            }

            foreach (var child in sym.GetMembers().OfType <INamespaceOrTypeSymbol>().SelectMany(GetNamedTypes))
            {
                yield return(child);
            }
        }
Exemple #23
0
        private static IEnumerable <(ISymbol Original, string NewName)> GetSymbolsWithNewNames(INamespaceOrTypeSymbol containerSymbol)
        {
            var members = containerSymbol.GetMembers();
            //TODO If container is a type, call GetCsLocalSymbolDeclarations and rename those too - see test called CaseConflict_LocalWithLocal
            var membersByCaseInsensitiveName = members.ToLookup(m => m.Name, m => m, StringComparer.OrdinalIgnoreCase);
            var names = new HashSet <string>(membersByCaseInsensitiveName.Select(ms => ms.Key),
                                             StringComparer.OrdinalIgnoreCase);
            var symbolsWithNewNames = membersByCaseInsensitiveName.Where(ms => ms.Count() > 1)
                                      .SelectMany(symbolGroup => GetSymbolsWithNewNames(symbolGroup.ToArray(), names));

            return(symbolsWithNewNames);
        }
 private static IEnumerable <ISymbol> GetApiAffectingMembers(INamespaceOrTypeSymbol symbol)
 {
     if (symbol == null || NoMembersAffectApi(symbol))
     {
         return(Enumerable.Empty <ISymbol>());
     }
     return(from member in symbol.GetMembers()
            let methodKind = (member as IMethodSymbol)?.MethodKind
                             where methodKind != MethodKind.PropertyGet && methodKind != MethodKind.PropertySet
                             where methodKind != MethodKind.EventAdd && methodKind != MethodKind.EventRemove
                             where methodKind != MethodKind.Constructor || ((ITypeSymbol)symbol).TypeKind != TypeKind.Enum
                             select member);
 }
Exemple #25
0
        private static string GetMessage(INamespaceOrTypeSymbol namedType, SymbolAnalysisContext analysisContext)
        {
            var disposableFields = namedType.GetMembers()
                                   .OfType <IFieldSymbol>()
                                   .Where(fs => fs.IsNonStaticNonPublicDisposableField(analysisContext.Compilation.GetLanguageVersion()))
                                   .ToHashSet();

            if (disposableFields.Count == 0)
            {
                return(string.Empty);
            }

            var otherInitializationsOfFields = namedType.GetMembers()
                                               .OfType <IMethodSymbol>()
                                               .SelectMany(m => GetAssignmentsToFieldsIn(m, analysisContext.Compilation))
                                               .Where(f => disposableFields.Contains(f));

            return(string.Join(", ", disposableFields.Where(IsOwnerSinceDeclaration)
                               .Union(otherInitializationsOfFields)
                               .Distinct()
                               .Select(symbol => $"'{symbol.Name}'")
                               .OrderBy(name => name)));
        }
    private static IEnumerable <(ISymbol Original, string NewName)> GetSymbolsWithNewNames(INamespaceOrTypeSymbol containerSymbol, Compilation compilation)
    {
        if (containerSymbol.IsNamespace)
        {
            return(Enumerable.Empty <(ISymbol Original, string NewName)>());
        }

        var members = containerSymbol.GetMembers()
                      .Where(m => m.Locations.Any(loc => loc.SourceTree != null && compilation.ContainsSyntaxTree(loc.SourceTree)))
                      .Where(s => containerSymbol.Name == s.Name || containerSymbol is INamedTypeSymbol nt && nt.IsEnumType() && SymbolRenamer.GetName(s).StartsWith(containerSymbol.Name, StringComparison.InvariantCulture));
        var symbolSet = containerSymbol.Yield().Concat(members).ToArray();

        return(SymbolRenamer.GetSymbolsWithNewNames(symbolSet, new HashSet <string>(symbolSet.Select(SymbolRenamer.GetName)), true));
    }
Exemple #27
0
        private ITypeSymbol ResolveType(SemanticModel semanticModel, Type type)
        {
            string[] names = type.FullName.Split(new[] { '.', '+' }, StringSplitOptions.RemoveEmptyEntries);

            INamespaceOrTypeSymbol scope = null;

            for (int i = 0; i != names.Count(); ++i)
            {
                string metadataName         = names[i];
                string name                 = metadataName;
                int    index                = name.IndexOf('`');
                int    numberOfGenericTypes = 0;
                if (index != -1)
                {
                    string sNumber = name.Substring(index + 1);
                    if (!int.TryParse(sNumber, out numberOfGenericTypes))
                    {
                        return(null);
                    }
                    name = name.Substring(0, index);
                }

                IEnumerable <ISymbol> symbols;
                if (i == 0)
                {
                    symbols = semanticModel.LookupNamespacesAndTypes(0, scope, name);
                }
                else
                {
                    symbols = scope.GetMembers(name).Where(m => m.Kind == SymbolKind.Namespace || m.Kind == SymbolKind.NamedType);
                }

                if (numberOfGenericTypes != 0)
                {
                    symbols = symbols.Where(s => s.MetadataName == metadataName);
                }
                if (symbols.Count() == 1)
                {
                    scope = (INamespaceOrTypeSymbol)symbols.First();
                }
                else
                {
                    scope = null;
                    break;
                }
            }

            return((ITypeSymbol)scope);
        }
Exemple #28
0
    private void GetAllTypes(List <INamedTypeSymbol> items, INamespaceOrTypeSymbol symbol)
    {
        if (symbol is INamedTypeSymbol typeSymbol)
        {
            items.Add(typeSymbol);
        }

        foreach (var member in symbol.GetMembers())
        {
            if (member is INamespaceOrTypeSymbol child)
            {
                GetAllTypes(items, child);
            }
        }
    }
            static void doGetTypes(List <INamedTypeSymbol> list, INamespaceOrTypeSymbol symbol)
            {
                if (symbol is INamedTypeSymbol type && IsSerializable(type))
                {
                    list.Add(type);
                }

                foreach (ISymbol child in symbol.GetMembers())
                {
                    if (child is INamespaceOrTypeSymbol namespaceOrType)
                    {
                        doGetTypes(list, namespaceOrType);
                    }
                }
            }
Exemple #30
0
        private void GetAllTypes(List <INamedTypeSymbol> result, INamespaceOrTypeSymbol symbol)
        {
            if (symbol is INamedTypeSymbol type)
            {
                result.Add(type);
            }

            foreach (var child in symbol.GetMembers())
            {
                if (child is INamespaceOrTypeSymbol nsChild)
                {
                    GetAllTypes(result, nsChild);
                }
            }
        }
            private static void GetMatchingTypes(INamespaceOrTypeSymbol container, string memberName, int arity, List <ISymbol> results)
            {
                var members = container.GetMembers(memberName);

                foreach (var symbol in members)
                {
                    if (symbol.Kind == SymbolKind.NamedType)
                    {
                        var namedType = (INamedTypeSymbol)symbol;
                        if (namedType.Arity == arity)
                        {
                            results.Add(namedType);
                        }
                    }
                }
            }
Exemple #32
0
        // returns all the symbols in the container corresponding to the node
        private void Bind(
            int index, INamespaceOrTypeSymbol rootContainer, ArrayBuilder<ISymbol> results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var node = _nodes[index];
            if (node.IsRoot)
            {
                return;
            }

            if (_nodes[node.ParentIndex].IsRoot)
            {
                results.AddRange(rootContainer.GetMembers(GetName(node)));
            }
            else
            {
                var containerSymbols = ArrayBuilder<ISymbol>.GetInstance();
                try
                {
                    Bind(node.ParentIndex, rootContainer, containerSymbols, cancellationToken);

                    foreach (var containerSymbol in containerSymbols)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        var nsOrType = containerSymbol as INamespaceOrTypeSymbol;
                        if (nsOrType != null)
                        {
                            results.AddRange(nsOrType.GetMembers(GetName(node)));
                        }
                    }
                }
                finally
                {
                    containerSymbols.Free();
                }
            }
        }
Exemple #33
0
        // returns all the symbols in the container corresponding to the node
        private void Bind(int index, INamespaceOrTypeSymbol rootContainer, List<ISymbol> results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var node = _nodes[index];

            if (_nodes[node.ParentIndex].IsRoot)
            {
                results.AddRange(rootContainer.GetMembers(node.Name));
            }
            else
            {
                using (var containerSymbols = SharedPools.Default<List<ISymbol>>().GetPooledObject())
                {
                    Bind(node.ParentIndex, rootContainer, containerSymbols.Object, cancellationToken);

                    foreach (var containerSymbol in containerSymbols.Object.OfType<INamespaceOrTypeSymbol>())
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        results.AddRange(containerSymbol.GetMembers(node.Name));
                    }
                }
            }
        }
Exemple #34
0
 private void dfs_through_members_build_name(String name, INamespaceOrTypeSymbol symbol)
 {
     if (symbol != null && symbol.GetMembers() != null)
         foreach (var el in symbol.GetMembers())
         {
             var cur = el as INamespaceOrTypeSymbol;
             if (cur != null)
             {
                 var cur_name = name + "." + cur.Name.ToString();
                 current_names.Add(cur_name);
                 dfs_through_members_build_name(cur_name, cur);
             }
         }
 }
Exemple #35
0
        // returns all the symbols in the container corresponding to the node
        private void Bind(int index, INamespaceOrTypeSymbol rootContainer, List<ISymbol> results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var node = this.nodes[index];

            if (this.nodes[node.ParentIndex].IsRoot)
            {
                results.AddRange(rootContainer.GetMembers(node.Name));
            }
            else
            {
                var containerSymbols = SharedPools.Default<List<ISymbol>>().AllocateAndClear();
                try
                {
                    Bind(node.ParentIndex, rootContainer, containerSymbols, cancellationToken);

                    foreach (var containerSymbol in containerSymbols.OfType<INamespaceOrTypeSymbol>())
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        results.AddRange(containerSymbol.GetMembers(node.Name));
                    }
                }
                finally
                {
                    SharedPools.Default<List<ISymbol>>().ClearAndFree(containerSymbols);
                }
            }
        }
Exemple #36
0
        private static void dfs(Solution solution, Compilation compilation,  INamespaceOrTypeSymbol v)
        {
            if (v == null) return;
            foreach (var cur in v.GetMembers())
            {
                var to = cur as INamespaceOrTypeSymbol;
                if (to != null)
                {
                    var toAsType = to as ITypeSymbol;
                    if (toAsType != null)
                    {
                        if (toAsType.BaseType != null)
                        {
                            if (!_cacheTypesHierarchy[compilation].ContainsKey(toAsType.BaseType))
                                _cacheTypesHierarchy[compilation].Add(toAsType.BaseType, new HashSet<ITypeSymbol>());

                            _cacheTypesHierarchy[compilation][toAsType.BaseType].Add(toAsType);
                        }
                    }
                    dfs(solution, compilation,  to);
                }

                var asMeth = cur as IMethodSymbol;
                //if (asMeth != null)
                //{
                //    if (asMeth.PartialImplementationPart != null)
                //        asMeth = asMeth.PartialImplementationPart; // if partial method => work with implementation

                //    var declarings = asMeth.DeclaringSyntaxReferences;
                //    if (declarings != null && declarings.Length == 1)// it is a method, so it could not be implemented in two places.
                //        foreach (var invocationExpression in
                //                 declarings
                //                 .First()
                //                 .GetSyntax()
                //                 .DescendantNodes()
                //                 .OfType<InvocationExpressionSyntax>())
                //            foreach (var model in models)
                //            {
                //                try
                //                {
                //                    //var t1 = invocationExpression.Parent;
                //                    //var t2 = invocationExpression.Parent.Parent;
                //                    //var t3 = invocationExpression.Parent.Parent.Parent;
                //                    //var t4 = invocationExpression.Parent.Parent.Parent.Parent;

                //                    SymbolInfo symbolInfo = model.GetSymbolInfo(invocationExpression);
                //                    var calledMethod = (IMethodSymbol)symbolInfo.Symbol;

                //                    if (!_cacheMethodsCoupling[compilation].ContainsKey(asMeth))
                //                        _cacheMethodsCoupling[compilation].Add(asMeth, new HashSet<ISymbol>());

                //                    _cacheMethodsCoupling[compilation][asMeth].Add(calledMethod);

                //                    break;
                //                }
                //                catch
                //                { }
                //            }

                //}

                var asPro = cur as IPropertySymbol;
                var MethodList = new List<IMethodSymbol> { asMeth };
                //we do not need analyse property, because its methods already is members to see
                //if (asPro != null) MethodList = new List<IMethodSymbol> { asPro.GetMethod, asPro.SetMethod};

                foreach (var meth in MethodList)
                {
                    if (meth != null)
                    {
                        //TODO : make it to parallel processes
                        var too = SymbolFinder.FindCallersAsync(meth, solution);
                        too.Wait();
                        var CallingMethods = new List<IMethodSymbol>();
                        foreach (var el in too.Result)
                        {
                            var method = el.CallingSymbol as IMethodSymbol;
                            if (method != null) CallingMethods.Add(method);
                            var property = el.CallingSymbol as IPropertySymbol;
                            if (property != null)
                            {
                                if (property.GetMethod != null) CallingMethods.Add(property.GetMethod);
                                if (property.SetMethod != null) CallingMethods.Add(property.SetMethod);
                            }
                        }

                        foreach (var el in CallingMethods)
                        {
                            if (el.ContainingType != meth.ContainingType)
                            {
                                if (!_cacheMethodsCoupling[compilation].ContainsKey(el))
                                    _cacheMethodsCoupling[compilation].Add(el, new HashSet<ISymbol>());

                                _cacheMethodsCoupling[compilation][el].Add(meth);
                            }
                        }

                    }
                }

                ///////////////////////////////////////////////////////////////
            }
        }
Exemple #37
0
        private void GetDeclaredSymbols(INamespaceOrTypeSymbol container, List<ISymbol> symbols)
        {
            foreach (var member in container.GetMembers())
            {
                symbols.Add(member);

                var nsOrType = member as INamespaceOrTypeSymbol;
                if (nsOrType != null)
                {
                    GetDeclaredSymbols(nsOrType, symbols);
                }
            }
        }
		private void Visit(INamespaceOrTypeSymbol type)
		{
			foreach (var method in type.GetMembers().OfType<IMethodSymbol>())
			{
				Visit(method);
			}
		}
Exemple #39
0
        IEnumerable<ISymbol> GetAllSymbols(INamespaceOrTypeSymbol root) {
            foreach(var m in root.GetMembers()) {
                yield return m;

                var r2 = m as INamespaceOrTypeSymbol;
                if(r2 != null)
                    foreach(var m2 in GetAllSymbols(r2))
                        yield return m2;
            }
        }