Example #1
0
            public DelegateInteropUsageAnalyzer(Compilation compilation, AutoLayoutTypeCache autoLayoutCache)
            {
                if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeInteropServicesMarshal, out var marshalType))
                {
                    var getDelegateForFunctionPointerMethods = marshalType.GetMembers("GetDelegateForFunctionPointer").OfType <IMethodSymbol>();
                    _getDelegateForFunctionPointerNonGeneric = getDelegateForFunctionPointerMethods.FirstOrDefault(m => m.TypeArguments.Length == 0);
                    _getDelegateForFunctionPointerGeneric    = getDelegateForFunctionPointerMethods.FirstOrDefault(m => m.TypeArguments.Length != 0);
                    var getFunctionPointerForDelegateMethods = marshalType.GetMembers("GetFunctionPointerForDelegate").OfType <IMethodSymbol>();
                    _getFunctionPointerForDelegateNonGeneric = getFunctionPointerForDelegateMethods.FirstOrDefault(m => m.TypeArguments.Length == 0);
                    _getFunctionPointerForDelegateGeneric    = getFunctionPointerForDelegateMethods.FirstOrDefault(m => m.TypeArguments.Length != 0);
                }

                _autoLayoutCache = autoLayoutCache;
            }
 public DisabledRuntimeMarshallingAssemblyAnalyzer(Compilation compilation, AutoLayoutTypeCache autoLayoutCache)
 {
     _unmanagedFunctionPointerAttribute = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeInteropServicesUnmanagedFunctionPoitnerAttribute);
     _lcidConversionAttribute           = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeInteropServicesLCIDConversionAttribute);
     if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeInteropServicesMarshal, out var marshalType))
     {
         var marshalMethods = ImmutableArray.CreateBuilder <ISymbol>();
         marshalMethods.AddRange(marshalType.GetMembers("SizeOf"));
         marshalMethods.AddRange(marshalType.GetMembers("OffsetOf"));
         marshalMethods.AddRange(marshalType.GetMembers("StructureToPtr"));
         marshalMethods.AddRange(marshalType.GetMembers("PtrToStructure"));
         _marshalMethods = marshalMethods.ToImmutable();
     }
     _autoLayoutCache = autoLayoutCache;
 }
        private static void AnalyzeMethodSignature(AutoLayoutTypeCache autoLayoutCache, Action <Diagnostic> reportDiagnostic, IMethodSymbol method, ImmutableArray <Location> locationsOverride = default, DiagnosticDescriptor?descriptorOverride = null)
        {
            AnalyzeSignatureType(locationsOverride.IsDefaultOrEmpty ? method.Locations : locationsOverride, method.ReturnType);
            foreach (var param in method.Parameters)
            {
                var paramLocation = locationsOverride.IsDefaultOrEmpty ? param.Locations : locationsOverride;
                if (param.RefKind != RefKind.None)
                {
                    reportDiagnostic(paramLocation.CreateDiagnostic(descriptorOverride ?? FeatureUnsupportedWhenRuntimeMarshallingDisabledByRefParameters));
                }
                AnalyzeSignatureType(paramLocation, param.Type);
            }

            void AnalyzeSignatureType(ImmutableArray <Location> locations, ITypeSymbol type)
            {
                if (type.SpecialType == SpecialType.System_Void)
                {
                    return;
                }

                if (type.Language == LanguageNames.CSharp)
                {
                    if (!type.IsUnmanagedType)
                    {
                        reportDiagnostic(locations.CreateDiagnostic(descriptorOverride ?? FeatureUnsupportedWhenRuntimeMarshallingDisabledManagedParameterOrReturnTypes));
                    }
                }
                // For non-C# languages, we'll do a quick check to catch simple cases
                // since IsUnmanagedType only works in languages that support unmanaged types
                // and non-C# languages that might not support is (such as VB) aren't a big focus of the attribute
                // this analyzer validates.
                else if (type.IsReferenceType || type.GetMembers().Any(m => m is IFieldSymbol {
                    IsStatic: false, Type.IsReferenceType: true
                }))
                {
                    reportDiagnostic(locations.CreateDiagnostic(descriptorOverride ?? FeatureUnsupportedWhenRuntimeMarshallingDisabledManagedParameterOrReturnTypes));
                }

                if (type.IsValueType && autoLayoutCache.TypeIsAutoLayoutOrContainsAutoLayout(type))
                {
                    reportDiagnostic(locations.CreateDiagnostic(descriptorOverride ?? FeatureUnsupportedWhenRuntimeMarshallingDisabledAutoLayoutTypes));
                }
            }