Skip to content

Commit

Permalink
fix(net9): Add linker directives
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Nov 20, 2024
1 parent 58f8233 commit 4595bcb
Show file tree
Hide file tree
Showing 42 changed files with 332 additions and 48 deletions.
85 changes: 85 additions & 0 deletions src/SourceGenerators/System.Xaml/Assembly/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Suppresses reporting of a specific rule violation, allowing multiple suppressions on a
/// single code artifact.
/// </summary>
/// <remarks>
/// <see cref="UnconditionalSuppressMessageAttribute"/> is different than
/// <see cref="SuppressMessageAttribute"/> in that it doesn't have a
/// <see cref="ConditionalAttribute"/>. So it is always preserved in the compiled assembly.
/// </remarks>
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
internal sealed class UnconditionalSuppressMessageAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="UnconditionalSuppressMessageAttribute"/>
/// class, specifying the category of the tool and the identifier for an analysis rule.
/// </summary>
/// <param name="category">The category for the attribute.</param>
/// <param name="checkId">The identifier of the analysis rule the attribute applies to.</param>
public UnconditionalSuppressMessageAttribute(string category, string checkId)
{
Category = category;
CheckId = checkId;
}

/// <summary>
/// Gets the category identifying the classification of the attribute.
/// </summary>
/// <remarks>
/// The <see cref="Category"/> property describes the tool or tool analysis category
/// for which a message suppression attribute applies.
/// </remarks>
public string Category { get; }

/// <summary>
/// Gets the identifier of the analysis tool rule to be suppressed.
/// </summary>
/// <remarks>
/// Concatenated together, the <see cref="Category"/> and <see cref="CheckId"/>
/// properties form a unique check identifier.
/// </remarks>
public string CheckId { get; }

/// <summary>
/// Gets or sets the scope of the code that is relevant for the attribute.
/// </summary>
/// <remarks>
/// The Scope property is an optional argument that specifies the metadata scope for which
/// the attribute is relevant.
/// </remarks>
public string Scope { get; set; }

/// <summary>
/// Gets or sets a fully qualified path that represents the target of the attribute.
/// </summary>
/// <remarks>
/// The <see cref="Target"/> property is an optional argument identifying the analysis target
/// of the attribute. An example value is "System.IO.Stream.ctor():System.Void".
/// Because it is fully qualified, it can be long, particularly for targets such as parameters.
/// The analysis tool user interface should be capable of automatically formatting the parameter.
/// </remarks>
public string Target { get; set; }

/// <summary>
/// Gets or sets an optional argument expanding on exclusion criteria.
/// </summary>
/// <remarks>
/// The <see cref="MessageId "/> property is an optional argument that specifies additional
/// exclusion where the literal metadata target is not sufficiently precise. For example,
/// the <see cref="UnconditionalSuppressMessageAttribute"/> cannot be applied within a method,
/// and it may be desirable to suppress a violation against a statement in the method that will
/// give a rule violation, but not against all statements in the method.
/// </remarks>
public string MessageId { get; set; }

