From 51281dd723afe347c62eb9075c613a867cefa3f1 Mon Sep 17 00:00:00 2001 From: Tlakollo Date: Tue, 24 Jan 2023 17:59:09 -0800 Subject: [PATCH 1/6] Initial commit of handling the star argument in LinkAttributes files for NativeAOT. Add testing infrastructure from illink --- .../Common/Compiler/ProcessLinkerXmlBase.cs | 4 +- .../Compiler/UsageBasedMetadataManager.cs | 41 ++++- .../AssemblyLevelLinkerAttributeRemoval.cs | 22 +++ .../AssemblyLevelLinkerAttributeRemoval.xml | 8 + ...AssemblyLevelLinkerAttributeRemoval_Lib.cs | 23 +++ .../EmbeddedAttributeErrorCases.cs | 6 + .../EmbeddedAttributeErrorCases.xml | 10 ++ ...ovalAndPreserveAssembly_Lib.Descriptor.xml | 4 + ...AttributeRemovalAndPreserveAssembly_Lib.cs | 10 ++ ...tributeRemovalAttributeFromCopyAssembly.cs | 22 +++ ...LinkerAttributeRemovalAttributeToRemove.cs | 32 ++++ .../LinkerAttributeRemovalCopyAssembly.cs | 20 +++ ...nkerAttributeRemovalEmbeddedAndLazyLoad.cs | 18 ++ .../ReferencedAssemblyWithAttributes.cs | 12 ++ .../Dependencies/TestRemoveAttribute.xml | 7 + .../TestRemoveDontRemoveAttributes.cs | 18 ++ .../LinkAttributes/EmbeddedLinkAttributes.cs | 49 ++++++ .../LinkAttributes/EmbeddedLinkAttributes.xml | 18 ++ ...eddedLinkAttributesInReferencedAssembly.cs | 26 +++ ...butesInReferencedAssembly_AssemblyLevel.cs | 30 ++++ .../LinkAttributes/LinkAttributeErrorCases.cs | 83 +++++++++ .../LinkAttributeErrorCases.xml | 69 ++++++++ .../LinkAttributes/LinkerAttributeRemoval.cs | 162 ++++++++++++++++++ .../LinkAttributes/LinkerAttributeRemoval.xml | 46 +++++ ...ovalAndPreserveAssembly.LinkAttributes.xml | 8 + ...nkerAttributeRemovalAndPreserveAssembly.cs | 54 ++++++ .../LinkerAttributeRemovalConditional.cs | 148 ++++++++++++++++ .../LinkerAttributeRemovalConditional.xml | 69 ++++++++ .../LinkerAttributeRemovalWithOverride.xml | 8 + .../OverrideAttributeRemoval.cs | 40 +++++ .../OverrideAttributeRemoval.xml | 9 + .../LinkAttributes/TypedArguments.cs | 123 +++++++++++++ .../LinkAttributes/TypedArguments.xml | 40 +++++ .../LinkAttributes/TypedArgumentsErrors.cs | 35 ++++ .../LinkAttributes/TypedArgumentsErrors.xml | 31 ++++ .../TestCases/TestDatabase.cs | 5 + .../Mono.Linker.Tests/TestCases/TestSuites.cs | 7 + .../TestCasesRunner/ILCompilerDriver.cs | 11 +- .../TestCasesRunner/ILCompilerOptions.cs | 1 + .../ILCompilerOptionsBuilder.cs | 1 + 40 files changed, 1319 insertions(+), 11 deletions(-) create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs create mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml diff --git a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs index a46d8446aabec8..efdf0e73670108 100644 --- a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs +++ b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs @@ -138,9 +138,7 @@ protected virtual void ProcessAssemblies(XPathNavigator nav) bool processAllAssemblies = ShouldProcessAllAssemblies(assemblyNav, out AssemblyName? name); if (processAllAssemblies && AllowedAssemblySelector != AllowedAssemblies.AllAssemblies) { - // NativeAOT doesn't have a way to eliminate all the occurrences of an attribute yet - // https://github.com/dotnet/runtime/issues/77753 - //LogWarning(assemblyNav, DiagnosticId.XmlUnsupportedWildcard); + LogWarning(assemblyNav, DiagnosticId.XmlUnsuportedWildcard); continue; } diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs index 04d765c00e4c78..29d311e6bfd930 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs @@ -1126,11 +1126,18 @@ public AssemblyFeatureInfo(EcmaModule module, Logger logger, IReadOnlyDictionary Module = module; RemovedAttributes = new HashSet(); - PEMemoryBlock resourceDirectory = module.PEReader.GetSectionData(module.PEReader.PEHeaders.CorHeader.ResourcesDirectory.RelativeVirtualAddress); + ParseLinkAttributesXml(module, logger, featureSwitchValues, false); + ParseLinkAttributesXml(module, logger, featureSwitchValues, true); + } + + public void ParseLinkAttributesXml(EcmaModule module, Logger logger, IReadOnlyDictionary featureSwitchValues, bool isCorLib) + { + EcmaModule xmlModule = isCorLib ? (EcmaModule) module.Context.SystemModule : module; + PEMemoryBlock resourceDirectory = xmlModule.PEReader.GetSectionData(xmlModule.PEReader.PEHeaders.CorHeader.ResourcesDirectory.RelativeVirtualAddress); - foreach (var resourceHandle in module.MetadataReader.ManifestResources) + foreach (var resourceHandle in xmlModule.MetadataReader.ManifestResources) { - ManifestResource resource = module.MetadataReader.GetManifestResource(resourceHandle); + ManifestResource resource = xmlModule.MetadataReader.GetManifestResource(resourceHandle); // Don't try to process linked resources or resources in other assemblies if (!resource.Implementation.IsNil) @@ -1138,7 +1145,7 @@ public AssemblyFeatureInfo(EcmaModule module, Logger logger, IReadOnlyDictionary continue; } - string resourceName = module.MetadataReader.GetString(resource.Name); + string resourceName = xmlModule.MetadataReader.GetString(resource.Name); if (resourceName == "ILLink.LinkAttributes.xml") { BlobReader reader = resourceDirectory.GetReader((int)resource.Offset, resourceDirectory.Length - (int)resource.Offset); @@ -1150,19 +1157,26 @@ public AssemblyFeatureInfo(EcmaModule module, Logger logger, IReadOnlyDictionary ms = new UnmanagedMemoryStream(reader.CurrentPointer, length); } - RemovedAttributes = LinkAttributesReader.GetRemovedAttributes(logger, module.Context, ms, resource, module, "resource " + resourceName + " in " + module.ToString(), featureSwitchValues); + RemovedAttributes.UnionWith(LinkAttributesReader.GetRemovedAttributes(logger, xmlModule.Context, ms, resource, module, "resource " + resourceName + " in " + module.ToString(), featureSwitchValues)); } } } } - private sealed class LinkAttributesReader : ProcessLinkerXmlBase + public static class PlatformAssemblies + { + public const string CoreLib = "System.Private.CoreLib"; + } + + internal sealed class LinkAttributesReader : ProcessLinkerXmlBase { private readonly HashSet _removedAttributes = new(); + private readonly ModuleDesc _resource; public LinkAttributesReader(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues) : base(logger, context, documentStream, resource, resourceAssembly, xmlDocumentLocation, featureSwitchValues) { + _resource = resourceAssembly; } private void ProcessAttribute(TypeDesc type, XPathNavigator nav) @@ -1181,6 +1195,21 @@ public static HashSet GetRemovedAttributes(Logger logger, TypeSystemCo return rdr._removedAttributes; } + protected override AllowedAssemblies AllowedAssemblySelector + { + get + { + if (_resource?.Assembly == null) + return AllowedAssemblies.AllAssemblies; + + // Corelib XML may contain assembly wildcard to support compiler-injected attribute types + if (_resource?.Assembly.GetName().Name == PlatformAssemblies.CoreLib) + return AllowedAssemblies.AllAssemblies; + + return AllowedAssemblies.ContainingAssembly; + } + } + protected override void ProcessAssembly(ModuleDesc assembly, XPathNavigator nav, bool warnOnUnresolvedTypes) { ProcessTypes(assembly, nav, warnOnUnresolvedTypes); diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs new file mode 100644 index 00000000000000..5ada498c03b113 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs @@ -0,0 +1,22 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupLinkAttributesFile ("AssemblyLevelLinkerAttributeRemoval.xml")] + [IgnoreLinkAttributes (false)] + + [SetupCompileBefore ("library.dll", new[] { "Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs" })] + + [RemovedTypeInAssembly ("library.dll", "Mono.Linker.Tests.Cases.TestAttributeLib.MyAttribute")] + class AssemblyLevelLinkerAttributeRemoval + { + public static void Main () + { + new Mono.Linker.Tests.Cases.TestAttributeLib.Foo (); + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml new file mode 100644 index 00000000000000..40e22d677e645f --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs new file mode 100644 index 00000000000000..c5eeef15bd6ff4 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs @@ -0,0 +1,23 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using Mono.Linker.Tests.Cases.TestAttributeLib; + +[assembly: MyAttribute] +[module: MyAttribute] + +namespace Mono.Linker.Tests.Cases.TestAttributeLib +{ + [System.AttributeUsage (System.AttributeTargets.All, Inherited = false, AllowMultiple = true)] + public sealed class MyAttribute : System.Attribute + { + public MyAttribute () + { + } + } + + public class Foo + { + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs new file mode 100644 index 00000000000000..9943876b428495 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs @@ -0,0 +1,6 @@ +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + public class EmbeddedAttributeErrorCases + { + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml new file mode 100644 index 00000000000000..f5366f23b2ca0e --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml new file mode 100644 index 00000000000000..d5ca5b732215b3 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs new file mode 100644 index 00000000000000..aace5a4e2fa50e --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs @@ -0,0 +1,10 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + public static class Used + { + public static void Use () { } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs new file mode 100644 index 00000000000000..4bd134ad9f936c --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + public class AttributeFromCopyAssemblyAttribute : Attribute + { + // Add all kinds of members - as they cause the type to be marked again + // and we don't want these to generate more warnings. + + private object _field; + + private class NestedType + { + } + + private int TestProperty { get; } + + private event EventHandler _event; + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs new file mode 100644 index 00000000000000..840397210bac98 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs @@ -0,0 +1,32 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Text; + +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + public enum TestAttributeUsedFromCopyAssemblyEnum + { + None + } + + public class TestAttributeUsedFromCopyAssemblyAttribute : Attribute + { + public TestAttributeUsedFromCopyAssemblyAttribute (TestAttributeUsedFromCopyAssemblyEnum n) + { + } + } + + public class TestAnotherAttributeUsedFromCopyAssemblyAttribute : Attribute + { + public TestAnotherAttributeUsedFromCopyAssemblyAttribute () + { + } + } + + public class TestAttributeReferencedAsTypeFromCopyAssemblyAttribute : Attribute + { + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs new file mode 100644 index 00000000000000..f46724c69541f5 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs @@ -0,0 +1,20 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.ComponentModel; +using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; + +[assembly: TestAnotherAttributeUsedFromCopyAssembly] + +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + [TestAttributeUsedFromCopyAssemblyAttribute (TestAttributeUsedFromCopyAssemblyEnum.None)] + [EditorBrowsable (EditorBrowsableState.Never)] + public class TypeOnCopyAssemblyWithAttributeUsage + { + public TypeOnCopyAssemblyWithAttributeUsage () + { + typeof (TestAttributeReferencedAsTypeFromCopyAssemblyAttribute).ToString (); + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs new file mode 100644 index 00000000000000..3b6cbbe6a23fc7 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + [EmbeddedAttributeToBeRemoved] + public class TypeWithEmbeddedAttributeToBeRemoved + { + public TypeWithEmbeddedAttributeToBeRemoved () { } + } + + public class EmbeddedAttributeToBeRemoved : Attribute + { + public EmbeddedAttributeToBeRemoved () { } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs new file mode 100644 index 00000000000000..eb02e492f219c3 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs @@ -0,0 +1,12 @@ +using System; +using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; + +[assembly: TestRemove] +[assembly: TestDontRemove] + +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + public class ReferencedAssemblyWithAttributes + { + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml new file mode 100644 index 00000000000000..17ef8e3aee84bd --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs new file mode 100644 index 00000000000000..d2bdbba86773e9 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs @@ -0,0 +1,18 @@ +using System; + +namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies +{ + public class TestRemoveAttribute : Attribute + { + public TestRemoveAttribute () + { + } + } + + public class TestDontRemoveAttribute : Attribute + { + public TestDontRemoveAttribute () + { + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs new file mode 100644 index 00000000000000..b882e019ba9461 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs @@ -0,0 +1,49 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Diagnostics.CodeAnalysis; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Helpers; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SkipKeptItemsValidation] + [SetupCompileResource ("EmbeddedLinkAttributes.xml", "ILLink.LinkAttributes.xml")] + [IgnoreLinkAttributes (false)] + [RemovedResourceInAssembly ("test.exe", "ILLink.LinkAttributes.xml")] + [ExpectedNoWarnings] + class EmbeddedLinkAttributes + { + public static void Main () + { + var instance = new EmbeddedLinkAttributes (); + + instance.ReadFromInstanceField (); + instance.ReadFromInstanceField2 (); + } + + Type _typeWithPublicParameterlessConstructor; + + [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicConstructors))] + [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresNonPublicConstructors))] + [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicParameterlessConstructor), ProducedBy = ProducedBy.NativeAot)] + private void ReadFromInstanceField () + { + _typeWithPublicParameterlessConstructor.RequiresPublicParameterlessConstructor (); + _typeWithPublicParameterlessConstructor.RequiresPublicConstructors (); + _typeWithPublicParameterlessConstructor.RequiresNonPublicConstructors (); + } + + Type _typeWithPublicFields; + + [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicConstructors))] + [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.NativeAot)] + private void ReadFromInstanceField2 () + { + _typeWithPublicFields.RequiresPublicConstructors (); + _typeWithPublicFields.RequiresPublicFields (); + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml new file mode 100644 index 00000000000000..decb3699ad61f6 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml @@ -0,0 +1,18 @@ + + + + + + PublicParameterlessConstructor + + + + + + + + PublicFields + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs new file mode 100644 index 00000000000000..b6aee070286252 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; +using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupCompileBefore ("attributes.dll", new string[] { "Dependencies/TestRemoveDontRemoveAttributes.cs" }, + resources: new object[] { new string[] { "Dependencies/TestRemoveAttribute.xml", "ILLink.LinkAttributes.xml" } })] + [IgnoreLinkAttributes (false)] + class EmbeddedLinkAttributesInReferencedAssembly + { + public static void Main () + { + TestAttributeRemoval (); + } + + [Kept] + [KeptAttributeAttribute (typeof (TestDontRemoveAttribute))] + [TestDontRemove] + [TestRemove] + public static void TestAttributeRemoval () { } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs new file mode 100644 index 00000000000000..5111a00f06c007 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs @@ -0,0 +1,30 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; +using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupCompileBefore ("attributes.dll", new string[] { "Dependencies/TestRemoveDontRemoveAttributes.cs" }, + resources: new object[] { new string[] { "Dependencies/TestRemoveAttribute.xml", "ILLink.LinkAttributes.xml" } }, addAsReference: false)] + [SetupCompileBefore ("library.dll", new string[] { "Dependencies/ReferencedAssemblyWithAttributes.cs" }, references: new string[] { "attributes.dll" })] + [SetupLinkerAction ("copyused", "attributes")] // Ensure that assembly-level attributes are kept + [IgnoreLinkAttributes (false)] + [KeptAttributeInAssembly ("library.dll", "Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TestDontRemoveAttribute")] + [RemovedAttributeInAssembly ("library.dll", "Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TestRemoveAttribute")] + class EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel + { + public static void Main () + { + ReferenceOtherAssembly (); + } + + [Kept] + public static void ReferenceOtherAssembly () + { + var _1 = new ReferencedAssemblyWithAttributes (); + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs new file mode 100644 index 00000000000000..91938c375f96da --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs @@ -0,0 +1,83 @@ +using System; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; +using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupLinkAttributesFile ("LinkAttributeErrorCases.xml")] + [IgnoreLinkAttributes (false)] + [SetupLinkerArgument ("--skip-unresolved", "true")] + [SetupCompileBefore ("library.dll", new string[] { "Dependencies/EmbeddedAttributeErrorCases.cs" }, + resources: new object[] { new string[] { "Dependencies/EmbeddedAttributeErrorCases.xml", "ILLink.LinkAttributes.xml" } })] + + [ExpectedWarning ("IL2007", "NonExistentAssembly2", FileName = "LinkAttributeErrorCases.xml", SourceLine = 67, SourceColumn = 4)] + [ExpectedWarning ("IL2030", "NonExistentAssembly1", FileName = "LinkAttributeErrorCases.xml", SourceLine = 6, SourceColumn = 8)] + [ExpectedWarning ("IL2030", "MalformedAssemblyName, thisiswrong", FileName = "LinkAttributeErrorCases.xml", SourceLine = 7, SourceColumn = 8)] + [ExpectedWarning ("IL2031", "NonExistentAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 10, SourceColumn = 8)] + [ExpectedWarning ("IL2022", "AttributeWithNoParametersAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 13, SourceColumn = 8)] + [ExpectedWarning ("IL2022", "AttributeWithEnumParameterAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 18, SourceColumn = 8)] + [ExpectedWarning ("IL2022", "AttributeWithIntParameterAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 23, SourceColumn = 8)] + [ExpectedWarning ("IL2023", "GetTypeMethod", FileName = "LinkAttributeErrorCases.xml", SourceLine = 47, SourceColumn = 10)] + [ExpectedWarning ("IL2024", "methodParameter", "MethodWithParameter", FileName = "LinkAttributeErrorCases.xml", SourceLine = 57, SourceColumn = 10)] + [ExpectedWarning ("IL2029", FileName = "LinkAttributeErrorCases.xml", SourceLine = 64, SourceColumn = 6)] + [ExpectedWarning ("IL2029", FileName = "LinkAttributeErrorCases.xml", SourceLine = 65, SourceColumn = 6)] + [ExpectedWarning ("IL2051", FileName = "LinkAttributeErrorCases.xml", SourceLine = 29, SourceColumn = 10)] + [ExpectedWarning ("IL2052", "NonExistentPropertyName", FileName = "LinkAttributeErrorCases.xml", SourceLine = 34, SourceColumn = 10)] + [ExpectedWarning ("IL2100", FileName = "ILLink.LinkAttributes.xml", SourceLine = 3, SourceColumn = 4)] + [ExpectedWarning ("IL2101", "library", "test", FileName = "ILLink.LinkAttributes.xml", SourceLine = 5, SourceColumn = 4)] + [ExpectedNoWarnings] + class LinkAttributeErrorCases + { + public static void Main () + { + var _ = new EmbeddedAttributeErrorCases (); + } + + public enum AttributeEnum + { + None + } + + public class AttributeWithEnumParameterAttribute : Attribute + { + public AttributeWithEnumParameterAttribute (AttributeEnum enumValue) + { + } + } + + public class AttributeWithIntParameterAttribute : Attribute + { + public AttributeWithIntParameterAttribute (int intValue) + { + } + } + + public class AttributeWithNoParametersAttribute : Attribute + { + public AttributeWithNoParametersAttribute () + { + } + } + + public class AttributeWithPropertyAttribute : Attribute + { + public AttributeWithPropertyAttribute () + { + } + + int IntProperty { get; } + } + + public class FirstAttribute : Attribute { } + public class SecondAttribute : Attribute { } + + public Type GetTypeMethod () => null; + + public void MethodWithParameter (int methodParameter) { } + + public class ReferencedFromOtherAssembly + { + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml new file mode 100644 index 00000000000000..297f8fed94d73c --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + ExtraArgumentValue + + + + + NonExistentEnumValue + + + + + NotANumber + + + + + StringValue + + + + + StringValue + + + + + StringValue + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs new file mode 100644 index 00000000000000..2475dd4f4027d9 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs @@ -0,0 +1,162 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Helpers; +using Mono.Linker.Tests.Cases.Expectations.Metadata; +using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupLinkAttributesFile ("LinkerAttributeRemoval.xml")] + [IgnoreLinkAttributes (false)] + + [SetupCompileBefore ("attribute.dll", new[] { "Dependencies/LinkerAttributeRemovalAttributeToRemove.cs" })] + [SetupCompileBefore ("copyattribute.dll", new[] { "Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs" })] + [SetupLinkerAction ("copy", "copyattribute")] +#if !NETCOREAPP + [Reference ("System.dll")] + [SetupCompileBefore ("copyassembly.dll", new[] { "Dependencies/LinkerAttributeRemovalCopyAssembly.cs" }, references: new[] { "System.dll", "attribute.dll" })] +#else + [SetupCompileBefore ("copyassembly.dll", new[] { "Dependencies/LinkerAttributeRemovalCopyAssembly.cs" }, references: new[] { "attribute.dll" })] +#endif + [SetupLinkerAction ("copy", "copyassembly")] + + [SetupCompileBefore ( + "LinkerAttributeRemovalEmbeddedAndLazyLoad.dll", + new[] { "Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs" })] + + // The test here is that the TypeOnCopyAssemblyWithAttributeUsage has an attribute TestAttributeUsedFromCopyAssemblyAttribute + // which is marked for removal by the LinkerAttributeRemoval.xml. Normally that would mean that the attribute usage + // as well as the type (and assembly since it's the only type in it) would be removed as there are no other usages of the attribute type. + // But because the copyassembly is linked with "copy" action, the attribute usage should not be removed and thus the attribute + // should be kept. + [KeptAssembly ("copyassembly.dll")] + [KeptAssembly ("attribute.dll")] + [KeptTypeInAssembly ("attribute.dll", typeof (TestAttributeUsedFromCopyAssemblyAttribute))] + [KeptTypeInAssembly ("attribute.dll", typeof (TestAnotherAttributeUsedFromCopyAssemblyAttribute))] + [KeptTypeInAssembly ("attribute.dll", typeof (TestAttributeReferencedAsTypeFromCopyAssemblyAttribute))] + [KeptTypeInAssembly ("copyassembly.dll", typeof (TypeOnCopyAssemblyWithAttributeUsage))] + [KeptAttributeInAssembly ("copyassembly.dll", typeof (TestAttributeUsedFromCopyAssemblyAttribute), typeof (TypeOnCopyAssemblyWithAttributeUsage))] + [KeptAttributeInAssembly ("copyassembly.dll", typeof (EditorBrowsableAttribute), typeof (TypeOnCopyAssemblyWithAttributeUsage))] + [KeptAttributeInAssembly ("copyassembly.dll", typeof (TestAnotherAttributeUsedFromCopyAssemblyAttribute))] + + [KeptAssembly ("copyattribute.dll")] + [KeptTypeInAssembly ("copyattribute.dll", typeof (AttributeFromCopyAssemblyAttribute))] + + [KeptAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad.dll")] + [KeptTypeInAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad.dll", typeof (TypeWithEmbeddedAttributeToBeRemoved))] + // This needs to fixed with lazy loading assembly refactoring - currently the assembly="*" only applies to assemblies in initial closure + // The attribute should be removed and not kept as it is now + // [RemovedAttributeInAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad.dll", typeof (EmbeddedAttributeToBeRemoved), typeof (TypeWithEmbeddedAttributeToBeRemoved))] + [KeptAttributeInAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad", typeof (EmbeddedAttributeToBeRemoved), typeof (TypeWithEmbeddedAttributeToBeRemoved))] + + [LogContains ("IL2045: Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TypeOnCopyAssemblyWithAttributeUsage.TypeOnCopyAssemblyWithAttributeUsage(): Attribute 'Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TestAttributeReferencedAsTypeFromCopyAssemblyAttribute'")] + [LogDoesNotContain ("IL2045")] // No other 2045 messages should be logged + + [ExpectedWarning ("IL2049", "'InvalidInternal'", FileName = "LinkerAttributeRemoval.xml")] + [ExpectedWarning ("IL2048", "RemoveAttributeInstances", FileName = "LinkerAttributeRemoval.xml")] + + [ExpectedNoWarnings] + + [KeptMember (".ctor()")] + class LinkerAttributeRemoval + { + public static void Main () + { + var instance = new LinkerAttributeRemoval (); + instance._fieldWithCustomAttribute = null; + string value = instance.methodWithCustomAttribute (null); + TestType (); + + _ = new TypeOnCopyAssemblyWithAttributeUsage (); + TestAttributeUsageRemovedEvenIfAttributeIsKeptForOtherReasons (); + + TestAttributeFromCopyAssembly (); + + TestEmbeddedAttributeLazyLoadRemoved (); + + TestMarkAllRemove (); + } + +#pragma warning disable CS0414 + [Kept] + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] + Type _fieldWithCustomAttribute; +#pragma warning restore CS0414 + + [Kept] + [KeptAttributeAttribute (typeof (TestDontRemoveAttribute))] + [TestDontRemoveAttribute] + [TestRemoveAttribute] + [return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] + private string methodWithCustomAttribute ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] string parameterWithCustomAttribute) + { + return null; + } + + [ExpectedWarning ("IL2045", "Attribute 'System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute'")] + [Kept] + public static void TestType () + { + const string reflectionTypeKeptString = "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute"; + var typeKept = Type.GetType (reflectionTypeKeptString, false); + } + + [Kept] + [TestAttributeUsedFromCopyAssembly (TestAttributeUsedFromCopyAssemblyEnum.None)] + static void TestAttributeUsageRemovedEvenIfAttributeIsKeptForOtherReasons () + { + } + + [Kept] + [AttributeFromCopyAssembly] + static void TestAttributeFromCopyAssembly () + { + } + + [Kept] + // This attribute should be removed once the assembly lazy loading is implemented + [DynamicDependency ( + DynamicallyAccessedMemberTypes.PublicConstructors, + "Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TypeWithEmbeddedAttributeToBeRemoved", + "LinkerAttributeRemovalEmbeddedAndLazyLoad")] + static void TestEmbeddedAttributeLazyLoadRemoved () + { + // This needs the DynamicDependency above otherwise linker will not load the assembly at all + Activator.CreateInstance (Type.GetType ("Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TypeWithEmbeddedAttributeToBeRemoved, LinkerAttributeRemovalEmbeddedAndLazyLoad")); + } + + [ExpectedWarning ("IL2045")] + [Kept] + static void TestMarkAllRemove () + { + Type.GetType ("Mono.Linker.Tests.Cases.LinkAttributes.TestMarkAllRemoveAttribute").RequiresAll (); + } + } + + [KeptBaseType (typeof (System.Attribute))] + class TestDontRemoveAttribute : Attribute + { + [Kept] + public TestDontRemoveAttribute () + { + } + } + + class TestRemoveAttribute : Attribute + { + public TestRemoveAttribute () + { + } + } + + [KeptBaseType (typeof (System.Attribute))] + [KeptMember (".ctor()")] + class TestMarkAllRemoveAttribute : Attribute + { + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml new file mode 100644 index 00000000000000..de2c080446b00e --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml new file mode 100644 index 00000000000000..095f6eb235e7f3 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs new file mode 100644 index 00000000000000..ab48d5fb932757 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs @@ -0,0 +1,54 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; +using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [IgnoreDescriptors (false)] + + // The test verifies that removed attributes which are later on kept due to descriptors correctly warn. + // The setup is: + // - test assembly with the AttributeToRemoveAttribute type + // - link attributes.xml which marks the attribute for removal (applied early, in this case via command line, but could be a embedded in the test assembly) + // - the attribute is used by the test assembly + // - another assembly lib.dll is added and is referenced (after the attribute is used/marked) + // - This new assembly has a descriptor which marks the entire test assembly (note that it marks the TEST assembly) + // - This marking causes the warning, as it's an explicit request to keep the attribute which was supposed to be removed + + [SetupLinkAttributesFile ("LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml")] + + [SetupCompileBefore ( + "lib.dll", + new[] { "Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs" }, + resources: new object[] { new string[] { "Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml", "ILLink.Descriptors.xml" } })] + + [ExpectedWarning ("IL2045", FileName = "ILLink.Descriptors.xml in lib, Version=0.0.0.0, Culture=neutral, PublicKeyToken")] + [ExpectedNoWarnings] + + [KeptMember (".ctor()")] + class LinkerAttributeRemovalAndPreserveAssembly + { + public static void Main () + { + TestAttributeRemoval (); + } + + [AttributeToRemoveAttribute] + [Kept] + static void TestAttributeRemoval () + { + Used.Use (); + } + } + + // The attribute is kept (partially) because we found out about it too late + // It does report a warning though + [Kept] + [KeptBaseType (typeof (Attribute))] + [KeptMember (".ctor()")] + public class AttributeToRemoveAttribute : Attribute { } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs new file mode 100644 index 00000000000000..b7dc024459112b --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs @@ -0,0 +1,148 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupLinkAttributesFile ("LinkerAttributeRemovalConditional.xml")] + [IgnoreLinkAttributes (false)] + class LinkerAttributeRemovalConditional + { + public static void Main () + { + Kept_1 (); + Kept_2 (); + Kept_3 (); + Removed_1 (); + Removed_2 (); + Removed_3 (); + Removed_4 (); + Kept_4 (); + Kept_5 (); + Removed_5 (); + Removed_6 (); + Kept_6 (); + } + + [Kept] + [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] + [TestConditionalRemove ()] + static void Kept_1 () + { + } + + [Kept] + [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] + [TestConditionalRemove ("my-value", "string value")] + static void Kept_2 () + { + } + + [Kept] + [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] + [TestConditionalRemove (1, true)] + static void Kept_3 () + { + } + + [Kept] + [TestConditionalRemove ("remove0", "string value")] + static void Removed_1 () + { + } + + [Kept] + [TestConditionalRemove (100, '1')] + [TestConditionalRemove ("remove1", '1', 3)] + static void Removed_2 () + { + } + + [Kept] + [TestConditionalRemove ("remove0", 'k', 0)] // It's removed because the converted string value matches + static void Removed_3 () + { + } + + [Kept] + [TestConditionalRemove ("remove2", "remove3")] + static void Removed_4 () + { + } + + [Kept] + [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] + [TestConditionalRemove ("remove2", "unmatched second arg")] + static void Kept_4 () + { + } + + [Kept] + [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] + [TestConditionalRemove ((int) 99, "remove3")] + static void Kept_5 () // Kept because int arg in constructor doesn't match long arg in xml + { + } + + [Kept] + [TestConditionalRemove (72, "a", "b", "c", "d", "e")] + static void Removed_5 () + { + } + + [Kept] + [TestConditionalRemove (new int[] { 1, 2, 3 }, "remove4")] + static void Removed_6 () + { + } + + [Kept] + [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] + [TestConditionalRemove (new int[] { 1, 2, 3, 4 }, "remove4")] + static void Kept_6 () + { + } + } + + [Kept] + [KeptBaseType (typeof (System.Attribute))] + [KeptAttributeAttribute (typeof (AttributeUsageAttribute))] + [AttributeUsage (AttributeTargets.All, AllowMultiple = true)] + class TestConditionalRemoveAttribute : Attribute + { + [Kept] + public TestConditionalRemoveAttribute () + { + } + + [Kept] + // Any usage with "remove0" key is removed + public TestConditionalRemoveAttribute (string key, string value) + { + } + + // Any usage with 100 key is removed + // Any usage with "remove1" key is removed + public TestConditionalRemoveAttribute (object key, char value, int ivalue) + { + } + + [Kept] + public TestConditionalRemoveAttribute (int key, object value) + { + } + + public TestConditionalRemoveAttribute (int key, [KeptAttributeAttribute (typeof (ParamArrayAttribute))] params object[] values) + { + } + + [Kept] + public TestConditionalRemoveAttribute (int[] intArray, string str) + { + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml new file mode 100644 index 00000000000000..70518eb7939054 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml @@ -0,0 +1,69 @@ + + + + + + + remove1 + + + + + + + 100 + + + + + + + remove0 + + + + + remove2 + remove3 + + + + + + 99 + + + remove3 + + + + + + + + + 72 + + + + a + b + c + d + e + + + + + + + + 1 + 2 + 3 + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml new file mode 100644 index 00000000000000..ee3aa79a481125 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs new file mode 100644 index 00000000000000..a055cf6b97f2ff --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs @@ -0,0 +1,40 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupLinkAttributesFile ("LinkerAttributeRemovalWithOverride.xml")] + [SetupLinkerDescriptorFile ("OverrideAttributeRemoval.xml")] + [IgnoreLinkAttributes (false)] + [KeptMember (".ctor()")] + class OverrideAttributeRemoval + { + public static void Main () + { + var instance = new OverrideAttributeRemoval (); + instance._fieldWithCustomAttribute = null; + string value = instance.methodWithCustomAttribute ("parameter"); + } + [Kept] + [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] + Type _fieldWithCustomAttribute; + + [Kept] + [return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] + [return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] + private string methodWithCustomAttribute ( + [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] + string parameterWithCustomAttribute) + { + return "this is a return value"; + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml new file mode 100644 index 00000000000000..3991969c573718 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs new file mode 100644 index 00000000000000..c89948c37bad32 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs @@ -0,0 +1,123 @@ +using System; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupLinkAttributesFile ("TypedArguments.xml")] + [IgnoreLinkAttributes (false)] + [SetupLinkerArgument ("--verbose")] + + [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.ObjectAttribute.ObjectAttribute(Object) { args: System.Object Mono.Cecil.CustomAttributeArgument }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field1'")] + [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.ObjectAttribute.ObjectAttribute(Object) { args: System.Object Mono.Cecil.CustomAttributeArgument }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field2'")] + [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.EnumAttribute.EnumAttribute(TypedArgumentsEnumA) { args: Mono.Linker.Tests.Cases.LinkAttributes.TypedArgumentsEnumA 3 }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field3'")] + [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.ByteAttribute.ByteAttribute(Byte) { args: System.Byte 6 }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field4'")] + [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.StringAttribute.StringAttribute(String) { args: System.String str }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field5'")] + [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.TypeAttribute.TypeAttribute(Type) { args: System.Type System.DateTime }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field6'")] + class TypedArguments + { + [Kept] + static object field1; + + [Kept] + static object field2; + + [Kept] + static object field3; + + [Kept] + static object field4; + + [Kept] + static object field5; + + [Kept] + static object field6; + + public static void Main () + { + field1 = null; + field2 = null; + field3 = null; + field4 = null; + field5 = null; + field6 = null; + } + + public class ObjectAttribute : Attribute + { + public ObjectAttribute (object objectValue) + { + } + + public ObjectAttribute (string stringValue) + { + } + } + + public class ByteAttribute : Attribute + { + public ByteAttribute (uint intValue) + { + } + + public ByteAttribute (byte byteValue) + { + } + + public ByteAttribute (object objectValue) + { + } + } + + public class EnumAttribute : Attribute + { + public EnumAttribute (int intValue) + { + } + + public EnumAttribute (object objectValue) + { + } + + public EnumAttribute (TypedArgumentsEnumA enumA) + { + } + + public EnumAttribute (TypedArgumentsEnumB enumB) + { + } + } + + public class StringAttribute : Attribute + { + public StringAttribute (string stringValue) + { + } + + public StringAttribute (object objectValue) + { + } + } + + public class TypeAttribute : Attribute + { + public TypeAttribute (string stringValue) + { + } + + public TypeAttribute (Type typeValue) + { + } + } + } + + enum TypedArgumentsEnumA + { + Value = 2 + } + + enum TypedArgumentsEnumB + { + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml new file mode 100644 index 00000000000000..3340054bb68d91 --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml @@ -0,0 +1,40 @@ + + + + + + + StringValue + + + + + + + 9 + + + + + + 3 + + + + + 6 + + + + + str + + + + + System.DateTime + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs new file mode 100644 index 00000000000000..44513d83e07bab --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs @@ -0,0 +1,35 @@ +using System; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.LinkAttributes +{ + [SetupLinkAttributesFile ("TypedArgumentsErrors.xml")] + [IgnoreLinkAttributes (false)] + [NoLinkedOutput] + + [LogContains ("The type 'System.NoOBJECT' used with attribute value 'str' could not be found")] + [LogContains ("Cannot convert value 'str' to type 'System.Int32'")] + [LogContains ("Custom attribute argument for 'System.Object' requires nested 'argument' node")] + [LogContains ("The type 'System.Bar' used with attribute value 'str4' could not be found")] + [LogContains ("Could not resolve custom attribute type value 'str5'")] + class TypedArgumentsErrors + { + static object field; + + public static void Main () + { + } + + public class ObjectAttribute : Attribute + { + public ObjectAttribute (object objectValue) + { + } + + public ObjectAttribute (string stringValue) + { + } + } + } +} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml new file mode 100644 index 00000000000000..0b870b9b9c882d --- /dev/null +++ b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml @@ -0,0 +1,31 @@ + + + + + + str + + + + + str + + + + + str + + + + + str4 + + + + + str5 + + + + + diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs index de77b4e4d8621c..55c826adb79c62 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs @@ -24,6 +24,11 @@ public static IEnumerable DynamicDependencies () return TestNamesBySuiteName (); } + public static IEnumerable LinkAttributes () + { + return TestNamesBySuiteName (); + } + public static IEnumerable LinkXml() { return TestNamesBySuiteName(); diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs index e34ce26045a1f9..41df23866af530 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs @@ -24,6 +24,13 @@ public void DynamicDependencies (string t) Run (t); } + [Theory] + [MemberData (nameof (TestDatabase.LinkAttributes), MemberType = typeof (TestDatabase))] + public void LinkAttributes (string t) + { + Run (t); + } + [Theory] [MemberData (nameof (TestDatabase.LinkXml), MemberType = typeof (TestDatabase))] public void LinkXml (string t) diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs index dc1a00667d3b61..3aacaf5e9f8340 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs @@ -65,14 +65,21 @@ public ILScanResults Trim (ILCompilerOptions options, ILogWriter logWriter) ILProvider ilProvider = new NativeAotILProvider (); + Logger logger = new Logger (logWriter, ilProvider, isVerbose: true); + + HashSet removedAttributes = new HashSet (); + foreach (var linkAttributes in options.LinkAttributes) { + if (!File.Exists (linkAttributes)) + throw new FileNotFoundException ($"'{linkAttributes}' doesn't exist"); + _ = removedAttributes; + } + foreach (var descriptor in options.Descriptors) { if (!File.Exists (descriptor)) throw new FileNotFoundException ($"'{descriptor}' doesn't exist"); compilationRoots.Add (new ILCompiler.DependencyAnalysis.TrimmingDescriptorNode (descriptor)); } - Logger logger = new Logger (logWriter, ilProvider, isVerbose: true); - ilProvider = new FeatureSwitchManager (ilProvider, logger, options.FeatureSwitches); CompilerGeneratedState compilerGeneratedState = new CompilerGeneratedState (ilProvider, logger); diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs index 5b48963927e19e..ba7d25632db84e 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs @@ -14,6 +14,7 @@ public class ILCompilerOptions public List AdditionalRootAssemblies = new List (); public Dictionary FeatureSwitches = new Dictionary (); public List Descriptors = new List (); + public List LinkAttributes = new List (); public bool FrameworkCompilation; } } diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs index afb660e9b7bc9d..680efd8074f625 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs @@ -156,6 +156,7 @@ public virtual void AddSubstitutions (string file) public virtual void AddLinkAttributes (string file) { + Options.LinkAttributes.Add (file); } public virtual void AddAdditionalArgument (string flag, string[] values) From 01369e2a47d20c8fc2fd117b455ce07fe3040ddb Mon Sep 17 00:00:00 2001 From: Tlakollo Date: Wed, 25 Jan 2023 17:33:55 -0800 Subject: [PATCH 2/6] Remove linker testing since it tests more unsupported scenarios and no actual testing of the AllAssembly functionality Refactor some of the code --- .../Common/Compiler/ProcessLinkerXmlBase.cs | 6 +- .../Compiler/UsageBasedMetadataManager.cs | 30 ++-- .../AssemblyLevelLinkerAttributeRemoval.cs | 22 --- .../AssemblyLevelLinkerAttributeRemoval.xml | 8 - ...AssemblyLevelLinkerAttributeRemoval_Lib.cs | 23 --- .../EmbeddedAttributeErrorCases.cs | 6 - .../EmbeddedAttributeErrorCases.xml | 10 -- ...ovalAndPreserveAssembly_Lib.Descriptor.xml | 4 - ...AttributeRemovalAndPreserveAssembly_Lib.cs | 10 -- ...tributeRemovalAttributeFromCopyAssembly.cs | 22 --- ...LinkerAttributeRemovalAttributeToRemove.cs | 32 ---- .../LinkerAttributeRemovalCopyAssembly.cs | 20 --- ...nkerAttributeRemovalEmbeddedAndLazyLoad.cs | 18 -- .../ReferencedAssemblyWithAttributes.cs | 12 -- .../Dependencies/TestRemoveAttribute.xml | 7 - .../TestRemoveDontRemoveAttributes.cs | 18 -- .../LinkAttributes/EmbeddedLinkAttributes.cs | 49 ------ .../LinkAttributes/EmbeddedLinkAttributes.xml | 18 -- ...eddedLinkAttributesInReferencedAssembly.cs | 26 --- ...butesInReferencedAssembly_AssemblyLevel.cs | 30 ---- .../LinkAttributes/LinkAttributeErrorCases.cs | 83 --------- .../LinkAttributeErrorCases.xml | 69 -------- .../LinkAttributes/LinkerAttributeRemoval.cs | 162 ------------------ .../LinkAttributes/LinkerAttributeRemoval.xml | 46 ----- ...ovalAndPreserveAssembly.LinkAttributes.xml | 8 - ...nkerAttributeRemovalAndPreserveAssembly.cs | 54 ------ .../LinkerAttributeRemovalConditional.cs | 148 ---------------- .../LinkerAttributeRemovalConditional.xml | 69 -------- .../LinkerAttributeRemovalWithOverride.xml | 8 - .../OverrideAttributeRemoval.cs | 40 ----- .../OverrideAttributeRemoval.xml | 9 - .../LinkAttributes/TypedArguments.cs | 123 ------------- .../LinkAttributes/TypedArguments.xml | 40 ----- .../LinkAttributes/TypedArgumentsErrors.cs | 35 ---- .../LinkAttributes/TypedArgumentsErrors.xml | 31 ---- .../TestCases/TestDatabase.cs | 5 - .../Mono.Linker.Tests/TestCases/TestSuites.cs | 7 - .../TestCasesRunner/ILCompilerDriver.cs | 7 - .../TestCasesRunner/ILCompilerOptions.cs | 1 - .../ILCompilerOptionsBuilder.cs | 1 - 40 files changed, 20 insertions(+), 1297 deletions(-) delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs delete mode 100644 src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml diff --git a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs index efdf0e73670108..a6b560943b9254 100644 --- a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs +++ b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs @@ -162,10 +162,8 @@ protected virtual void ProcessAssemblies(XPathNavigator nav) if (processAllAssemblies) { - throw new NotImplementedException(); - // We could avoid loading all references in this case: https://github.com/dotnet/linker/issues/1708 - //foreach (ModuleDesc assembly in GetReferencedAssemblies()) - // ProcessAssembly(assembly, assemblyNav, warnOnUnresolvedTypes: false); + Debug.Assert(_owningModule != null); + ProcessAssembly(_owningModule, assemblyNav, warnOnUnresolvedTypes: false); } else { diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs index 29d311e6bfd930..987547c08bcc07 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs @@ -39,7 +39,7 @@ public sealed class UsageBasedMetadataManager : GeneratingMetadataManager internal readonly UsageBasedMetadataGenerationOptions _generationOptions; - private readonly FeatureSwitchHashtable _featureSwitchHashtable; + private readonly LinkAttributesHashTable _featureSwitchHashtable; private static (string AttributeName, DiagnosticId Id)[] _requiresAttributeMismatchNameAndId = new[] { @@ -91,7 +91,7 @@ public UsageBasedMetadataManager( FlowAnnotations = flowAnnotations; Logger = logger; - _featureSwitchHashtable = new FeatureSwitchHashtable(Logger, new Dictionary(featureSwitchValues)); + _featureSwitchHashtable = new LinkAttributesHashTable(Logger, new Dictionary(featureSwitchValues)); FeatureSwitches = new Dictionary(featureSwitchValues); _rootEntireAssembliesModules = new HashSet(rootEntireAssembliesModules); @@ -1093,12 +1093,12 @@ public bool IsBlocked(MethodDesc methodDef) } } - private sealed class FeatureSwitchHashtable : LockFreeReaderHashtable + private sealed class LinkAttributesHashTable : LockFreeReaderHashtable { private readonly Dictionary _switchValues; private readonly Logger _logger; - public FeatureSwitchHashtable(Logger logger, Dictionary switchValues) + public LinkAttributesHashTable(Logger logger, Dictionary switchValues) { _logger = logger; _switchValues = switchValues; @@ -1126,13 +1126,17 @@ public AssemblyFeatureInfo(EcmaModule module, Logger logger, IReadOnlyDictionary Module = module; RemovedAttributes = new HashSet(); - ParseLinkAttributesXml(module, logger, featureSwitchValues, false); - ParseLinkAttributesXml(module, logger, featureSwitchValues, true); + // System.Private.CorLib has a special functionality that could delete an attribute in all modules. + // In order to get the set of attributes that need to be removed the modules need collect both the + // set of attributes in it's embedded XML file and the set inside System.Private.CorLib embedded + // XML file + ParseLinkAttributesXml(module, logger, featureSwitchValues, globalAttributeRemoval: false); + ParseLinkAttributesXml(module, logger, featureSwitchValues, globalAttributeRemoval: true); } - public void ParseLinkAttributesXml(EcmaModule module, Logger logger, IReadOnlyDictionary featureSwitchValues, bool isCorLib) + public void ParseLinkAttributesXml(EcmaModule module, Logger logger, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval) { - EcmaModule xmlModule = isCorLib ? (EcmaModule) module.Context.SystemModule : module; + EcmaModule xmlModule = globalAttributeRemoval ? (EcmaModule) module.Context.SystemModule : module; PEMemoryBlock resourceDirectory = xmlModule.PEReader.GetSectionData(xmlModule.PEReader.PEHeaders.CorHeader.ResourcesDirectory.RelativeVirtualAddress); foreach (var resourceHandle in xmlModule.MetadataReader.ManifestResources) @@ -1157,7 +1161,7 @@ public void ParseLinkAttributesXml(EcmaModule module, Logger logger, IReadOnlyD ms = new UnmanagedMemoryStream(reader.CurrentPointer, length); } - RemovedAttributes.UnionWith(LinkAttributesReader.GetRemovedAttributes(logger, xmlModule.Context, ms, resource, module, "resource " + resourceName + " in " + module.ToString(), featureSwitchValues)); + RemovedAttributes.UnionWith(LinkAttributesReader.GetRemovedAttributes(logger, xmlModule.Context, ms, resource, module, "resource " + resourceName + " in " + module.ToString(), featureSwitchValues, globalAttributeRemoval)); } } } @@ -1172,11 +1176,13 @@ internal sealed class LinkAttributesReader : ProcessLinkerXmlBase { private readonly HashSet _removedAttributes = new(); private readonly ModuleDesc _resource; + private readonly bool _globalAttributeRemoval; - public LinkAttributesReader(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues) + public LinkAttributesReader(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval) : base(logger, context, documentStream, resource, resourceAssembly, xmlDocumentLocation, featureSwitchValues) { _resource = resourceAssembly; + _globalAttributeRemoval = globalAttributeRemoval; } private void ProcessAttribute(TypeDesc type, XPathNavigator nav) @@ -1188,9 +1194,9 @@ private void ProcessAttribute(TypeDesc type, XPathNavigator nav) } } - public static HashSet GetRemovedAttributes(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues) + public static HashSet GetRemovedAttributes(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval) { - var rdr = new LinkAttributesReader(logger, context, documentStream, resource, resourceAssembly, xmlDocumentLocation, featureSwitchValues); + var rdr = new LinkAttributesReader(logger, context, documentStream, resource, resourceAssembly, xmlDocumentLocation, featureSwitchValues, globalAttributeRemoval); rdr.ProcessXml(false); return rdr._removedAttributes; } diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs deleted file mode 100644 index 5ada498c03b113..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupLinkAttributesFile ("AssemblyLevelLinkerAttributeRemoval.xml")] - [IgnoreLinkAttributes (false)] - - [SetupCompileBefore ("library.dll", new[] { "Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs" })] - - [RemovedTypeInAssembly ("library.dll", "Mono.Linker.Tests.Cases.TestAttributeLib.MyAttribute")] - class AssemblyLevelLinkerAttributeRemoval - { - public static void Main () - { - new Mono.Linker.Tests.Cases.TestAttributeLib.Foo (); - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml deleted file mode 100644 index 40e22d677e645f..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/AssemblyLevelLinkerAttributeRemoval.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs deleted file mode 100644 index c5eeef15bd6ff4..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/AssemblyLevelLinkerAttributeRemoval_Lib.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using Mono.Linker.Tests.Cases.TestAttributeLib; - -[assembly: MyAttribute] -[module: MyAttribute] - -namespace Mono.Linker.Tests.Cases.TestAttributeLib -{ - [System.AttributeUsage (System.AttributeTargets.All, Inherited = false, AllowMultiple = true)] - public sealed class MyAttribute : System.Attribute - { - public MyAttribute () - { - } - } - - public class Foo - { - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs deleted file mode 100644 index 9943876b428495..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - public class EmbeddedAttributeErrorCases - { - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml deleted file mode 100644 index f5366f23b2ca0e..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/EmbeddedAttributeErrorCases.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml deleted file mode 100644 index d5ca5b732215b3..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs deleted file mode 100644 index aace5a4e2fa50e..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - public static class Used - { - public static void Use () { } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs deleted file mode 100644 index 4bd134ad9f936c..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - public class AttributeFromCopyAssemblyAttribute : Attribute - { - // Add all kinds of members - as they cause the type to be marked again - // and we don't want these to generate more warnings. - - private object _field; - - private class NestedType - { - } - - private int TestProperty { get; } - - private event EventHandler _event; - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs deleted file mode 100644 index 840397210bac98..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalAttributeToRemove.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Text; - -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - public enum TestAttributeUsedFromCopyAssemblyEnum - { - None - } - - public class TestAttributeUsedFromCopyAssemblyAttribute : Attribute - { - public TestAttributeUsedFromCopyAssemblyAttribute (TestAttributeUsedFromCopyAssemblyEnum n) - { - } - } - - public class TestAnotherAttributeUsedFromCopyAssemblyAttribute : Attribute - { - public TestAnotherAttributeUsedFromCopyAssemblyAttribute () - { - } - } - - public class TestAttributeReferencedAsTypeFromCopyAssemblyAttribute : Attribute - { - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs deleted file mode 100644 index f46724c69541f5..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalCopyAssembly.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.ComponentModel; -using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; - -[assembly: TestAnotherAttributeUsedFromCopyAssembly] - -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - [TestAttributeUsedFromCopyAssemblyAttribute (TestAttributeUsedFromCopyAssemblyEnum.None)] - [EditorBrowsable (EditorBrowsableState.Never)] - public class TypeOnCopyAssemblyWithAttributeUsage - { - public TypeOnCopyAssemblyWithAttributeUsage () - { - typeof (TestAttributeReferencedAsTypeFromCopyAssemblyAttribute).ToString (); - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs deleted file mode 100644 index 3b6cbbe6a23fc7..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; - -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - [EmbeddedAttributeToBeRemoved] - public class TypeWithEmbeddedAttributeToBeRemoved - { - public TypeWithEmbeddedAttributeToBeRemoved () { } - } - - public class EmbeddedAttributeToBeRemoved : Attribute - { - public EmbeddedAttributeToBeRemoved () { } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs deleted file mode 100644 index eb02e492f219c3..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/ReferencedAssemblyWithAttributes.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; - -[assembly: TestRemove] -[assembly: TestDontRemove] - -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - public class ReferencedAssemblyWithAttributes - { - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml deleted file mode 100644 index 17ef8e3aee84bd..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveAttribute.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs deleted file mode 100644 index d2bdbba86773e9..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/TestRemoveDontRemoveAttributes.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies -{ - public class TestRemoveAttribute : Attribute - { - public TestRemoveAttribute () - { - } - } - - public class TestDontRemoveAttribute : Attribute - { - public TestDontRemoveAttribute () - { - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs deleted file mode 100644 index b882e019ba9461..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.cs +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Diagnostics.CodeAnalysis; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Helpers; -using Mono.Linker.Tests.Cases.Expectations.Metadata; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SkipKeptItemsValidation] - [SetupCompileResource ("EmbeddedLinkAttributes.xml", "ILLink.LinkAttributes.xml")] - [IgnoreLinkAttributes (false)] - [RemovedResourceInAssembly ("test.exe", "ILLink.LinkAttributes.xml")] - [ExpectedNoWarnings] - class EmbeddedLinkAttributes - { - public static void Main () - { - var instance = new EmbeddedLinkAttributes (); - - instance.ReadFromInstanceField (); - instance.ReadFromInstanceField2 (); - } - - Type _typeWithPublicParameterlessConstructor; - - [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicConstructors))] - [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresNonPublicConstructors))] - [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicParameterlessConstructor), ProducedBy = ProducedBy.NativeAot)] - private void ReadFromInstanceField () - { - _typeWithPublicParameterlessConstructor.RequiresPublicParameterlessConstructor (); - _typeWithPublicParameterlessConstructor.RequiresPublicConstructors (); - _typeWithPublicParameterlessConstructor.RequiresNonPublicConstructors (); - } - - Type _typeWithPublicFields; - - [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicConstructors))] - [ExpectedWarning ("IL2077", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.NativeAot)] - private void ReadFromInstanceField2 () - { - _typeWithPublicFields.RequiresPublicConstructors (); - _typeWithPublicFields.RequiresPublicFields (); - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml deleted file mode 100644 index decb3699ad61f6..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributes.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - PublicParameterlessConstructor - - - - - - - - PublicFields - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs deleted file mode 100644 index b6aee070286252..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; -using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupCompileBefore ("attributes.dll", new string[] { "Dependencies/TestRemoveDontRemoveAttributes.cs" }, - resources: new object[] { new string[] { "Dependencies/TestRemoveAttribute.xml", "ILLink.LinkAttributes.xml" } })] - [IgnoreLinkAttributes (false)] - class EmbeddedLinkAttributesInReferencedAssembly - { - public static void Main () - { - TestAttributeRemoval (); - } - - [Kept] - [KeptAttributeAttribute (typeof (TestDontRemoveAttribute))] - [TestDontRemove] - [TestRemove] - public static void TestAttributeRemoval () { } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs deleted file mode 100644 index 5111a00f06c007..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; -using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupCompileBefore ("attributes.dll", new string[] { "Dependencies/TestRemoveDontRemoveAttributes.cs" }, - resources: new object[] { new string[] { "Dependencies/TestRemoveAttribute.xml", "ILLink.LinkAttributes.xml" } }, addAsReference: false)] - [SetupCompileBefore ("library.dll", new string[] { "Dependencies/ReferencedAssemblyWithAttributes.cs" }, references: new string[] { "attributes.dll" })] - [SetupLinkerAction ("copyused", "attributes")] // Ensure that assembly-level attributes are kept - [IgnoreLinkAttributes (false)] - [KeptAttributeInAssembly ("library.dll", "Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TestDontRemoveAttribute")] - [RemovedAttributeInAssembly ("library.dll", "Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TestRemoveAttribute")] - class EmbeddedLinkAttributesInReferencedAssembly_AssemblyLevel - { - public static void Main () - { - ReferenceOtherAssembly (); - } - - [Kept] - public static void ReferenceOtherAssembly () - { - var _1 = new ReferencedAssemblyWithAttributes (); - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs deleted file mode 100644 index 91938c375f96da..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; -using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupLinkAttributesFile ("LinkAttributeErrorCases.xml")] - [IgnoreLinkAttributes (false)] - [SetupLinkerArgument ("--skip-unresolved", "true")] - [SetupCompileBefore ("library.dll", new string[] { "Dependencies/EmbeddedAttributeErrorCases.cs" }, - resources: new object[] { new string[] { "Dependencies/EmbeddedAttributeErrorCases.xml", "ILLink.LinkAttributes.xml" } })] - - [ExpectedWarning ("IL2007", "NonExistentAssembly2", FileName = "LinkAttributeErrorCases.xml", SourceLine = 67, SourceColumn = 4)] - [ExpectedWarning ("IL2030", "NonExistentAssembly1", FileName = "LinkAttributeErrorCases.xml", SourceLine = 6, SourceColumn = 8)] - [ExpectedWarning ("IL2030", "MalformedAssemblyName, thisiswrong", FileName = "LinkAttributeErrorCases.xml", SourceLine = 7, SourceColumn = 8)] - [ExpectedWarning ("IL2031", "NonExistentAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 10, SourceColumn = 8)] - [ExpectedWarning ("IL2022", "AttributeWithNoParametersAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 13, SourceColumn = 8)] - [ExpectedWarning ("IL2022", "AttributeWithEnumParameterAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 18, SourceColumn = 8)] - [ExpectedWarning ("IL2022", "AttributeWithIntParameterAttribute", FileName = "LinkAttributeErrorCases.xml", SourceLine = 23, SourceColumn = 8)] - [ExpectedWarning ("IL2023", "GetTypeMethod", FileName = "LinkAttributeErrorCases.xml", SourceLine = 47, SourceColumn = 10)] - [ExpectedWarning ("IL2024", "methodParameter", "MethodWithParameter", FileName = "LinkAttributeErrorCases.xml", SourceLine = 57, SourceColumn = 10)] - [ExpectedWarning ("IL2029", FileName = "LinkAttributeErrorCases.xml", SourceLine = 64, SourceColumn = 6)] - [ExpectedWarning ("IL2029", FileName = "LinkAttributeErrorCases.xml", SourceLine = 65, SourceColumn = 6)] - [ExpectedWarning ("IL2051", FileName = "LinkAttributeErrorCases.xml", SourceLine = 29, SourceColumn = 10)] - [ExpectedWarning ("IL2052", "NonExistentPropertyName", FileName = "LinkAttributeErrorCases.xml", SourceLine = 34, SourceColumn = 10)] - [ExpectedWarning ("IL2100", FileName = "ILLink.LinkAttributes.xml", SourceLine = 3, SourceColumn = 4)] - [ExpectedWarning ("IL2101", "library", "test", FileName = "ILLink.LinkAttributes.xml", SourceLine = 5, SourceColumn = 4)] - [ExpectedNoWarnings] - class LinkAttributeErrorCases - { - public static void Main () - { - var _ = new EmbeddedAttributeErrorCases (); - } - - public enum AttributeEnum - { - None - } - - public class AttributeWithEnumParameterAttribute : Attribute - { - public AttributeWithEnumParameterAttribute (AttributeEnum enumValue) - { - } - } - - public class AttributeWithIntParameterAttribute : Attribute - { - public AttributeWithIntParameterAttribute (int intValue) - { - } - } - - public class AttributeWithNoParametersAttribute : Attribute - { - public AttributeWithNoParametersAttribute () - { - } - } - - public class AttributeWithPropertyAttribute : Attribute - { - public AttributeWithPropertyAttribute () - { - } - - int IntProperty { get; } - } - - public class FirstAttribute : Attribute { } - public class SecondAttribute : Attribute { } - - public Type GetTypeMethod () => null; - - public void MethodWithParameter (int methodParameter) { } - - public class ReferencedFromOtherAssembly - { - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml deleted file mode 100644 index 297f8fed94d73c..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkAttributeErrorCases.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - - - - - - - - ExtraArgumentValue - - - - - NonExistentEnumValue - - - - - NotANumber - - - - - StringValue - - - - - StringValue - - - - - StringValue - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs deleted file mode 100644 index 2475dd4f4027d9..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.cs +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Helpers; -using Mono.Linker.Tests.Cases.Expectations.Metadata; -using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupLinkAttributesFile ("LinkerAttributeRemoval.xml")] - [IgnoreLinkAttributes (false)] - - [SetupCompileBefore ("attribute.dll", new[] { "Dependencies/LinkerAttributeRemovalAttributeToRemove.cs" })] - [SetupCompileBefore ("copyattribute.dll", new[] { "Dependencies/LinkerAttributeRemovalAttributeFromCopyAssembly.cs" })] - [SetupLinkerAction ("copy", "copyattribute")] -#if !NETCOREAPP - [Reference ("System.dll")] - [SetupCompileBefore ("copyassembly.dll", new[] { "Dependencies/LinkerAttributeRemovalCopyAssembly.cs" }, references: new[] { "System.dll", "attribute.dll" })] -#else - [SetupCompileBefore ("copyassembly.dll", new[] { "Dependencies/LinkerAttributeRemovalCopyAssembly.cs" }, references: new[] { "attribute.dll" })] -#endif - [SetupLinkerAction ("copy", "copyassembly")] - - [SetupCompileBefore ( - "LinkerAttributeRemovalEmbeddedAndLazyLoad.dll", - new[] { "Dependencies/LinkerAttributeRemovalEmbeddedAndLazyLoad.cs" })] - - // The test here is that the TypeOnCopyAssemblyWithAttributeUsage has an attribute TestAttributeUsedFromCopyAssemblyAttribute - // which is marked for removal by the LinkerAttributeRemoval.xml. Normally that would mean that the attribute usage - // as well as the type (and assembly since it's the only type in it) would be removed as there are no other usages of the attribute type. - // But because the copyassembly is linked with "copy" action, the attribute usage should not be removed and thus the attribute - // should be kept. - [KeptAssembly ("copyassembly.dll")] - [KeptAssembly ("attribute.dll")] - [KeptTypeInAssembly ("attribute.dll", typeof (TestAttributeUsedFromCopyAssemblyAttribute))] - [KeptTypeInAssembly ("attribute.dll", typeof (TestAnotherAttributeUsedFromCopyAssemblyAttribute))] - [KeptTypeInAssembly ("attribute.dll", typeof (TestAttributeReferencedAsTypeFromCopyAssemblyAttribute))] - [KeptTypeInAssembly ("copyassembly.dll", typeof (TypeOnCopyAssemblyWithAttributeUsage))] - [KeptAttributeInAssembly ("copyassembly.dll", typeof (TestAttributeUsedFromCopyAssemblyAttribute), typeof (TypeOnCopyAssemblyWithAttributeUsage))] - [KeptAttributeInAssembly ("copyassembly.dll", typeof (EditorBrowsableAttribute), typeof (TypeOnCopyAssemblyWithAttributeUsage))] - [KeptAttributeInAssembly ("copyassembly.dll", typeof (TestAnotherAttributeUsedFromCopyAssemblyAttribute))] - - [KeptAssembly ("copyattribute.dll")] - [KeptTypeInAssembly ("copyattribute.dll", typeof (AttributeFromCopyAssemblyAttribute))] - - [KeptAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad.dll")] - [KeptTypeInAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad.dll", typeof (TypeWithEmbeddedAttributeToBeRemoved))] - // This needs to fixed with lazy loading assembly refactoring - currently the assembly="*" only applies to assemblies in initial closure - // The attribute should be removed and not kept as it is now - // [RemovedAttributeInAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad.dll", typeof (EmbeddedAttributeToBeRemoved), typeof (TypeWithEmbeddedAttributeToBeRemoved))] - [KeptAttributeInAssembly ("LinkerAttributeRemovalEmbeddedAndLazyLoad", typeof (EmbeddedAttributeToBeRemoved), typeof (TypeWithEmbeddedAttributeToBeRemoved))] - - [LogContains ("IL2045: Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TypeOnCopyAssemblyWithAttributeUsage.TypeOnCopyAssemblyWithAttributeUsage(): Attribute 'Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TestAttributeReferencedAsTypeFromCopyAssemblyAttribute'")] - [LogDoesNotContain ("IL2045")] // No other 2045 messages should be logged - - [ExpectedWarning ("IL2049", "'InvalidInternal'", FileName = "LinkerAttributeRemoval.xml")] - [ExpectedWarning ("IL2048", "RemoveAttributeInstances", FileName = "LinkerAttributeRemoval.xml")] - - [ExpectedNoWarnings] - - [KeptMember (".ctor()")] - class LinkerAttributeRemoval - { - public static void Main () - { - var instance = new LinkerAttributeRemoval (); - instance._fieldWithCustomAttribute = null; - string value = instance.methodWithCustomAttribute (null); - TestType (); - - _ = new TypeOnCopyAssemblyWithAttributeUsage (); - TestAttributeUsageRemovedEvenIfAttributeIsKeptForOtherReasons (); - - TestAttributeFromCopyAssembly (); - - TestEmbeddedAttributeLazyLoadRemoved (); - - TestMarkAllRemove (); - } - -#pragma warning disable CS0414 - [Kept] - [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] - Type _fieldWithCustomAttribute; -#pragma warning restore CS0414 - - [Kept] - [KeptAttributeAttribute (typeof (TestDontRemoveAttribute))] - [TestDontRemoveAttribute] - [TestRemoveAttribute] - [return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] - private string methodWithCustomAttribute ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] string parameterWithCustomAttribute) - { - return null; - } - - [ExpectedWarning ("IL2045", "Attribute 'System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute'")] - [Kept] - public static void TestType () - { - const string reflectionTypeKeptString = "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute"; - var typeKept = Type.GetType (reflectionTypeKeptString, false); - } - - [Kept] - [TestAttributeUsedFromCopyAssembly (TestAttributeUsedFromCopyAssemblyEnum.None)] - static void TestAttributeUsageRemovedEvenIfAttributeIsKeptForOtherReasons () - { - } - - [Kept] - [AttributeFromCopyAssembly] - static void TestAttributeFromCopyAssembly () - { - } - - [Kept] - // This attribute should be removed once the assembly lazy loading is implemented - [DynamicDependency ( - DynamicallyAccessedMemberTypes.PublicConstructors, - "Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TypeWithEmbeddedAttributeToBeRemoved", - "LinkerAttributeRemovalEmbeddedAndLazyLoad")] - static void TestEmbeddedAttributeLazyLoadRemoved () - { - // This needs the DynamicDependency above otherwise linker will not load the assembly at all - Activator.CreateInstance (Type.GetType ("Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.TypeWithEmbeddedAttributeToBeRemoved, LinkerAttributeRemovalEmbeddedAndLazyLoad")); - } - - [ExpectedWarning ("IL2045")] - [Kept] - static void TestMarkAllRemove () - { - Type.GetType ("Mono.Linker.Tests.Cases.LinkAttributes.TestMarkAllRemoveAttribute").RequiresAll (); - } - } - - [KeptBaseType (typeof (System.Attribute))] - class TestDontRemoveAttribute : Attribute - { - [Kept] - public TestDontRemoveAttribute () - { - } - } - - class TestRemoveAttribute : Attribute - { - public TestRemoveAttribute () - { - } - } - - [KeptBaseType (typeof (System.Attribute))] - [KeptMember (".ctor()")] - class TestMarkAllRemoveAttribute : Attribute - { - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml deleted file mode 100644 index de2c080446b00e..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemoval.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml deleted file mode 100644 index 095f6eb235e7f3..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs deleted file mode 100644 index ab48d5fb932757..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalAndPreserveAssembly.cs +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; -using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [IgnoreDescriptors (false)] - - // The test verifies that removed attributes which are later on kept due to descriptors correctly warn. - // The setup is: - // - test assembly with the AttributeToRemoveAttribute type - // - link attributes.xml which marks the attribute for removal (applied early, in this case via command line, but could be a embedded in the test assembly) - // - the attribute is used by the test assembly - // - another assembly lib.dll is added and is referenced (after the attribute is used/marked) - // - This new assembly has a descriptor which marks the entire test assembly (note that it marks the TEST assembly) - // - This marking causes the warning, as it's an explicit request to keep the attribute which was supposed to be removed - - [SetupLinkAttributesFile ("LinkerAttributeRemovalAndPreserveAssembly.LinkAttributes.xml")] - - [SetupCompileBefore ( - "lib.dll", - new[] { "Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.cs" }, - resources: new object[] { new string[] { "Dependencies/LinkerAttributeRemovalAndPreserveAssembly_Lib.Descriptor.xml", "ILLink.Descriptors.xml" } })] - - [ExpectedWarning ("IL2045", FileName = "ILLink.Descriptors.xml in lib, Version=0.0.0.0, Culture=neutral, PublicKeyToken")] - [ExpectedNoWarnings] - - [KeptMember (".ctor()")] - class LinkerAttributeRemovalAndPreserveAssembly - { - public static void Main () - { - TestAttributeRemoval (); - } - - [AttributeToRemoveAttribute] - [Kept] - static void TestAttributeRemoval () - { - Used.Use (); - } - } - - // The attribute is kept (partially) because we found out about it too late - // It does report a warning though - [Kept] - [KeptBaseType (typeof (Attribute))] - [KeptMember (".ctor()")] - public class AttributeToRemoveAttribute : Attribute { } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs deleted file mode 100644 index b7dc024459112b..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.cs +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Reflection; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupLinkAttributesFile ("LinkerAttributeRemovalConditional.xml")] - [IgnoreLinkAttributes (false)] - class LinkerAttributeRemovalConditional - { - public static void Main () - { - Kept_1 (); - Kept_2 (); - Kept_3 (); - Removed_1 (); - Removed_2 (); - Removed_3 (); - Removed_4 (); - Kept_4 (); - Kept_5 (); - Removed_5 (); - Removed_6 (); - Kept_6 (); - } - - [Kept] - [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] - [TestConditionalRemove ()] - static void Kept_1 () - { - } - - [Kept] - [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] - [TestConditionalRemove ("my-value", "string value")] - static void Kept_2 () - { - } - - [Kept] - [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] - [TestConditionalRemove (1, true)] - static void Kept_3 () - { - } - - [Kept] - [TestConditionalRemove ("remove0", "string value")] - static void Removed_1 () - { - } - - [Kept] - [TestConditionalRemove (100, '1')] - [TestConditionalRemove ("remove1", '1', 3)] - static void Removed_2 () - { - } - - [Kept] - [TestConditionalRemove ("remove0", 'k', 0)] // It's removed because the converted string value matches - static void Removed_3 () - { - } - - [Kept] - [TestConditionalRemove ("remove2", "remove3")] - static void Removed_4 () - { - } - - [Kept] - [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] - [TestConditionalRemove ("remove2", "unmatched second arg")] - static void Kept_4 () - { - } - - [Kept] - [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] - [TestConditionalRemove ((int) 99, "remove3")] - static void Kept_5 () // Kept because int arg in constructor doesn't match long arg in xml - { - } - - [Kept] - [TestConditionalRemove (72, "a", "b", "c", "d", "e")] - static void Removed_5 () - { - } - - [Kept] - [TestConditionalRemove (new int[] { 1, 2, 3 }, "remove4")] - static void Removed_6 () - { - } - - [Kept] - [KeptAttributeAttribute (typeof (TestConditionalRemoveAttribute))] - [TestConditionalRemove (new int[] { 1, 2, 3, 4 }, "remove4")] - static void Kept_6 () - { - } - } - - [Kept] - [KeptBaseType (typeof (System.Attribute))] - [KeptAttributeAttribute (typeof (AttributeUsageAttribute))] - [AttributeUsage (AttributeTargets.All, AllowMultiple = true)] - class TestConditionalRemoveAttribute : Attribute - { - [Kept] - public TestConditionalRemoveAttribute () - { - } - - [Kept] - // Any usage with "remove0" key is removed - public TestConditionalRemoveAttribute (string key, string value) - { - } - - // Any usage with 100 key is removed - // Any usage with "remove1" key is removed - public TestConditionalRemoveAttribute (object key, char value, int ivalue) - { - } - - [Kept] - public TestConditionalRemoveAttribute (int key, object value) - { - } - - public TestConditionalRemoveAttribute (int key, [KeptAttributeAttribute (typeof (ParamArrayAttribute))] params object[] values) - { - } - - [Kept] - public TestConditionalRemoveAttribute (int[] intArray, string str) - { - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml deleted file mode 100644 index 70518eb7939054..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalConditional.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - remove1 - - - - - - - 100 - - - - - - - remove0 - - - - - remove2 - remove3 - - - - - - 99 - - - remove3 - - - - - - - - - 72 - - - - a - b - c - d - e - - - - - - - - 1 - 2 - 3 - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml deleted file mode 100644 index ee3aa79a481125..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/LinkerAttributeRemovalWithOverride.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs deleted file mode 100644 index a055cf6b97f2ff..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.Reflection; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupLinkAttributesFile ("LinkerAttributeRemovalWithOverride.xml")] - [SetupLinkerDescriptorFile ("OverrideAttributeRemoval.xml")] - [IgnoreLinkAttributes (false)] - [KeptMember (".ctor()")] - class OverrideAttributeRemoval - { - public static void Main () - { - var instance = new OverrideAttributeRemoval (); - instance._fieldWithCustomAttribute = null; - string value = instance.methodWithCustomAttribute ("parameter"); - } - [Kept] - [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] - [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] - Type _fieldWithCustomAttribute; - - [Kept] - [return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] - [return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] - private string methodWithCustomAttribute ( - [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] - [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)] - string parameterWithCustomAttribute) - { - return "this is a return value"; - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml deleted file mode 100644 index 3991969c573718..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/OverrideAttributeRemoval.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs deleted file mode 100644 index c89948c37bad32..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupLinkAttributesFile ("TypedArguments.xml")] - [IgnoreLinkAttributes (false)] - [SetupLinkerArgument ("--verbose")] - - [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.ObjectAttribute.ObjectAttribute(Object) { args: System.Object Mono.Cecil.CustomAttributeArgument }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field1'")] - [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.ObjectAttribute.ObjectAttribute(Object) { args: System.Object Mono.Cecil.CustomAttributeArgument }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field2'")] - [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.EnumAttribute.EnumAttribute(TypedArgumentsEnumA) { args: Mono.Linker.Tests.Cases.LinkAttributes.TypedArgumentsEnumA 3 }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field3'")] - [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.ByteAttribute.ByteAttribute(Byte) { args: System.Byte 6 }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field4'")] - [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.StringAttribute.StringAttribute(String) { args: System.String str }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field5'")] - [LogContains ("Assigning external custom attribute 'Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments.TypeAttribute.TypeAttribute(Type) { args: System.Type System.DateTime }' instance to 'System.Object Mono.Linker.Tests.Cases.LinkAttributes.TypedArguments::field6'")] - class TypedArguments - { - [Kept] - static object field1; - - [Kept] - static object field2; - - [Kept] - static object field3; - - [Kept] - static object field4; - - [Kept] - static object field5; - - [Kept] - static object field6; - - public static void Main () - { - field1 = null; - field2 = null; - field3 = null; - field4 = null; - field5 = null; - field6 = null; - } - - public class ObjectAttribute : Attribute - { - public ObjectAttribute (object objectValue) - { - } - - public ObjectAttribute (string stringValue) - { - } - } - - public class ByteAttribute : Attribute - { - public ByteAttribute (uint intValue) - { - } - - public ByteAttribute (byte byteValue) - { - } - - public ByteAttribute (object objectValue) - { - } - } - - public class EnumAttribute : Attribute - { - public EnumAttribute (int intValue) - { - } - - public EnumAttribute (object objectValue) - { - } - - public EnumAttribute (TypedArgumentsEnumA enumA) - { - } - - public EnumAttribute (TypedArgumentsEnumB enumB) - { - } - } - - public class StringAttribute : Attribute - { - public StringAttribute (string stringValue) - { - } - - public StringAttribute (object objectValue) - { - } - } - - public class TypeAttribute : Attribute - { - public TypeAttribute (string stringValue) - { - } - - public TypeAttribute (Type typeValue) - { - } - } - } - - enum TypedArgumentsEnumA - { - Value = 2 - } - - enum TypedArgumentsEnumB - { - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml deleted file mode 100644 index 3340054bb68d91..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArguments.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - StringValue - - - - - - - 9 - - - - - - 3 - - - - - 6 - - - - - str - - - - - System.DateTime - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs deleted file mode 100644 index 44513d83e07bab..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using Mono.Linker.Tests.Cases.Expectations.Assertions; -using Mono.Linker.Tests.Cases.Expectations.Metadata; - -namespace Mono.Linker.Tests.Cases.LinkAttributes -{ - [SetupLinkAttributesFile ("TypedArgumentsErrors.xml")] - [IgnoreLinkAttributes (false)] - [NoLinkedOutput] - - [LogContains ("The type 'System.NoOBJECT' used with attribute value 'str' could not be found")] - [LogContains ("Cannot convert value 'str' to type 'System.Int32'")] - [LogContains ("Custom attribute argument for 'System.Object' requires nested 'argument' node")] - [LogContains ("The type 'System.Bar' used with attribute value 'str4' could not be found")] - [LogContains ("Could not resolve custom attribute type value 'str5'")] - class TypedArgumentsErrors - { - static object field; - - public static void Main () - { - } - - public class ObjectAttribute : Attribute - { - public ObjectAttribute (object objectValue) - { - } - - public ObjectAttribute (string stringValue) - { - } - } - } -} diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml b/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml deleted file mode 100644 index 0b870b9b9c882d..00000000000000 --- a/src/coreclr/tools/aot/Mono.Linker.Tests.Cases/LinkAttributes/TypedArgumentsErrors.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - str - - - - - str - - - - - str - - - - - str4 - - - - - str5 - - - - - diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs index 55c826adb79c62..de77b4e4d8621c 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestDatabase.cs @@ -24,11 +24,6 @@ public static IEnumerable DynamicDependencies () return TestNamesBySuiteName (); } - public static IEnumerable LinkAttributes () - { - return TestNamesBySuiteName (); - } - public static IEnumerable LinkXml() { return TestNamesBySuiteName(); diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs index 41df23866af530..e34ce26045a1f9 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCases/TestSuites.cs @@ -24,13 +24,6 @@ public void DynamicDependencies (string t) Run (t); } - [Theory] - [MemberData (nameof (TestDatabase.LinkAttributes), MemberType = typeof (TestDatabase))] - public void LinkAttributes (string t) - { - Run (t); - } - [Theory] [MemberData (nameof (TestDatabase.LinkXml), MemberType = typeof (TestDatabase))] public void LinkXml (string t) diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs index 3aacaf5e9f8340..fa245abdc210a7 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs @@ -67,13 +67,6 @@ public ILScanResults Trim (ILCompilerOptions options, ILogWriter logWriter) Logger logger = new Logger (logWriter, ilProvider, isVerbose: true); - HashSet removedAttributes = new HashSet (); - foreach (var linkAttributes in options.LinkAttributes) { - if (!File.Exists (linkAttributes)) - throw new FileNotFoundException ($"'{linkAttributes}' doesn't exist"); - _ = removedAttributes; - } - foreach (var descriptor in options.Descriptors) { if (!File.Exists (descriptor)) throw new FileNotFoundException ($"'{descriptor}' doesn't exist"); diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs index ba7d25632db84e..5b48963927e19e 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptions.cs @@ -14,7 +14,6 @@ public class ILCompilerOptions public List AdditionalRootAssemblies = new List (); public Dictionary FeatureSwitches = new Dictionary (); public List Descriptors = new List (); - public List LinkAttributes = new List (); public bool FrameworkCompilation; } } diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs index 680efd8074f625..afb660e9b7bc9d 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerOptionsBuilder.cs @@ -156,7 +156,6 @@ public virtual void AddSubstitutions (string file) public virtual void AddLinkAttributes (string file) { - Options.LinkAttributes.Add (file); } public virtual void AddAdditionalArgument (string flag, string[] values) From 65e887a36b3f72ddad152493e7ab52fc56d0eb59 Mon Sep 17 00:00:00 2001 From: Tlakollo Date: Mon, 30 Jan 2023 18:27:33 -0800 Subject: [PATCH 3/6] Add a way to only process star/nonstar assemblies in ProcessLinkerXmlBase Add test --- .../Common/Compiler/ProcessLinkerXmlBase.cs | 10 +- .../Compiler/UsageBasedMetadataManager.cs | 17 +++- .../ILLink.LinkAttributes.xml | 16 +++ .../TrimmingBehaviors/ILLinkLinkAttributes.cs | 98 +++++++++++++++++++ .../TrimmingBehaviors.csproj | 5 + 5 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLink.LinkAttributes.xml create mode 100644 src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs diff --git a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs index a6b560943b9254..c576c70e2ba3f7 100644 --- a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs +++ b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs @@ -55,6 +55,7 @@ public abstract class ProcessLinkerXmlBase private readonly XPathNavigator _document; private readonly Logger _logger; protected readonly ModuleDesc? _owningModule; + protected readonly bool _globalAttributeRemoval; private readonly IReadOnlyDictionary _featureSwitchValues; protected readonly TypeSystemContext _context; @@ -70,10 +71,11 @@ protected ProcessLinkerXmlBase(Logger logger, TypeSystemContext context, Stream _featureSwitchValues = featureSwitchValues; } - protected ProcessLinkerXmlBase(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues) + protected ProcessLinkerXmlBase(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval = false) : this(logger, context, documentStream, xmlDocumentLocation, featureSwitchValues) { _owningModule = resourceAssembly; + _globalAttributeRemoval = globalAttributeRemoval; } protected virtual bool ShouldProcessElement(XPathNavigator nav) => FeatureSettings.ShouldProcessElement(nav, _featureSwitchValues); @@ -160,11 +162,15 @@ protected virtual void ProcessAssemblies(XPathNavigator nav) if (!ShouldProcessElement(assemblyNav)) continue; - if (processAllAssemblies) + if (processAllAssemblies && _globalAttributeRemoval) { Debug.Assert(_owningModule != null); ProcessAssembly(_owningModule, assemblyNav, warnOnUnresolvedTypes: false); } + else if (processAllAssemblies ^ _globalAttributeRemoval) + { + continue; + } else { Debug.Assert(!processAllAssemblies); diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs index 987547c08bcc07..32e860c658f0ed 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs @@ -1176,21 +1176,28 @@ internal sealed class LinkAttributesReader : ProcessLinkerXmlBase { private readonly HashSet _removedAttributes = new(); private readonly ModuleDesc _resource; - private readonly bool _globalAttributeRemoval; public LinkAttributesReader(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval) - : base(logger, context, documentStream, resource, resourceAssembly, xmlDocumentLocation, featureSwitchValues) + : base(logger, context, documentStream, resource, resourceAssembly, xmlDocumentLocation, featureSwitchValues, globalAttributeRemoval) { _resource = resourceAssembly; - _globalAttributeRemoval = globalAttributeRemoval; } + private static bool IsRemoveAttributeInstances(string attributeName) => attributeName == "RemoveAttributeInstances" || attributeName == "RemoveAttributeInstancesAttribute"; + private void ProcessAttribute(TypeDesc type, XPathNavigator nav) { string internalValue = GetAttribute(nav, "internal"); - if (internalValue == "RemoveAttributeInstances" && nav.IsEmptyElement) + if (!string.IsNullOrEmpty(internalValue)) { - _removedAttributes.Add(type); + if (!IsRemoveAttributeInstances(internalValue) || !nav.IsEmptyElement) + { + LogWarning(nav, DiagnosticId.UnrecognizedInternalAttribute, internalValue); + } + if (IsRemoveAttributeInstances(internalValue) && nav.IsEmptyElement) + { + _removedAttributes.Add(type); + } } } diff --git a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLink.LinkAttributes.xml b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLink.LinkAttributes.xml new file mode 100644 index 00000000000000..83e579f7285b77 --- /dev/null +++ b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLink.LinkAttributes.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs new file mode 100644 index 00000000000000..1f8bfd60c8de7b --- /dev/null +++ b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs @@ -0,0 +1,98 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Diagnostics.CodeAnalysis; + +using BindingFlags = System.Reflection.BindingFlags; + +class ILLinkLinkAttributes +{ + public static int Run() + { + ThrowIfTypeNotPresent(typeof(ILLinkLinkAttributes), nameof(TestDontRemoveAttribute)); + ThrowIfTypePresent(typeof(ILLinkLinkAttributes), nameof(TestRemoveAttribute)); + ThrowIfTypePresent(typeof(ILLinkLinkAttributes), nameof(TestMarkAllRemoveAttribute)); + ThrowIfAttributePresent(typeof(ILLinkLinkAttributes), nameof(_fieldWithCustomAttribute), nameof(TestRemoveAttribute)); + ThrowIfAttributeNotPresent(typeof(ILLinkLinkAttributes), nameof(_fieldWithCustomAttribute), nameof(TestDontRemoveAttribute)); + ThrowIfAttributePresent(typeof(ILLinkLinkAttributes), nameof(_fieldWithCustomAttribute), nameof(AllowNullAttribute)); + return 100; + } + + [TestDontRemoveAttribute] + [TestRemoveAttribute] + [AllowNullAttribute] + private string _fieldWithCustomAttribute = "Hello world"; + + class TestDontRemoveAttribute : Attribute + { + public TestDontRemoveAttribute () + { + } + } + + class TestRemoveAttribute : Attribute + { + public TestRemoveAttribute () + { + } + } + + class TestMarkAllRemoveAttribute : Attribute + { + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", + Justification = "That's the point")] + private static bool IsTypePresent(Type testType, string typeName) => testType.GetNestedType(typeName, BindingFlags.NonPublic | BindingFlags.Public) != null; + + private static void ThrowIfTypeNotPresent(Type testType, string typeName) + { + if (!IsTypePresent(testType, typeName)) + { + throw new Exception(typeName); + } + } + + private static void ThrowIfTypePresent(Type testType, string typeName) + { + if (IsTypePresent(testType, typeName)) + { + throw new Exception(typeName); + } + } + + private static bool IsAttributePresent(Type testType, string memberName, string attributeName) + { + foreach (var member in testType.GetMembers()) + { + if (member.Name == memberName) + { + foreach (var attribute in member.GetCustomAttributes(false)) + { + if (attribute.GetType().Name == attributeName) + { + return true; + } + } + } + } + return false; + } + + private static void ThrowIfAttributeNotPresent(Type testType, string memberName, string attributeName) + { + if (!IsAttributePresent(testType, memberName, attributeName)) + { + throw new Exception(attributeName); + } + } + + private static void ThrowIfAttributePresent(Type testType, string memberName, string attributeName) + { + if (IsAttributePresent(testType, memberName, attributeName)) + { + throw new Exception(attributeName); + } + } +} diff --git a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/TrimmingBehaviors.csproj b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/TrimmingBehaviors.csproj index d324f0ab841ad0..799e3a9afa2fdc 100644 --- a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/TrimmingBehaviors.csproj +++ b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/TrimmingBehaviors.csproj @@ -4,6 +4,7 @@ BuildAndRun 0 true + false true @@ -12,11 +13,15 @@ + + + ILLink.LinkAttributes.xml + ILLink.Substitutions.xml From 7612cf32126ae56064f5e140a45ee18b840c1625 Mon Sep 17 00:00:00 2001 From: Tlakollo Date: Tue, 31 Jan 2023 12:12:23 -0800 Subject: [PATCH 4/6] Run formatter Apply feedback Avoid using LogWarning in ReadyToRun --- .../Common/Compiler/ProcessLinkerXmlBase.cs | 6 ++-- .../Compiler/UsageBasedMetadataManager.cs | 21 +++++-------- .../TrimmingBehaviors/ILLinkLinkAttributes.cs | 30 +++++++++---------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs index c576c70e2ba3f7..ab3b8f9e05b660 100644 --- a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs +++ b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs @@ -140,7 +140,9 @@ protected virtual void ProcessAssemblies(XPathNavigator nav) bool processAllAssemblies = ShouldProcessAllAssemblies(assemblyNav, out AssemblyName? name); if (processAllAssemblies && AllowedAssemblySelector != AllowedAssemblies.AllAssemblies) { +#if !READYTORUN LogWarning(assemblyNav, DiagnosticId.XmlUnsuportedWildcard); +#endif continue; } @@ -471,8 +473,8 @@ protected virtual void ProcessProperty(TypeDesc type, XPathNavigator nav, object bool foundMatch = false; foreach (PropertyPseudoDesc property in type.GetPropertiesOnTypeHierarchy(p => p.Name == name)) { - foundMatch = true; - ProcessProperty(type, property, nav, customData, false); + foundMatch = true; + ProcessProperty(type, property, nav, customData, false); } if (!foundMatch) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs index 32e860c658f0ed..9ebbecdae30f3a 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs @@ -39,7 +39,7 @@ public sealed class UsageBasedMetadataManager : GeneratingMetadataManager internal readonly UsageBasedMetadataGenerationOptions _generationOptions; - private readonly LinkAttributesHashTable _featureSwitchHashtable; + private readonly LinkAttributesHashTable _linkAttributesHashTable; private static (string AttributeName, DiagnosticId Id)[] _requiresAttributeMismatchNameAndId = new[] { @@ -91,7 +91,7 @@ public UsageBasedMetadataManager( FlowAnnotations = flowAnnotations; Logger = logger; - _featureSwitchHashtable = new LinkAttributesHashTable(Logger, new Dictionary(featureSwitchValues)); + _linkAttributesHashTable = new LinkAttributesHashTable(Logger, new Dictionary(featureSwitchValues)); FeatureSwitches = new Dictionary(featureSwitchValues); _rootEntireAssembliesModules = new HashSet(rootEntireAssembliesModules); @@ -844,7 +844,7 @@ public bool GeneratesAttributeMetadata(TypeDesc attributeType) var ecmaType = attributeType.GetTypeDefinition() as EcmaType; if (ecmaType != null) { - var moduleInfo = _featureSwitchHashtable.GetOrCreateValue(ecmaType.EcmaModule); + var moduleInfo = _linkAttributesHashTable.GetOrCreateValue(ecmaType.EcmaModule); return !moduleInfo.RemovedAttributes.Contains(ecmaType); } @@ -1134,9 +1134,9 @@ public AssemblyFeatureInfo(EcmaModule module, Logger logger, IReadOnlyDictionary ParseLinkAttributesXml(module, logger, featureSwitchValues, globalAttributeRemoval: true); } - public void ParseLinkAttributesXml(EcmaModule module, Logger logger, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval) + public void ParseLinkAttributesXml(EcmaModule module, Logger logger, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval) { - EcmaModule xmlModule = globalAttributeRemoval ? (EcmaModule) module.Context.SystemModule : module; + EcmaModule xmlModule = globalAttributeRemoval ? (EcmaModule)module.Context.SystemModule : module; PEMemoryBlock resourceDirectory = xmlModule.PEReader.GetSectionData(xmlModule.PEReader.PEHeaders.CorHeader.ResourcesDirectory.RelativeVirtualAddress); foreach (var resourceHandle in xmlModule.MetadataReader.ManifestResources) @@ -1167,20 +1167,13 @@ public void ParseLinkAttributesXml(EcmaModule module, Logger logger, IReadOnlyD } } - public static class PlatformAssemblies - { - public const string CoreLib = "System.Private.CoreLib"; - } - internal sealed class LinkAttributesReader : ProcessLinkerXmlBase { private readonly HashSet _removedAttributes = new(); - private readonly ModuleDesc _resource; public LinkAttributesReader(Logger logger, TypeSystemContext context, Stream documentStream, ManifestResource resource, ModuleDesc resourceAssembly, string xmlDocumentLocation, IReadOnlyDictionary featureSwitchValues, bool globalAttributeRemoval) : base(logger, context, documentStream, resource, resourceAssembly, xmlDocumentLocation, featureSwitchValues, globalAttributeRemoval) { - _resource = resourceAssembly; } private static bool IsRemoveAttributeInstances(string attributeName) => attributeName == "RemoveAttributeInstances" || attributeName == "RemoveAttributeInstancesAttribute"; @@ -1212,11 +1205,11 @@ protected override AllowedAssemblies AllowedAssemblySelector { get { - if (_resource?.Assembly == null) + if (_owningModule?.Assembly == null) return AllowedAssemblies.AllAssemblies; // Corelib XML may contain assembly wildcard to support compiler-injected attribute types - if (_resource?.Assembly.GetName().Name == PlatformAssemblies.CoreLib) + if (_owningModule?.Assembly == _context.SystemModule) return AllowedAssemblies.AllAssemblies; return AllowedAssemblies.ContainingAssembly; diff --git a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs index 1f8bfd60c8de7b..5cfaff7a765da3 100644 --- a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs +++ b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/ILLinkLinkAttributes.cs @@ -23,24 +23,24 @@ public static int Run() [TestRemoveAttribute] [AllowNullAttribute] private string _fieldWithCustomAttribute = "Hello world"; - + class TestDontRemoveAttribute : Attribute - { - public TestDontRemoveAttribute () - { - } - } + { + public TestDontRemoveAttribute() + { + } + } - class TestRemoveAttribute : Attribute - { - public TestRemoveAttribute () - { - } - } + class TestRemoveAttribute : Attribute + { + public TestRemoveAttribute() + { + } + } - class TestMarkAllRemoveAttribute : Attribute - { - } + class TestMarkAllRemoveAttribute : Attribute + { + } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "That's the point")] From c905faaa7fdf0492e70a4e1ca323a73fd4ad2d66 Mon Sep 17 00:00:00 2001 From: tlakollo Date: Tue, 31 Jan 2023 21:17:38 -0800 Subject: [PATCH 5/6] Ask early on if the assembly should be processed, no reason to do work before that reorg conditions to print warnings only if _globalAttributeRemoval is false Move assembly to process check for non star members only --- .../Common/Compiler/ProcessLinkerXmlBase.cs | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs index ab3b8f9e05b660..cc39535acd6595 100644 --- a/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs +++ b/src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs @@ -135,46 +135,46 @@ protected virtual void ProcessAssemblies(XPathNavigator nav) { foreach (XPathNavigator assemblyNav in nav.SelectChildren("assembly", "")) { + if (!ShouldProcessElement(assemblyNav)) + continue; + // Errors for invalid assembly names should show up even if this element will be // skipped due to feature conditions. bool processAllAssemblies = ShouldProcessAllAssemblies(assemblyNav, out AssemblyName? name); - if (processAllAssemblies && AllowedAssemblySelector != AllowedAssemblies.AllAssemblies) + if (processAllAssemblies && !_globalAttributeRemoval) { #if !READYTORUN - LogWarning(assemblyNav, DiagnosticId.XmlUnsuportedWildcard); + if (AllowedAssemblySelector != AllowedAssemblies.AllAssemblies) + LogWarning(assemblyNav, DiagnosticId.XmlUnsuportedWildcard); #endif continue; } - - ModuleDesc? assemblyToProcess = null; - if (!AllowedAssemblySelector.HasFlag(AllowedAssemblies.AnyAssembly)) + else if (!processAllAssemblies && _globalAttributeRemoval) { - Debug.Assert(!processAllAssemblies); - Debug.Assert(_owningModule != null); - if (_owningModule.Assembly.GetName().Name != name!.Name) - { -#if !READYTORUN - LogWarning(assemblyNav, DiagnosticId.AssemblyWithEmbeddedXmlApplyToAnotherAssembly, _owningModule.Assembly.GetName().Name ?? "", name.ToString()); -#endif - continue; - } - assemblyToProcess = _owningModule; - } - - if (!ShouldProcessElement(assemblyNav)) continue; - - if (processAllAssemblies && _globalAttributeRemoval) + } + else if (processAllAssemblies && _globalAttributeRemoval) { Debug.Assert(_owningModule != null); ProcessAssembly(_owningModule, assemblyNav, warnOnUnresolvedTypes: false); } - else if (processAllAssemblies ^ _globalAttributeRemoval) - { - continue; - } else { + ModuleDesc? assemblyToProcess = null; + if (!AllowedAssemblySelector.HasFlag(AllowedAssemblies.AnyAssembly)) + { + Debug.Assert(!processAllAssemblies); + Debug.Assert(_owningModule != null); + if (_owningModule.Assembly.GetName().Name != name!.Name) + { +#if !READYTORUN + LogWarning(assemblyNav, DiagnosticId.AssemblyWithEmbeddedXmlApplyToAnotherAssembly, _owningModule.Assembly.GetName().Name ?? "", name.ToString()); +#endif + continue; + } + assemblyToProcess = _owningModule; + } + Debug.Assert(!processAllAssemblies); ModuleDesc? assembly = assemblyToProcess ?? _context.ResolveAssembly(name!, false); From 02278659c5f5ec417b7119feb0d28ee3622d77bb Mon Sep 17 00:00:00 2001 From: Tlakollo Date: Wed, 8 Feb 2023 15:28:49 -0800 Subject: [PATCH 6/6] Actually fix the merge conflict --- .../TestCasesRunner/ILCompilerDriver.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs index 6fc77de3bc5be6..5181d1be700224 100644 --- a/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs +++ b/src/coreclr/tools/aot/Mono.Linker.Tests/TestCasesRunner/ILCompilerDriver.cs @@ -66,14 +66,6 @@ public ILScanResults Trim (ILCompilerOptions options, ILogWriter logWriter) ILProvider ilProvider = new NativeAotILProvider (); - Logger logger = new Logger (logWriter, ilProvider, isVerbose: true); - - foreach (var descriptor in options.Descriptors) { - if (!File.Exists (descriptor)) - throw new FileNotFoundException ($"'{descriptor}' doesn't exist"); - compilationRoots.Add (new ILCompiler.DependencyAnalysis.TrimmingDescriptorNode (descriptor)); - } - Logger logger = new Logger ( logWriter, ilProvider, @@ -83,8 +75,14 @@ public ILScanResults Trim (ILCompilerOptions options, ILogWriter logWriter) singleWarnEnabledModules: Enumerable.Empty (), singleWarnDisabledModules: Enumerable.Empty (), suppressedCategories: Enumerable.Empty ()); + + foreach (var descriptor in options.Descriptors) { + if (!File.Exists (descriptor)) + throw new FileNotFoundException ($"'{descriptor}' doesn't exist"); + compilationRoots.Add (new ILCompiler.DependencyAnalysis.TrimmingDescriptorNode (descriptor)); + } - ilProvider = new FeatureSwitchManager (ilProvider, logger, options.FeatureSwitches); + ilProvider = new FeatureSwitchManager (ilProvider, logger, options.FeatureSwitches); CompilerGeneratedState compilerGeneratedState = new CompilerGeneratedState (ilProvider, logger);