From 9538bee0fc1fb24993217071e954d9597385485f Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 7 Jan 2021 07:59:11 -0800 Subject: [PATCH] EventSource linker warnings fix (#46510) * eh fix * First set of changes for EventSource trim warnings This is WIP that fixes EventSource.cs warnings with other fixes to come * incorporating fb * incorporating feedback * Missed removing annotation in the earlier checkin * Incorporating feedback * Incorporating feedback * Incorporating feedback * merging suppresion file with master * fix build break * removing a suppression for linker update --- .../src/ILLink/ILLink.Suppressions.Shared.xml | 18 ------------------ .../src/System/Diagnostics/Tracing/EventSource.cs | 22 ++++++++++++++++------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml index 44dee73..fb533a5 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml @@ -77,12 +77,6 @@ ILLink IL2070 member - M:System.Diagnostics.Tracing.ManifestBuilder.GetTypeName(System.Type) - - - ILLink - IL2070 - member M:System.Diagnostics.Tracing.NullableTypeInfo.#ctor(System.Type,System.Collections.Generic.List{System.Type}) @@ -123,18 +117,6 @@ ILLink - IL2075 - member - M:System.Diagnostics.Tracing.EventSource.CreateManifestAndDescriptors(System.Type,System.String,System.Diagnostics.Tracing.EventSource,System.Diagnostics.Tracing.EventManifestOptions) - - - ILLink - IL2075 - member - M:System.Diagnostics.Tracing.ManifestBuilder.CreateManifestString - - - ILLink IL2077 member M:Internal.Runtime.InteropServices.ComActivator.BasicClassFactory.CreateInstance(System.Object,System.Guid@,System.IntPtr@) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 9a96d44..c63d070 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -334,8 +334,7 @@ namespace System.Diagnostics.Tracing } #if !ES_BUILD_STANDALONE - private const DynamicallyAccessedMemberTypes ManifestMemberTypes = DynamicallyAccessedMemberTypes.PublicNestedTypes - | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods; + private const DynamicallyAccessedMemberTypes ManifestMemberTypes = DynamicallyAccessedMemberTypes.All; #endif /// @@ -2876,7 +2875,7 @@ namespace System.Diagnostics.Tracing // Use reflection to look at the attributes of a class, and generate a manifest for it (as UTF8) and // return the UTF8 bytes. It also sets up the code:EventData structures needed to dispatch events // at run time. 'source' is the event source to place the descriptors. If it is null, - // then the descriptors are not creaed, and just the manifest is generated. + // then the descriptors are not created, and just the manifest is generated. private static byte[]? CreateManifestAndDescriptors( #if !ES_BUILD_STANDALONE [DynamicallyAccessedMembers(ManifestMemberTypes)] @@ -5451,6 +5450,18 @@ namespace System.Diagnostics.Tracing } // Write out the maps + + // Scoping the call to enum GetFields to a local function to limit the linker suppression +#if !ES_BUILD_STANDALONE + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", + Justification = "Trimmer does not trim enums")] +#endif + static FieldInfo[] GetEnumFields(Type localEnumType) + { + Debug.Assert(localEnumType.IsEnum); + return localEnumType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static); + } + if (mapsTab != null) { sb.AppendLine(" "); @@ -5461,7 +5472,7 @@ namespace System.Diagnostics.Tracing sb.Append(" <").Append(mapKind).Append(" name=\"").Append(enumType.Name).AppendLine("\">"); // write out each enum value - FieldInfo[] staticFields = enumType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static); + FieldInfo[] staticFields = GetEnumFields(enumType); bool anyValuesWritten = false; foreach (FieldInfo staticField in staticFields) { @@ -5780,8 +5791,7 @@ namespace System.Diagnostics.Tracing { if (type.IsEnum) { - FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - string typeName = GetTypeName(fields[0].FieldType); + string typeName = GetTypeName(type.GetEnumUnderlyingType()); return typeName.Replace("win:Int", "win:UInt"); // ETW requires enums to be unsigned. } -- 2.7.4