示例#1
0
        private void Run()
        {
            if (this.CurrentSymbol == null)
            {
                return;
            }

            if (this.CurrentSymbol is ILocalSymbol local)
            {
                if (local.TrySingleDeclaration(this.cancellationToken, out SyntaxNode declaration))
                {
                    var scope = (SyntaxNode)declaration.FirstAncestorOrSelf <AnonymousFunctionExpressionSyntax>() ??
                                declaration.FirstAncestorOrSelf <MemberDeclarationSyntax>();
                    if (scope != null)
                    {
                        this.Visit(scope);
                    }
                }

                return;
            }

            if (this.CurrentSymbol is IParameterSymbol)
            {
                var scope = (SyntaxNode)this.context?.FirstAncestorOrSelf <AnonymousFunctionExpressionSyntax>() ??
                            this.context?.FirstAncestorOrSelf <MemberDeclarationSyntax>();
                if (scope != null)
                {
                    this.Visit(scope);
                }

                return;
            }

            if (this.CurrentSymbol is IFieldSymbol ||
                this.CurrentSymbol is IPropertySymbol)
            {
                var type = (INamedTypeSymbol)this.semanticModel.GetDeclaredSymbolSafe(this.context?.FirstAncestorOrSelf <TypeDeclarationSyntax>(), this.cancellationToken);
                if (type == null)
                {
                    return;
                }

                if (this.CurrentSymbol is IFieldSymbol &&
                    this.CurrentSymbol.TrySingleDeclaration(this.cancellationToken, out FieldDeclarationSyntax fieldDeclarationSyntax))
                {
                    this.Visit(fieldDeclarationSyntax);
                }
                else if (this.CurrentSymbol is IPropertySymbol &&
                         this.CurrentSymbol.TrySingleDeclaration(this.cancellationToken, out PropertyDeclarationSyntax propertyDeclaration) &&
                         propertyDeclaration.Initializer != null)
                {
                    this.values.Add(propertyDeclaration.Initializer.Value);
                }

                var contextCtor = this.context?.FirstAncestorOrSelf <ConstructorDeclarationSyntax>();
                foreach (var reference in type.DeclaringSyntaxReferences)
                {
                    using (var ctorWalker = ConstructorsWalker.Borrow((TypeDeclarationSyntax)reference.GetSyntax(this.cancellationToken), this.semanticModel, this.cancellationToken))
                    {
                        foreach (var creation in ctorWalker.ObjectCreations)
                        {
                            if (this.visitedLocations.Add(creation))
                            {
                                if (contextCtor == null ||
                                    creation.Creates(contextCtor, Search.Recursive, this.semanticModel, this.cancellationToken))
                                {
                                    this.VisitObjectCreationExpression(creation);
                                    var method = this.semanticModel.GetSymbolSafe(creation, this.cancellationToken);
                                    this.HandleInvoke(method, creation.ArgumentList);
                                }
                            }
                        }

                        if (contextCtor != null)
                        {
                            foreach (var initializer in ctorWalker.Initializers)
                            {
                                var other = (ConstructorDeclarationSyntax)initializer.Parent;
                                if (Constructor.IsRunBefore(contextCtor, other, this.semanticModel, this.cancellationToken))
                                {
                                    this.Visit(other);
                                }
                            }

                            if (!contextCtor.Modifiers.Any(SyntaxKind.PrivateKeyword))
                            {
                                this.Visit(contextCtor);
                            }

                            return;
                        }

                        if (ctorWalker.Default != null)
                        {
                            this.Visit(ctorWalker.Default);
                        }

                        foreach (var ctor in ctorWalker.NonPrivateCtors)
                        {
                            this.Visit(ctor);
                        }
                    }
                }

                if ((this.CurrentSymbol is IFieldSymbol field &&
                     !field.IsReadOnly) ||
                    (this.CurrentSymbol is IPropertySymbol property &&
                     !property.IsReadOnly))
                {
                    var scope = (SyntaxNode)this.context?.FirstAncestorOrSelf <AnonymousFunctionExpressionSyntax>() ??
                                this.context?.FirstAncestorOrSelf <MemberDeclarationSyntax>();
                    if (scope != null &&
                        !(scope is ConstructorDeclarationSyntax))
                    {
                        while (type.Is(this.CurrentSymbol.ContainingType))
                        {
                            foreach (var reference in type.DeclaringSyntaxReferences)
                            {
                                var typeDeclaration = (TypeDeclarationSyntax)reference.GetSyntax(this.cancellationToken);
                                this.memberWalker.Visit(typeDeclaration);
                            }

                            type = type.BaseType;
                        }
                    }
                }
            }
        }
