Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitek Karas <vitek.karas@microsoft.com>2020-05-20 18:23:01 +0300
committerGitHub <noreply@github.com>2020-05-20 18:23:01 +0300
commit7d149f673b921ecc23372a5741082c9cd0a6b70a (patch)
tree356fb78247cf42609406155e3b6c9de4509e3ed8 /test/Mono.Linker.Tests.Cases.Expectations
parent3409a1a819e380c0ffeedfaaccc74f064b67b2ff (diff)
Implement support for RequiresUnreferencedCodeAttribute (#1204)
Introduces a cache of "linker attributes", currently only RequiresUnreferencedCodeAttribute is supported, but it's highly likely we will need this for other attributes as well (DynamicDependency, future AOT/Single-file related attributes, UnconditionalSuppressMessage, ...). The cache is on Annotations class and should also act as a natural point to unify attribute and XML based inputs (once we have the XML based attribute injection). RequiresUnreferencedCode now causes a linker warning whenever a method with it is called. It also auto-suppresses warnings within the method with this attribute - it actually disables data flow analysis on the method right now. In the future when we have AOT/Single-File related analysis we will need to modify this to selectively support a subset and only auto-suppress warnings related to the relevant subsets. Extends the LogContains/LogDoesNotContain test attributes so that they can appear on anything within the test class. Functionality is still the same as if they're on the class, but it makes it easier to structure the tests and make them more readable. Once we have solid warning origins, we could also auto-validate that the warning came from the method it is on. Added support for multiple attributes of the same type. Added logic to detect multiple instances of attribute where there should only be one - in this case linker will issue a warning and use the first instance only.
Diffstat (limited to 'test/Mono.Linker.Tests.Cases.Expectations')
-rw-r--r--test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogContainsAttribute.cs5
-rw-r--r--test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogDoesNotContainAttribute.cs5
-rw-r--r--test/Mono.Linker.Tests.Cases.Expectations/System.Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs43
3 files changed, 51 insertions, 2 deletions
diff --git a/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogContainsAttribute.cs b/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogContainsAttribute.cs
index 10a436a8e..1dd4daaa1 100644
--- a/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogContainsAttribute.cs
+++ b/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogContainsAttribute.cs
@@ -2,7 +2,10 @@ using System;
namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
- [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
+ [AttributeUsage (
+ AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Constructor | AttributeTargets.Field,
+ AllowMultiple = true,
+ Inherited = false)]
public class LogContainsAttribute : EnableLoggerAttribute
{
public LogContainsAttribute (string message, bool regexMatch = false)
diff --git a/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogDoesNotContainAttribute.cs b/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogDoesNotContainAttribute.cs
index 697623966..d4299fde1 100644
--- a/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogDoesNotContainAttribute.cs
+++ b/test/Mono.Linker.Tests.Cases.Expectations/Assertions/LogDoesNotContainAttribute.cs
@@ -2,7 +2,10 @@
namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
- [AttributeUsage (AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
+ [AttributeUsage (
+ AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Constructor | AttributeTargets.Field,
+ AllowMultiple = true,
+ Inherited = false)]
public class LogDoesNotContainAttribute : EnableLoggerAttribute
{
public LogDoesNotContainAttribute (string message)
diff --git a/test/Mono.Linker.Tests.Cases.Expectations/System.Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs b/test/Mono.Linker.Tests.Cases.Expectations/System.Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs
new file mode 100644
index 000000000..11e56f543
--- /dev/null
+++ b/test/Mono.Linker.Tests.Cases.Expectations/System.Diagnostics.CodeAnalysis/RequiresUnreferencedCodeAttribute.cs
@@ -0,0 +1,43 @@
+// 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.
+
+namespace System.Diagnostics.CodeAnalysis
+{
+ /// <summary>
+ /// Indicates that the specified method requires dynamic access to code that is not referenced
+ /// statically, for example through <see cref="System.Reflection"/>.
+ /// </summary>
+ /// <remarks>
+ /// This allows tools to understand which methods are unsafe to call when removing unreferenced
+ /// code from an application.
+ ///
+ /// This is a copy of the attribute from dotnet/runtime repo - once linker runs on .NET 5 this should be removed.
+ /// </remarks>
+ [AttributeUsage (AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)]
+ public sealed class RequiresUnreferencedCodeAttribute : Attribute
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="RequiresUnreferencedCodeAttribute"/> class
+ /// with the specified message.
+ /// </summary>
+ /// <param name="message">
+ /// A message that contains information about the usage of unreferenced code.
+ /// </param>
+ public RequiresUnreferencedCodeAttribute (string message)
+ {
+ Message = message;
+ }
+
+ /// <summary>
+ /// Gets a message that contains information about the usage of unreferenced code.
+ /// </summary>
+ public string Message { get; }
+
+ /// <summary>
+ /// Gets or sets an optional URL that contains more information about the method,
+ /// why it requries unreferenced code, and what options a consumer has to deal with it.
+ /// </summary>
+ public string Url { get; set; }
+ }
+} \ No newline at end of file