Пример #1
0
        public static bool IsCompositeEnumValue(
            SyntaxNode node,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default)
        {
            var enumTypeSymbol = (INamedTypeSymbol)semanticModel.GetTypeSymbol(node, cancellationToken);

            Optional <object> constantValue = semanticModel.GetConstantValue(node, cancellationToken);

            ulong value = SymbolUtility.GetEnumValueAsUInt64(constantValue.Value, enumTypeSymbol);

            return(FlagsUtility <ulong> .Instance.IsComposite(value));
        }
Пример #2
0
        public static OneOrMany <EnumFieldSymbolInfo> GetConstituentFields(object value, INamedTypeSymbol enumType)
        {
            EnumSymbolInfo enumInfo = EnumSymbolInfo.Create(enumType);

            ulong convertedValue = SymbolUtility.GetEnumValueAsUInt64(value, enumType);

            if (!enumType.HasAttribute(MetadataNames.System_FlagsAttribute) ||
                convertedValue == 0)
            {
                return(OneOrMany.Create(FindField(enumInfo, convertedValue)));
            }

            return(GetConstituentFields(convertedValue, enumInfo));
        }
Пример #3
0
        public static bool TryCreate(IFieldSymbol fieldSymbol, out EnumFieldSymbolInfo fieldInfo)
        {
            if (fieldSymbol == null)
            {
                fieldInfo = default;
                return(false);
            }

            ulong value = (fieldSymbol.HasConstantValue)
                ? SymbolUtility.GetEnumValueAsUInt64(fieldSymbol.ConstantValue, fieldSymbol.ContainingType)
                : 0;

            fieldInfo = new EnumFieldSymbolInfo(fieldSymbol, value);

            return(true);
        }
Пример #4
0
        public static async Task <bool> IsUnusedSymbolAsync(
            ISymbol symbol,
            Solution solution,
            CancellationToken cancellationToken = default)
        {
            ImmutableArray <SyntaxReference> syntaxReferences = symbol.DeclaringSyntaxReferences;

            Debug.Assert(syntaxReferences.Any(), $"No syntax references for {symbol.ToDisplayString()}");

            if (!syntaxReferences.Any())
            {
                return(false);
            }

            if (IsReferencedInDebuggerDisplayAttribute(symbol))
            {
                return(false);
            }

            IEnumerable <ReferencedSymbol> referencedSymbols = await SymbolFinder.FindReferencesAsync(symbol, solution, cancellationToken).ConfigureAwait(false);

            foreach (ReferencedSymbol referencedSymbol in referencedSymbols)
            {
                foreach (ReferenceLocation referenceLocation in referencedSymbol.Locations)
                {
                    if (referenceLocation.IsImplicit)
                    {
                        continue;
                    }

                    if (referenceLocation.IsCandidateLocation)
                    {
                        return(false);
                    }

                    Location location = referenceLocation.Location;

                    if (!location.IsInSource)
                    {
                        continue;
                    }

                    foreach (SyntaxReference syntaxReference in syntaxReferences)
                    {
                        if (syntaxReference.SyntaxTree != location.SourceTree ||
                            !syntaxReference.Span.Contains(location.SourceSpan))
                        {
                            return(false);
                        }
                    }
                }
            }

            if (symbol.Kind == SymbolKind.Field)
            {
                INamedTypeSymbol containingType = symbol.ContainingType;

                if (containingType.TypeKind == TypeKind.Enum &&
                    containingType.HasAttribute(MetadataNames.System_FlagsAttribute))
                {
                    var fieldSymbol = (IFieldSymbol)symbol;

                    if (fieldSymbol.HasConstantValue)
                    {
                        ulong value = SymbolUtility.GetEnumValueAsUInt64(fieldSymbol.ConstantValue, containingType);

                        if (value == 0)
                        {
                            return(false);
                        }
                    }
                }
            }

            if (symbol.Kind == SymbolKind.NamedType)
            {
                var namedType = (INamedTypeSymbol)symbol;

                if (namedType.TypeKind.Is(TypeKind.Class, TypeKind.Struct))
                {
                    foreach (ISymbol member in namedType.GetMembers())
                    {
                        if (member.Kind == SymbolKind.Method)
                        {
                            var methodSymbol = (IMethodSymbol)member;

                            if (SymbolUtility.CanBeEntryPoint(methodSymbol))
                            {
                                return(false);
                            }
                        }
                    }
                }
            }

            return(true);
        }