コード例 #1
0
        private static void HandleMethodParameter(SyntaxNodeAnalysisContext context, ParameterSyntax parameter)
        {
            var paramSympol = context.SemanticModel.GetDeclaredSymbol(parameter, context.CancellationToken);

            if (paramSympol != null)
            {
                var isRefCounter = RefCounter.IsRefCounterType(paramSympol.Type);
                if (isRefCounter)
                {
                    var param = context.SemanticModel.GetDeclaredSymbolSafe(parameter, context.CancellationToken);

                    if (param != null)
                    {
                        var method = parameter.FirstAncestorOrSelf <MethodDeclarationSyntax>();
                        var block  = method?.Body;
                        if (block == null)
                        {
                            return;
                        }

                        RefCounterStatus status = new RefCounterStatus();
                        if (method.Identifier.ToString().Equals(KnownSymbol.ReleaseReference))
                        {
                            status.AcquireReference("param of ReleaseReference", method.GetLocation());
                        }
                        ProcessLocalOrParamenterVar(context, param, block, status);
                    }
                }
            }
        }
コード例 #2
0
ファイル: TestFixture.cs プロジェクト: shawn11ZX/ObjectPool
        internal static bool IsAssignedAndDisposedInSetupAndTearDown(ISymbol fieldOrProperty, TypeDeclarationSyntax scope, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (fieldOrProperty == null)
            {
                return(false);
            }

            if (AssignmentWalker.SingleForSymbol(fieldOrProperty, scope, Search.TopLevel, semanticModel, cancellationToken, out var assignment) &&
                assignment.FirstAncestor <MethodDeclarationSyntax>() is MethodDeclarationSyntax methodDeclaration)
            {
                if (Attribute.TryGetAttribute(methodDeclaration, KnownSymbol.NUnitSetUpAttribute, semanticModel, cancellationToken, out _))
                {
                    if (fieldOrProperty.ContainingType.TryFirstMethodRecursive(
                            x => x.GetAttributes().Any(a => a.AttributeClass == KnownSymbol.NUnitTearDownAttribute),
                            out var tearDown))
                    {
                        return(RefCounter.IsMemberDisposed(fieldOrProperty, tearDown, semanticModel, cancellationToken));
                    }
                }

                if (Attribute.TryGetAttribute(methodDeclaration, KnownSymbol.NUnitOneTimeSetUpAttribute, semanticModel, cancellationToken, out _))
                {
                    if (fieldOrProperty.ContainingType.TryFirstMethodRecursive(
                            x => x.GetAttributes().Any(a => a.AttributeClass == KnownSymbol.NUnitOneTimeTearDownAttribute),
                            out var tearDown))
                    {
                        return(RefCounter.IsMemberDisposed(fieldOrProperty, tearDown, semanticModel, cancellationToken));
                    }
                }
            }

            return(false);
        }
コード例 #3
0
ファイル: RefCounter.cs プロジェクト: shawn11ZX/ObjectPool
        public static bool IsRefCounterType(ISymbol symbolInfo)
        {
            bool isRefCounter = false;

            if (symbolInfo is ITypeSymbol typeInfo)
            {
                if (RefCounter.IsAssignableTo(typeInfo))
                {
                    isRefCounter = true;
                }
            }
            return(isRefCounter);
        }
コード例 #4
0
        private static void HandleCreation(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax invocation)
        {
            var semanticModle = context.SemanticModel;
            var canel         = context.CancellationToken;
            var symbolInfo    = semanticModle.GetSymbolInfo(invocation.Type, canel);

            if (symbolInfo.Symbol is ITypeSymbol typeSymbol)
            {
                if (RefCounter.IsAssignableTo(typeSymbol))
                {
                    ProcessCreation(invocation, context);
                }
            }
        }
コード例 #5
0
        private static void HandleLocalDeclaration(SyntaxNodeAnalysisContext context,
                                                   LocalDeclarationStatementSyntax localDeclaration)
        {
            // 根据声明语句声明的Type(可能是var),获得SymbolInfo

            var isRefCounter = RefCounter.IsRefCounterType(context.SemanticModel, localDeclaration.Declaration.Type);

            if (isRefCounter)
            {
                foreach (var variable in localDeclaration.Declaration.Variables)
                {
                    HandleLocalVariable(context, variable);
                }
            }
        }
コード例 #6
0
        private static void HandleInvocation(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation)
        {
            var semanticModle = context.SemanticModel;
            var canel         = context.CancellationToken;
            var symbolInfo    = semanticModle.GetSymbolInfo(invocation.Expression, canel);

            if (symbolInfo.Symbol is IMethodSymbol methodSymbol)
            {
                if (RefCounter.IsAssignableTo(methodSymbol.ReturnType))
                {
                    if (!AssignmentUtils.IsGetMethod(invocation, context.SemanticModel, context.CancellationToken))
                    {
                        ProcessCreation(invocation, context);
                    }
                }
            }
        }