Example #1
0
        /// <summary>
        /// The result from this call is meant to be used in a using.
        /// </summary>
        internal static PooledSet <T> BorrowOrIncrementUsage(PooledSet <T> set)
        {
            if (set == null)
            {
                return(Borrow());
            }

            var current = Interlocked.Increment(ref set.refCount);

            Debug.Assert(current >= 1, $"{nameof(BorrowOrIncrementUsage)} set.refCount == {current}");
            return(set);
        }
        private void HandleNonFlagEnumMember(SyntaxNodeAnalysisContext context, EnumDeclarationSyntax enumDeclaration)
        {
            using (var enumValuesSet = PooledSet <ulong> .Borrow())
            {
                foreach (var enumMember in enumDeclaration.Members)
                {
                    var  symbol = context.SemanticModel.GetDeclaredSymbol(enumMember, context.CancellationToken);
                    bool notDerivedFromOther =
                        !IsDerivedFromOtherEnumMembers(enumMember, context.SemanticModel, context.CancellationToken);
                    var value = UnboxUMaxInt(symbol.ConstantValue);
                    if (notDerivedFromOther && enumValuesSet.Contains(value))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, enumMember.GetLocation()));
                    }

                    enumValuesSet.Add(value);
                }
            }
        }
Example #3
0
        internal static bool FirstWith(ISymbol symbol, SyntaxNode scope, Search search, SemanticModel semanticModel, CancellationToken cancellationToken, out AssignmentExpressionSyntax assignment, PooledSet <ISymbol> recursion = null)
        {
            assignment = null;
            if (symbol == null ||
                scope == null)
            {
                return(false);
            }

            using (var walker = Borrow(scope, Search.TopLevel, semanticModel, cancellationToken))
            {
                foreach (var candidate in walker.assignments)
                {
                    if (IsMatch(symbol, candidate.Right, semanticModel, cancellationToken))
                    {
                        assignment = candidate;
                        return(true);
                    }
                }

                foreach (var declaration in walker.localDeclarations)
                {
                    if (declaration.Declaration is VariableDeclarationSyntax variableDeclaration &&
                        variableDeclaration.Variables.TryFirst(x => x.Initializer != null, out var variable) &&
                        IsMatch(symbol, variable.Initializer.Value, semanticModel, cancellationToken) &&
                        semanticModel.GetDeclaredSymbolSafe(variable, cancellationToken) is ILocalSymbol local)
                    {
                        using (var visited = recursion.IncrementUsage())
                        {
                            if (visited.Add(local) &&
                                FirstWith(local, scope, search, semanticModel, cancellationToken, out assignment, visited))
                            {
                                return(true);
                            }
                        }
                    }
                }

                if (search != Search.TopLevel)
                {
                    foreach (var argument in walker.arguments)
                    {
                        if (argument.Expression is IdentifierNameSyntax identifierName &&
                            identifierName.Identifier.ValueText == symbol.Name &&
                            semanticModel.GetSymbolSafe(argument.Parent?.Parent, cancellationToken) is IMethodSymbol method &&
                            method.TrySingleDeclaration(cancellationToken, out var methodDeclaration) &&
                            method.TryGetMatchingParameter(argument, out var parameter))
                        {
                            using (var visited = recursion.IncrementUsage())
                            {
                                if (visited.Add(parameter) &&
                                    FirstWith(parameter, methodDeclaration, search, semanticModel, cancellationToken, out assignment, visited))
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
 public PooledSetDebugView(PooledSet <T> set)
 {
     this.set = set ?? throw new ArgumentNullException(nameof(set));
 }