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); } } } }
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); }
public static bool IsRefCounterType(ISymbol symbolInfo) { bool isRefCounter = false; if (symbolInfo is ITypeSymbol typeInfo) { if (RefCounter.IsAssignableTo(typeInfo)) { isRefCounter = true; } } return(isRefCounter); }
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); } } }
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); } } }
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); } } } }