diff options
Diffstat (limited to 'src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs')
-rw-r--r-- | src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs index efe00031b..e08359149 100644 --- a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs +++ b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs @@ -6,14 +6,35 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace ILLink.RoslynAnalyzer { public static class RequiresISymbolExtensions { // TODO: Consider sharing with linker IsMethodInRequiresUnreferencedCodeScope method + /// <summary> + /// True if the source of a call is considered to be annotated with the Requires... attribute + /// </summary> public static bool IsInRequiresScope (this ISymbol member, string requiresAttribute) { + return member.IsInRequiresScope (requiresAttribute, true); + } + + /// <summary> + /// True if member of a call is considered to be annotated with the Requires... attribute. + /// Doesn't check the associated symbol for overrides and virtual methods because the analyzer should warn on mismatched between the property AND the accessors + /// </summary> + /// <param name="containingSymbol"> + /// Symbol that is either an overriding member or an overriden/virtual member + /// </param> + public static bool IsOverrideInRequiresScope(this ISymbol member, string requiresAttribute) + { + return member.IsInRequiresScope(requiresAttribute, false); + } + + private static bool IsInRequiresScope (this ISymbol member, string requiresAttribute, bool checkAssociatedSymbol) + { if (member is ISymbol containingSymbol) { if (containingSymbol.HasAttribute (requiresAttribute) || (containingSymbol is not ITypeSymbol && @@ -21,7 +42,8 @@ namespace ILLink.RoslynAnalyzer return true; } } - if (member is IMethodSymbol { AssociatedSymbol: { } associated } && associated.HasAttribute (requiresAttribute)) + // Only check associated symbol if not override or virtual method + if (checkAssociatedSymbol && member is IMethodSymbol { AssociatedSymbol: { } associated } && associated.HasAttribute (requiresAttribute)) return true; return false; |