Skip to content

Commit b888d67

Browse files
authored
RAF Attribute unification (#2156)
Bump sdk version in global.json Fix new analyzer recommendations about AsSpan and Contains(char) Disable ImplicitNamespaceImports Update Microsoft.NetCore.App.Ref used by analyzer to take new Runtime attribute changes Update analyzer to not use Message as a property in RequiresAssemblyFilesAttribute Update cecil to use Microsoft.NETFramework.ReferenceAssemblies.net40 1.0.2 along with taking other cecil updates
1 parent 7b1945a commit b888d67

15 files changed

+61
-69
lines changed

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<IsReferenceAssembly Condition="'$(IsReferenceAssembly)' == '' and '$([System.IO.Path]::GetFileName($(MSBuildProjectDirectory)))' == 'ref'">true</IsReferenceAssembly>
4+
<DisableImplicitNamespaceImports_DotNet>true</DisableImplicitNamespaceImports_DotNet>
45
</PropertyGroup>
56
<PropertyGroup Condition=" '$(IsReferenceAssembly)' == 'true' ">
67
<!-- Since .NET 5 reference assemblies are always produced -->

external/cecil

Submodule cecil updated from a27b1fb to 3bef7fc

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"tools": {
3-
"dotnet": "6.0.100-preview.5.21225.11",
3+
"dotnet": "6.0.100-preview.7.21369.8",
44
"runtimes": {
55
"dotnet": [
66
"5.0.0"

src/ILLink.CodeFix/RequiresAssemblyFilesCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override SyntaxNode[] GetAttributeArguments (SemanticModel semanticMod
3636
if (string.IsNullOrEmpty (name) || HasPublicAccessibility (containingSymbol!)) {
3737
return Array.Empty<SyntaxNode> ();
3838
} else {
39-
return new[] { generator.AttributeArgument ("Message", generator.LiteralExpression ($"Calls {name}")) };
39+
return new[] { generator.AttributeArgument (generator.LiteralExpression ($"Calls {name}")) };
4040
}
4141
}
4242
}

src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Immutable;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Linq;
89
using ILLink.Shared;
910
using Microsoft.CodeAnalysis;
@@ -116,7 +117,7 @@ void CheckStaticConstructors (OperationAnalysisContext operationContext,
116117
ImmutableArray<IMethodSymbol> staticConstructors)
117118
{
118119
foreach (var staticConstructor in staticConstructors) {
119-
if (staticConstructor.HasAttribute (RequiresAttributeName) && TryGetRequiresAttribute (staticConstructor, out AttributeData? requiresAttribute))
120+
if (staticConstructor.HasAttribute (RequiresAttributeName) && TryGetRequiresAttribute (staticConstructor, out var requiresAttribute))
120121
ReportRequiresDiagnostic (operationContext, staticConstructor, requiresAttribute);
121122
}
122123
}
@@ -149,7 +150,7 @@ void CheckCalledMember (
149150
while (member is IMethodSymbol method && method.OverriddenMethod != null && SymbolEqualityComparer.Default.Equals (method.ReturnType, method.OverriddenMethod.ReturnType))
150151
member = method.OverriddenMethod;
151152

152-
if (TryGetRequiresAttribute (member, out AttributeData? requiresAttribute)) {
153+
if (TryGetRequiresAttribute (member, out var requiresAttribute)) {
153154
ReportRequiresDiagnostic (operationContext, member, requiresAttribute);
154155
}
155156
}
@@ -227,7 +228,7 @@ private static ISymbol FindContainingSymbol (OperationAnalysisContext operationC
227228
/// <param name="operationContext">Analyzer operation context to be able to report the diagnostic.</param>
228229
/// <param name="member">Information about the member that generated the diagnostic.</param>
229230
/// <param name="requiresAttribute">Requires attribute data to print attribute arguments.</param>
230-
private void ReportRequiresDiagnostic (OperationAnalysisContext operationContext, ISymbol member, AttributeData? requiresAttribute)
231+
private void ReportRequiresDiagnostic (OperationAnalysisContext operationContext, ISymbol member, AttributeData requiresAttribute)
231232
{
232233
var message = GetMessageFromAttribute (requiresAttribute);
233234
var url = GetUrlFromAttribute (requiresAttribute);
@@ -250,7 +251,7 @@ private void ReportMismatchInAttributesDiagnostic (SymbolAnalysisContext symbolA
250251

251252
private bool HasMismatchingAttributes (ISymbol member1, ISymbol member2) => member1.HasAttribute (RequiresAttributeName) ^ member2.HasAttribute (RequiresAttributeName);
252253

253-
protected abstract string GetMessageFromAttribute (AttributeData? requiresAttribute);
254+
protected abstract string GetMessageFromAttribute (AttributeData requiresAttribute);
254255

255256
private string GetUrlFromAttribute (AttributeData? requiresAttribute)
256257
{
@@ -264,7 +265,7 @@ private string GetUrlFromAttribute (AttributeData? requiresAttribute)
264265
/// <param name="member">Symbol of the member to search attribute.</param>
265266
/// <param name="requiresAttribute">Output variable in case of matching Requires attribute.</param>
266267
/// <returns>True if the member contains a Requires attribute; otherwise, returns false.</returns>
267-
private bool TryGetRequiresAttribute (ISymbol member, out AttributeData? requiresAttribute)
268+
private bool TryGetRequiresAttribute (ISymbol member, [NotNullWhen (returnValue: true)] out AttributeData? requiresAttribute)
268269
{
269270
requiresAttribute = null;
270271
foreach (var _attribute in member.GetAttributes ()) {

src/ILLink.RoslynAnalyzer/RequiresAssemblyFilesAnalyzer.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,18 @@ protected override bool ReportSpecialIncompatibleMembersDiagnostic (OperationAna
123123
return false;
124124
}
125125

126-
protected override bool VerifyAttributeArguments (AttributeData attribute) => attribute.ConstructorArguments.Length == 0;
126+
protected override bool VerifyAttributeArguments (AttributeData attribute) => attribute.ConstructorArguments.Length == 0 ||
127+
attribute.ConstructorArguments.Length >= 1 && attribute.ConstructorArguments[0] is { Type: { SpecialType: SpecialType.System_String } } ctorArg;
127128

128-
protected override string GetMessageFromAttribute (AttributeData? requiresAttribute)
129+
protected override string GetMessageFromAttribute (AttributeData requiresAttribute)
129130
{
130-
var message = requiresAttribute?.NamedArguments.FirstOrDefault (na => na.Key == "Message").Value.Value?.ToString ();
131-
if (!string.IsNullOrEmpty (message))
132-
message = $" {message}{(message!.TrimEnd ().EndsWith (".") ? "" : ".")}";
133-
134-
return message!;
131+
string message = "";
132+
if (requiresAttribute.ConstructorArguments.Length >= 1) {
133+
message = requiresAttribute.ConstructorArguments[0].Value?.ToString () ?? "";
134+
if (!string.IsNullOrEmpty (message))
135+
message = $" {message}{(message!.TrimEnd ().EndsWith (".") ? "" : ".")}";
136+
}
137+
return message;
135138
}
136139
}
137140
}

src/linker/Linker.Steps/MarkStep.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ bool MarkDependencyMethod (TypeDefinition type, string name, string[] signature,
10521052
bool marked = false;
10531053

10541054
int arity_marker = name.IndexOf ('`');
1055-
if (arity_marker < 1 || !int.TryParse (name.Substring (arity_marker + 1), out int arity)) {
1055+
if (arity_marker < 1 || !int.TryParse (name.AsSpan (arity_marker + 1), out int arity)) {
10561056
arity = 0;
10571057
} else {
10581058
name = name.Substring (0, arity_marker);
@@ -2035,7 +2035,7 @@ void MarkTypeWithDebuggerDisplayAttribute (TypeDefinition type, CustomAttribute
20352035
//
20362036
// We could implement support for this at some point, but for now it's important to make sure at least we don't crash trying to find some
20372037
// method on the current type when it exists on some other type
2038-
if (methodName.Contains ("."))
2038+
if (methodName.Contains ('.'))
20392039
continue;
20402040

20412041
MethodDefinition method = GetMethodWithNoParameters (type, methodName);

src/linker/Linker/Driver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ string Unquote (string arg)
805805
string[] values = value.Split (new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
806806
foreach (string v in values) {
807807
var id = v.Trim ();
808-
if (!id.StartsWith ("IL", StringComparison.Ordinal) || !ushort.TryParse (id.Substring (2), out ushort code))
808+
if (!id.StartsWith ("IL", StringComparison.Ordinal) || !ushort.TryParse (id.AsSpan (2), out ushort code))
809809
continue;
810810

811811
yield return code;
@@ -884,7 +884,7 @@ protected bool AddCustomStep (Pipeline pipeline, string arg)
884884
string customStepName;
885885
string targetName = null;
886886
bool before = false;
887-
if (!arg.Contains (":")) {
887+
if (!arg.Contains (':')) {
888888
customStepName = arg;
889889
} else {
890890
string[] parts = arg.Split (':');

src/linker/Linker/MethodReferenceExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using Mono.Cecil;
67

78
namespace Mono.Linker
@@ -16,7 +17,7 @@ public static string GetDisplayName (this MethodReference method)
1617
var methodDefinition = method.Resolve ();
1718
if (methodDefinition != null && (methodDefinition.IsSetter || methodDefinition.IsGetter)) {
1819
// Append property name
19-
string name = methodDefinition.IsSetter ? methodDefinition.Name.Substring (4) + ".set" : methodDefinition.Name.Substring (4) + ".get";
20+
string name = methodDefinition.IsSetter ? string.Concat (methodDefinition.Name.AsSpan (4), ".set") : string.Concat (methodDefinition.Name.AsSpan (4), ".get");
2021
sb.Append (name);
2122
// Insert declaring type name and namespace
2223
sb.Insert (0, '.').Insert (0, method.DeclaringType.GetDisplayName ());

src/linker/Linker/TypeReferenceExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void parseArrayDimensions (ArrayType at)
100100
sb.Append (']');
101101
}
102102

103-
sb.Append (arrayType.Name.Substring (0, arrayType.Name.IndexOf ('[')));
103+
sb.Append (arrayType.Name.AsSpan (0, arrayType.Name.IndexOf ('[')));
104104
parseArrayDimensions (arrayType);
105105
var element = arrayType.ElementType as ArrayType;
106106
while (element != null) {

0 commit comments

Comments
 (0)