예제 #1
0
 public static void ProcessReturnStatement(ISymbol local, BlockSyntax block, SemanticModel semanticModel, CancellationToken cancellationToken,
                                           RefCounterStatus status)
 {
     using (var walker = ReturnValueWalker.Borrow(block, Search.TopLevel, semanticModel, cancellationToken))
     {
         foreach (var value in walker)
         {
             var returnedSymbol = semanticModel.GetSymbolSafe(value, cancellationToken);
             if (SymbolComparer.Equals(local, returnedSymbol))
             {
                 var  method = block.FirstAncestor <MethodDeclarationSyntax>();
                 var  access = block.FirstAncestor <AccessorDeclarationSyntax>();
                 bool isGet  = false;
                 if (method != null)
                 {
                     isGet = KnownSymbol.IsGetMethodName(method.Identifier.ToString());
                 }
                 else if (access != null)
                 {
                     isGet = true;
                 }
                 if (isGet)
                 {
                     status.RemainRef("return value from get method", value.GetLocation());
                 }
                 else
                 {
                     status.ReleaseReference("return value from non get", value.GetLocation());
                 }
             }
         }
     }
 }
예제 #2
0
        public static int DelRefInvocationCount(ISymbol symbol, BlockSyntax block, SemanticModel semanticModel, CancellationToken cancellationToken, RefCounterStatus status)
        {
            int      count = 0;
            Location loc   = Location.None;;

            using (var pooledInvocations = InvocationWalker.Borrow(block))
            {
                foreach (var invocation in pooledInvocations.Invocations)
                {
                    var isFromSymbol = IsInvokeFrom(symbol, semanticModel, cancellationToken, invocation);
                    var method       = semanticModel.GetSymbolSafe(invocation, cancellationToken) as IMethodSymbol;
                    if (isFromSymbol && method?.ReceiverType != null)
                    {
                        if (method.ReceiverType.Is(KnownSymbol.RefCounter) &&
                            method?.Name == KnownSymbol.ReleaseReference)
                        {
                            count++;
                            loc = invocation.GetLocation();
                            if (LoopUtils.HasLoopBetween(invocation, block))
                            {
                                status.Skip("loop between ReleaseReference call and var block", invocation.GetLocation());
                            }
                        }
                    }
                    else
                    {
                        if (method?.Name == KnownSymbol.ReleaseReference)
                        {
                            foreach (ArgumentSyntax arg in invocation.ArgumentList.Arguments)
                            {
                                var     sym       = ModelExtensions.GetSymbolInfo(semanticModel, arg.Expression, cancellationToken);
                                ISymbol argSymbol = sym.Symbol;
                                if (symbol.Equals(argSymbol))
                                {
                                    count++;
                                    loc = invocation.GetLocation();

                                    if (LoopUtils.HasLoopBetween(invocation, block))
                                    {
                                        status.Skip("loop between ReleaseReference call and var block", arg.GetLocation());
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (count > 1)
            {
                status.Skip("more than one ReleaseReference", loc);
            }
            else if (count == 1)
            {
                status.ReleaseReference("call ReleaseReference", loc);
            }

            return(count);
        }
    public static void ProcessRightOfAssignmentToField(
        ISymbol variable,
        BlockSyntax block,
        SemanticModel semanticModel,
        CancellationToken cancellationToken,
        RefCounterStatus status)
    {
        List <AssignmentExpressionSyntax> rc = new List <AssignmentExpressionSyntax>();

        block?.TryGetAssignment(variable, semanticModel, cancellationToken, rc);
        int      count = 0;
        ISymbol  field = null;
        Location loc   = Location.None;

        foreach (AssignmentExpressionSyntax assignment in rc)
        {
            var classDef    = block.FirstAncestor <ClassDeclarationSyntax>();
            var classSymbol = CSharpExtensions.GetDeclaredSymbol(semanticModel, classDef, cancellationToken);

            if (IsRightOfAssignmentToField(semanticModel, cancellationToken, assignment.Left, out field))
            {
                count++;
                loc = assignment.GetLocation();
                if (!field.ContainingType.Equals(classSymbol))
                {
                    status.Skip("assigned to field/property of other class", loc);
                }

                if (LoopUtils.HasLoopBetween(assignment, block))
                {
                    status.Skip("loop between assignment to field/property and var block", loc);
                }
            }
        }

        if (count == 1)
        {
            status.ReleaseReference("assign to class field/property", loc);

            var methodBlock = block?.FirstAncestorOrSelf <BlockSyntax>();
            if (methodBlock == null)
            {
                return;
            }

            if (RightSideOfAssignmentCount(field, methodBlock, semanticModel, cancellationToken) > 0)
            {
                status.Skip("var assigned to field/property, which is assigned to others", loc);
            }


            ChangeReferenceMethodUtils.ProcessIncDelRefInvocation(field, methodBlock, semanticModel, cancellationToken, status);
            ReturnUtils.ProcessReturnStatement(field, methodBlock, semanticModel, cancellationToken, status);
        }
        else if (count > 1)
        {
            status.Skip("multiple assign to field/property", loc);
        }
    }
예제 #4
0
    public static void ProcessAddedToCollection(
        ISymbol symbol, BlockSyntax block, SemanticModel semanticModel, CancellationToken cancellationToken, RefCounterStatus status)
    {
        int      count = 0;
        Location loc   = Location.None;

        using (var pooledInvocations = InvocationWalker.Borrow(block))
        {
            foreach (var invocation in pooledInvocations.Invocations)
            {
                bool isAddToCollection = false;
                var  method            = semanticModel.GetSymbolSafe(invocation, cancellationToken) as IMethodSymbol;
                if (method?.ReceiverType != null)
                {
                    if (!isAddToCollection)
                    {
                        isAddToCollection = (method.ReceiverType.Is(KnownSymbol.IDictionary) || method.ReceiverType.Is(KnownSymbol.ICollection)) &&
                                            method?.Name == "Add";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.QueueOfT) &&
                                            method?.Name == "Enqueue";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.Queue) &&
                                            method?.Name == "Enqueue";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.ListOfT) &&
                                            method?.Name == "Insert";
                    }
                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.IRefCounterContainerOfT) &&
                                            method?.Name == "Add";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.CollectionOfT) &&
                                            method?.Name == "Add";
                    }
                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.LinkedListOfT) &&
                                            (method?.Name == "AddLast" || method?.Name == "AddFirst");
                    }
                }

                if (isAddToCollection)
                {
                    bool isInArg = false;
                    foreach (ArgumentSyntax arg in invocation.ArgumentList.Arguments)
                    {
                        var     sym       = ModelExtensions.GetSymbolInfo(semanticModel, arg.Expression, cancellationToken);
                        ISymbol argSymbol = sym.Symbol;
                        if (symbol.Equals(argSymbol))
                        {
                            isInArg = true;
                        }
                    }

                    if (isInArg)
                    {
                        count++;
                        loc = invocation.GetLocation();
                        if (LoopUtils.HasLoopBetween(invocation, block))
                        {
                            status.Skip("loop between add to collection and var block", invocation.GetLocation());
                        }
                    }
                }
            }
        }

        if (count == 1)
        {
            status.ReleaseReference("added to collection", loc);
        }
    }