示例#2
0
        private void Run()
        {
            if (this.CurrentSymbol == null)
            {
                return;
            }

            var type = (INamedTypeSymbol)this.semanticModel.GetDeclaredSymbolSafe(this.Context?.FirstAncestorOrSelf <TypeDeclarationSyntax>(), this.cancellationToken);

            if (type == null)
            {
                return;
            }

            if (this.CurrentSymbol is IFieldSymbol ||
                this.CurrentSymbol is IPropertySymbol)
            {
                if (this.CurrentSymbol is IFieldSymbol)
                {
                    foreach (var reference in this.CurrentSymbol.DeclaringSyntaxReferences)
                    {
                        var fieldDeclarationSyntax = reference.GetSyntax(this.cancellationToken)
                                                     ?.FirstAncestorOrSelf <FieldDeclarationSyntax>();
                        if (fieldDeclarationSyntax != null)
                        {
                            this.Visit(fieldDeclarationSyntax);
                        }
                    }
                }

                if (this.CurrentSymbol is IPropertySymbol)
                {
                    foreach (var reference in this.CurrentSymbol.DeclaringSyntaxReferences)
                    {
                        var propertyDeclarationSyntax = reference.GetSyntax(this.cancellationToken)
                                                        ?.FirstAncestorOrSelf <PropertyDeclarationSyntax>();
                        if (propertyDeclarationSyntax?.Initializer?.Value != null)
                        {
                            this.values.Add(propertyDeclarationSyntax.Initializer.Value);
                        }
                    }
                }

                var contextCtor = this.Context?.FirstAncestorOrSelf <ConstructorDeclarationSyntax>();
                foreach (var reference in type.DeclaringSyntaxReferences)
                {
                    using (var walker = ConstructorsWalker.Borrow((TypeDeclarationSyntax)reference.GetSyntax(this.cancellationToken), this.semanticModel, this.cancellationToken))
                    {
                        if (this.Context?.FirstAncestorOrSelf <ConstructorDeclarationSyntax>() == null &&
                            walker.Default != null)
                        {
                            this.Visit(walker.Default);
                        }

                        foreach (var creation in walker.ObjectCreations)
                        {
                            if (contextCtor == null ||
                                creation.Creates(contextCtor, Search.Recursive, this.semanticModel, this.cancellationToken))
                            {
                                if (this.visitedLocations.Add(creation))
                                {
                                    this.VisitObjectCreationExpression(creation);
                                    var method = this.semanticModel.GetSymbolSafe(creation, this.cancellationToken);
                                    this.HandleInvoke(method, creation.ArgumentList);
                                }
                            }
                        }

                        foreach (var ctor in walker.NonPrivateCtors)
                        {
                            if (contextCtor == null ||
                                ctor == contextCtor ||
                                contextCtor.IsRunBefore(ctor, this.semanticModel, this.cancellationToken))
                            {
                                this.Visit(ctor);
                            }
                        }
                    }
                }
            }

            var contextMember = this.Context?.FirstAncestorOrSelf <MemberDeclarationSyntax>();

            if (contextMember != null &&
                (this.CurrentSymbol is ILocalSymbol ||
                 this.CurrentSymbol is IParameterSymbol))
            {
                this.Visit(contextMember);
            }
            else if (!(contextMember is ConstructorDeclarationSyntax))
            {
                while (type.Is(this.CurrentSymbol.ContainingType))
                {
                    foreach (var reference in type.DeclaringSyntaxReferences)
                    {
                        var typeDeclaration = (TypeDeclarationSyntax)reference.GetSyntax(this.cancellationToken);
                        this.memberWalker.Visit(typeDeclaration);
                    }

                    type = type.BaseType;
                }
            }

            this.values.PurgeDuplicates();
        }