Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add override mechanism for RemoveAttributeInstances #1332

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/data-formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,6 @@ example:
</assembly>
</linker>
```
Notice that a descriptor file containing the custom attribute type overrides this behavior. In case the
custom attribute type is being referenced in a descriptor xml file and in the linkattributes xml file
for removal, the custom attribute will not be removed
2 changes: 1 addition & 1 deletion src/linker/Linker.Steps/LinkAttributesStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ IEnumerable<CustomAttribute> ProcessAttributes (XPathNavigator nav, ICustomAttri
TypeDefinition attributeType;

string internalAttribute = GetAttribute (iterator.Current, "internal");
if (internalAttribute == "RemoveAttributeInstances" && provider != null) {
if (internalAttribute == "RemoveAttributeInstances" && provider != null && !Annotations.IsMarked (provider)) {
IEnumerable<Attribute> removeAttributeInstance = new List<Attribute> { new RemoveAttributeInstancesAttribute () };
Context.CustomAttributes.AddInternalAttributes (provider, removeAttributeInstance);
}
Expand Down
2 changes: 1 addition & 1 deletion src/linker/Linker/LinkerAttributesInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ readonly struct LinkerAttributesInformation
{
readonly Dictionary<Type, List<Attribute>> _linkerAttributes;

public LinkerAttributesInformation (LinkContext context, ICustomAttributeProvider provider, bool removable = false)
public LinkerAttributesInformation (LinkContext context, ICustomAttributeProvider provider)
{
_linkerAttributes = null;
if (context.CustomAttributes.HasCustomAttributes (provider)) {
Expand Down
41 changes: 41 additions & 0 deletions test/Mono.Linker.Tests.Cases/DataFlow/OverrideAttributeRemoval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace Mono.Linker.Tests.Cases.DataFlow
{
[SetupLinkAttributesFile ("LinkerAttributeRemoval.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";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<linker>
<assembly fullname="Mono.Linker.Tests.Cases.Expectations">
<type fullname="System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute"/>
</assembly>
</linker>