/// <summary>
/// Gets or sets the justification for suppressing the code analysis message.
/// </summary>
public string Justification { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
//
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;

namespace Uno.Xaml.Schema
{
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Types manipulated here have been marked earlier")]
public class XamlTypeName
{
public static XamlTypeName Parse (string typeName, IXamlNamespaceResolver namespaceResolver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand All @@ -32,6 +33,7 @@

namespace Uno.Xaml
{
[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Types manipulated here have been marked earlier")]
static class TypeExtensionMethods
{
#region inheritance search and custom attribute provision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand All @@ -43,6 +44,8 @@ namespace Uno.Xaml
// it registers AssemblyLoaded event on CurrentDomain when it should
// reflect dynamic in-scope asemblies.
// It should be released at finalizer.
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Types manipulated here have been marked earlier")]
[UnconditionalSuppressMessage("Trimming", "IL2055", Justification = "Types manipulated here have been marked earlier")]
public class XamlSchemaContext
{
private static readonly char[] _semicolonArray = new char[] { ';' };
Expand Down
2 changes: 2 additions & 0 deletions src/SourceGenerators/System.Xaml/System.Xaml/XamlType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
using Uno.Xaml.Schema;
using System.Xml.Serialization;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;

namespace Uno.Xaml
{
[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "Types manipulated here have been marked earlier")]
public class XamlType : IEquatable<XamlType>
{
public XamlType (Type underlyingType, XamlSchemaContext schemaContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ where field.IsStatic
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1502:AvoidExcessiveComplexity\", Justification=\"Must be ignored even if generated code is checked.\")]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1506:AvoidExcessiveClassCoupling\", Justification = \"Must be ignored even if generated code is checked.\")]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1505:AvoidUnmaintainableCode\", Justification = \"Must be ignored even if generated code is checked.\")]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026\")]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2111\")]");
using (writer.BlockInvariant("internal static global::Uno.UI.DataBinding.IBindableType Build()"))
{
writer.AppendLineIndented("return Build(null);");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ private void BuildChildSubclasses(IIndentedStringBuilder writer, bool isTopLevel

WriteMetadataNewTypeAttribute(writer);
writer.AppendLineIndented("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026\")]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2111\")]");
using (writer.BlockInvariant($"{classAccessibility} class {className} {hrInterfaceImpl}"))
{
BuildBaseUri(writer);
Expand Down Expand Up @@ -1317,6 +1319,10 @@ private void BuildTopLevelResourceDictionary(IIndentedStringBuilder writer, Xaml
{
using (writer.BlockInvariant("namespace {0}", _defaultNamespace))
{

writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026\")]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2111\")]");

using (writer.BlockInvariant("public sealed partial class GlobalStaticResources"))
{
BuildBaseUri(writer);
Expand Down Expand Up @@ -1595,6 +1601,7 @@ private bool BuildDefaultStylesRegistration(IIndentedStringBuilder writer, XamlM

writer.AppendLine();

writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2075\")]");
using (writer.BlockInvariant("public static void RegisterDefaultStyles_{0}()", _fileUniqueId))
{
if (_isHotReloadEnabled)
Expand Down Expand Up @@ -1782,6 +1789,8 @@ private void BuildSingleTimeInitializer(IIndentedStringBuilder writer, string in
TryAnnotateWithGeneratorSource(writer);
using (ResourceOwnerScope())
{
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026\")]");
writer.AppendLineIndented("[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2111\")]");
writer.AppendLineIndented($"private object {initializerName}(object {CurrentResourceOwner}) =>");
using (writer.Indent())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading;
Expand All @@ -11,9 +12,12 @@ namespace Uno.Foundation.Interop
/// <summary>
/// Provider of <see cref="IJSObjectMetadata"/>
/// </summary>
[UnconditionalSuppressMessage("Trimming", "IL2067", Justification = "Types manipulated here have been marked earlier")]
[UnconditionalSuppressMessage("Trimming", "IL2041", Justification = "Types manipulated here have been marked earlier")]
internal static class JSObjectMetadataProvider
{
private static readonly Func<Type, IJSObjectMetadata> _getByReflection = t => new ReflectionMetadata(t);
private static readonly Func<Type, IJSObjectMetadata> _getByReflection = (t) => new ReflectionMetadata(t);

static JSObjectMetadataProvider() => _getByReflection = _getByReflection.AsMemoized();

/// <summary>
Expand All @@ -26,7 +30,9 @@ public static IJSObjectMetadata Get(Type type)

private class ReflectionMetadata : IJSObjectMetadata
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)]
private readonly Type _type;

private static long _handles;

private bool _isPrototypeExported;
Expand All @@ -35,7 +41,8 @@ private class ReflectionMetadata : IJSObjectMetadata
private static readonly char[] _parametersTrimArray = new char[] { '{', '}', ' ' };
private static readonly char[] _doubleQuoteSpaceArray = new[] { '"', ' ' };

public ReflectionMetadata(Type type)
public ReflectionMetadata(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)] Type type)
{
_type = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
using Uno.Foundation;
Expand Down Expand Up @@ -164,13 +165,15 @@ public static StructPtr AllocateStructure(Type type)
return new StructPtr(Marshal.AllocHGlobal(size), type);
}

public static T UnmarshalStructure<T>(AutoPtr ptr)
public static T UnmarshalStructure<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(AutoPtr ptr)
where T : struct
{
return Marshal.PtrToStructure<T>(ptr);
}

public static T UnmarshalStructure<T>(StructPtr ptr)
public static T UnmarshalStructure<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(StructPtr ptr)
where T : struct
{
return Marshal.PtrToStructure<T>(ptr);
Expand Down
12 changes: 11 additions & 1 deletion src/Uno.Foundation/Metadata/ApiInformation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Uno.Foundation.Logging;

Expand Down Expand Up @@ -50,9 +51,11 @@ public static bool IsTypePresent(string typeName)
}
}

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "GetMethod may return null, normal flow of operation")]
internal static bool IsMethodPresent(Type type, string methodName)
=> IsImplementedByUno(type?.GetMethod(methodName));

[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "GetField may return null, normal flow of operation")]
public static bool IsMethodPresent(string typeName, string methodName)
=> IsImplementedByUno(
GetValidType(typeName)
Expand All @@ -72,9 +75,11 @@ public static bool IsEventPresent(string typeName, string eventName)
GetValidType(typeName)
?.GetEvent(eventName));

[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "GetField may return null, normal flow of operation")]
internal static bool IsPropertyPresent(Type type, string methodName)
=> IsImplementedByUno(type?.GetProperty(methodName));

[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "GetProperty may return null, normal flow of operation")]
public static bool IsPropertyPresent(string typeName, string propertyName)
=> IsImplementedByUno(
GetValidType(typeName)
Expand Down Expand Up @@ -106,7 +111,11 @@ public static bool IsWriteablePropertyPresent(string typeName, string propertyNa
return false;
}

public static bool IsEnumNamedValuePresent(string enumTypeName, string valueName)
[UnconditionalSuppressMessage("Trimming", "IL2057", Justification = "GetField may return null, normal flow of operation")]
[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "GetField may return null, normal flow of operation")]
public static bool IsEnumNamedValuePresent(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] string enumTypeName,
string valueName)
=> GetValidType(enumTypeName)?.GetField(valueName) != null;

/// <summary>
Expand All @@ -124,6 +133,7 @@ public static bool IsEnumNamedValuePresent(string enumTypeName, string valueName
/// </summary>
public static LogLevel NotImplementedLogLevel { get; set; } = LogLevel.Debug;

[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Types may be removed or not present as part of the normal operations of that method")]
private static Type? GetValidType(string typeName)
{
lock (_assemblies)
Expand Down
7 changes: 7 additions & 0 deletions src/Uno.Foundation/SizeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;

Expand All @@ -10,6 +11,9 @@ internal class SizeConverter : TypeConverter
{
#if NETSTANDARD
private static readonly char[] _commaArray = new[] { ',' };
#endif
#if !NETSTANDARD
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Size))]
#endif
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
Expand All @@ -23,6 +27,9 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
return base.CanConvertFrom(context, sourceType);
}

#if !NETSTANDARD
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Size))]
#endif
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var stringValue = value as string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// ******************************************************************
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -60,7 +61,12 @@ public static bool IsNullable(this Type type)
/// <param name="type"></param>
/// <param name="genericTypeDefinition">The generic type without generic type argument(s).</param>
/// <returns></returns>
public static bool IsGenericDescentOf(this Type type, Type genericTypeDefinition)
public static bool IsGenericDescentOf(
#if NET9_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
#endif
this Type type,
Type genericTypeDefinition)
{
if (!genericTypeDefinition.IsGenericTypeDefinition)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/DataBinding/BindingExpression.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -840,6 +841,7 @@ private object ConvertValue(object value)
}
}

[UnconditionalSuppressMessage("Trimming", "IL2077", Justification = "Types manipulated here have been marked earlier")]
private object ConvertToBoundPropertyType(object value)
{
// _boundPropertyType can be null for properties not bound for the actual instance (no matching properties found)
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UI/DataBinding/BindingPropertyHelper.FastConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using System.Text.RegularExpressions;
using Microsoft.UI.Text;
using FontWeight = Windows.UI.Text.FontWeight;
using System.Diagnostics.CodeAnalysis;


#if __ANDROID__
using View = Android.Views.View;
Expand Down Expand Up @@ -962,6 +964,7 @@ private static bool FastStringToUriConvert(Type outputType, string input, ref ob
}
}

[UnconditionalSuppressMessage("Trimming", "IL2057", Justification = "GetType may return null, normal flow of operation")]
private static bool FastStringToTypeConvert(Type outputType, string input, ref object output)
{
if (outputType == typeof(Type))
Expand Down
Loading

0 comments on commit 4595bcb

Please sign in to comment.