Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
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
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<PropertyGroup>
<IsReferenceAssembly Condition="'$(IsReferenceAssembly)' == '' and '$([System.IO.Path]::GetFileName($(MSBuildProjectDirectory)))' == 'ref'">true</IsReferenceAssembly>
<DisableImplicitNamespaceImports_DotNet>true</DisableImplicitNamespaceImports_DotNet>
</PropertyGroup>
<PropertyGroup Condition=" '$(IsReferenceAssembly)' == 'true' ">
<!-- Since .NET 5 reference assemblies are always produced -->
Expand Down
2 changes: 1 addition & 1 deletion external/cecil
Submodule cecil updated from a27b1f to 3bef7f
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tools": {
"dotnet": "6.0.100-preview.5.21225.11",
"dotnet": "6.0.100-preview.7.21369.8",
"runtimes": {
"dotnet": [
"5.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/ILLink.CodeFix/RequiresAssemblyFilesCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override SyntaxNode[] GetAttributeArguments (SemanticModel semanticMod
if (string.IsNullOrEmpty (name) || HasPublicAccessibility (containingSymbol!)) {
return Array.Empty<SyntaxNode> ();
} else {
return new[] { generator.AttributeArgument ("Message", generator.LiteralExpression ($"Calls {name}")) };
return new[] { generator.AttributeArgument (generator.LiteralExpression ($"Calls {name}")) };
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using ILLink.Shared;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -116,7 +117,7 @@ void CheckStaticConstructors (OperationAnalysisContext operationContext,
ImmutableArray<IMethodSymbol> staticConstructors)
{
foreach (var staticConstructor in staticConstructors) {
if (staticConstructor.HasAttribute (RequiresAttributeName) && TryGetRequiresAttribute (staticConstructor, out AttributeData? requiresAttribute))
if (staticConstructor.HasAttribute (RequiresAttributeName) && TryGetRequiresAttribute (staticConstructor, out var requiresAttribute))
ReportRequiresDiagnostic (operationContext, staticConstructor, requiresAttribute);
}
}
Expand Down Expand Up @@ -149,7 +150,7 @@ void CheckCalledMember (
while (member is IMethodSymbol method && method.OverriddenMethod != null && SymbolEqualityComparer.Default.Equals (method.ReturnType, method.OverriddenMethod.ReturnType))
member = method.OverriddenMethod;

if (TryGetRequiresAttribute (member, out AttributeData? requiresAttribute)) {
if (TryGetRequiresAttribute (member, out var requiresAttribute)) {
ReportRequiresDiagnostic (operationContext, member, requiresAttribute);
}
}
Expand Down Expand Up @@ -227,7 +228,7 @@ private static ISymbol FindContainingSymbol (OperationAnalysisContext operationC
/// <param name="operationContext">Analyzer operation context to be able to report the diagnostic.</param>
/// <param name="member">Information about the member that generated the diagnostic.</param>
/// <param name="requiresAttribute">Requires attribute data to print attribute arguments.</param>
private void ReportRequiresDiagnostic (OperationAnalysisContext operationContext, ISymbol member, AttributeData? requiresAttribute)
private void ReportRequiresDiagnostic (OperationAnalysisContext operationContext, ISymbol member, AttributeData requiresAttribute)
{
var message = GetMessageFromAttribute (requiresAttribute);
var url = GetUrlFromAttribute (requiresAttribute);
Expand All @@ -250,7 +251,7 @@ private void ReportMismatchInAttributesDiagnostic (SymbolAnalysisContext symbolA

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

protected abstract string GetMessageFromAttribute (AttributeData? requiresAttribute);
protected abstract string GetMessageFromAttribute (AttributeData requiresAttribute);

private string GetUrlFromAttribute (AttributeData? requiresAttribute)
{
Expand All @@ -264,7 +265,7 @@ private string GetUrlFromAttribute (AttributeData? requiresAttribute)
/// <param name="member">Symbol of the member to search attribute.</param>
/// <param name="requiresAttribute">Output variable in case of matching Requires attribute.</param>
/// <returns>True if the member contains a Requires attribute; otherwise, returns false.</returns>
private bool TryGetRequiresAttribute (ISymbol member, out AttributeData? requiresAttribute)
private bool TryGetRequiresAttribute (ISymbol member, [NotNullWhen (returnValue: true)] out AttributeData? requiresAttribute)
{
requiresAttribute = null;
foreach (var _attribute in member.GetAttributes ()) {
Expand Down
17 changes: 10 additions & 7 deletions src/ILLink.RoslynAnalyzer/RequiresAssemblyFilesAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,18 @@ protected override bool ReportSpecialIncompatibleMembersDiagnostic (OperationAna
return false;
}

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

protected override string GetMessageFromAttribute (AttributeData? requiresAttribute)
protected override string GetMessageFromAttribute (AttributeData requiresAttribute)
{
var message = requiresAttribute?.NamedArguments.FirstOrDefault (na => na.Key == "Message").Value.Value?.ToString ();
if (!string.IsNullOrEmpty (message))
message = $" {message}{(message!.TrimEnd ().EndsWith (".") ? "" : ".")}";

return message!;
string message = "";
if (requiresAttribute.ConstructorArguments.Length >= 1) {
message = requiresAttribute.ConstructorArguments[0].Value?.ToString () ?? "";
if (!string.IsNullOrEmpty (message))
message = $" {message}{(message!.TrimEnd ().EndsWith (".") ? "" : ".")}";
}
return message;
}
}
}
4 changes: 2 additions & 2 deletions src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ bool MarkDependencyMethod (TypeDefinition type, string name, string[] signature,
bool marked = false;

int arity_marker = name.IndexOf ('`');
if (arity_marker < 1 || !int.TryParse (name.Substring (arity_marker + 1), out int arity)) {
if (arity_marker < 1 || !int.TryParse (name.AsSpan (arity_marker + 1), out int arity)) {
arity = 0;
} else {
name = name.Substring (0, arity_marker);
Expand Down Expand Up @@ -2035,7 +2035,7 @@ void MarkTypeWithDebuggerDisplayAttribute (TypeDefinition type, CustomAttribute
//
// 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
// method on the current type when it exists on some other type
if (methodName.Contains ("."))
if (methodName.Contains ('.'))
continue;

MethodDefinition method = GetMethodWithNoParameters (type, methodName);
Expand Down
4 changes: 2 additions & 2 deletions src/linker/Linker/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ string Unquote (string arg)
string[] values = value.Split (new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string v in values) {
var id = v.Trim ();
if (!id.StartsWith ("IL", StringComparison.Ordinal) || !ushort.TryParse (id.Substring (2), out ushort code))
if (!id.StartsWith ("IL", StringComparison.Ordinal) || !ushort.TryParse (id.AsSpan (2), out ushort code))
continue;

yield return code;
Expand Down Expand Up @@ -884,7 +884,7 @@ protected bool AddCustomStep (Pipeline pipeline, string arg)
string customStepName;
string targetName = null;
bool before = false;
if (!arg.Contains (":")) {
if (!arg.Contains (':')) {
customStepName = arg;
} else {
string[] parts = arg.Split (':');
Expand Down
3 changes: 2 additions & 1 deletion src/linker/Linker/MethodReferenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Mono.Cecil;

namespace Mono.Linker
Expand All @@ -16,7 +17,7 @@ public static string GetDisplayName (this MethodReference method)
var methodDefinition = method.Resolve ();
if (methodDefinition != null && (methodDefinition.IsSetter || methodDefinition.IsGetter)) {
// Append property name
string name = methodDefinition.IsSetter ? methodDefinition.Name.Substring (4) + ".set" : methodDefinition.Name.Substring (4) + ".get";
string name = methodDefinition.IsSetter ? string.Concat (methodDefinition.Name.AsSpan (4), ".set") : string.Concat (methodDefinition.Name.AsSpan (4), ".get");
sb.Append (name);
// Insert declaring type name and namespace
sb.Insert (0, '.').Insert (0, method.DeclaringType.GetDisplayName ());
Expand Down
2 changes: 1 addition & 1 deletion src/linker/Linker/TypeReferenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void parseArrayDimensions (ArrayType at)
sb.Append (']');
}

sb.Append (arrayType.Name.Substring (0, arrayType.Name.IndexOf ('[')));
sb.Append (arrayType.Name.AsSpan (0, arrayType.Name.IndexOf ('[')));
parseArrayDimensions (arrayType);
var element = arrayType.ElementType as ArrayType;
while (element != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;

Expand Down Expand Up @@ -109,7 +110,7 @@ static bool TryDecodeSuppressMessageAttributeData (CustomAttribute attribute, ou
if (!(attribute.ConstructorArguments[1].Value is string warningId) ||
warningId.Length < 6 ||
!warningId.StartsWith ("IL") ||
!int.TryParse (warningId.Substring (2, 4), out info.Id)) {
!int.TryParse (warningId.AsSpan (2, 4), out info.Id)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tlens/TLens.Analyzers/LargeStaticArraysAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected override void ProcessMethod (MethodDefinition method)
if (!name.StartsWith ("__StaticArrayInitTypeSize="))
continue;

if (!int.TryParse (name.Substring (26), out int size))
if (!int.TryParse (name.AsSpan (26), out int size))
throw new NotImplementedException (name);

methods.Add ((size, method));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Task RequiresAssemblyFilesWithMessageAndUrl ()

class C
{
[RequiresAssemblyFiles (Message = ""Message from attribute"", Url = ""https://helpurl"")]
[RequiresAssemblyFiles (""Message from attribute"", Url = ""https://helpurl"")]
void M1()
{
}
Expand Down Expand Up @@ -231,13 +231,13 @@ void M1()
M2();
}

[RequiresAssemblyFiles (Message = ""Warn from M2"")]
[RequiresAssemblyFiles (""Warn from M2"")]
void M2()
{
M3();
}

[RequiresAssemblyFiles (Message = ""Warn from M3"")]
[RequiresAssemblyFiles (""Warn from M3"")]
void M3()
{
}
Expand Down Expand Up @@ -321,6 +321,8 @@ public void M()
}
}";
return VerifyRequiresAssemblyFilesAnalyzer (src,
// (8,13): warning IL3002: Using member 'System.Reflection.AssemblyName.CodeBase.get' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. The code will return an empty string for assemblies embedded in a single-file app.
VerifyCS.Diagnostic (RequiresAssemblyFilesAnalyzer.IL3002).WithSpan (8, 13, 8, 23).WithArguments ("System.Reflection.AssemblyName.CodeBase.get", " The code will return an empty string for assemblies embedded in a single-file app.", ""),
// (8,13): warning IL3000: 'System.Reflection.AssemblyName.CodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
VerifyCS.Diagnostic (RequiresAssemblyFilesAnalyzer.IL3000).WithSpan (8, 13, 8, 23).WithArguments ("System.Reflection.AssemblyName.CodeBase"),
// (9,13): warning IL3000: 'System.Reflection.AssemblyName.EscapedCodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
Expand Down Expand Up @@ -445,7 +447,7 @@ public Task RequiresAssemblyFilesDiagnosticFix ()
using System.Diagnostics.CodeAnalysis;
public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;
int M2() => M1();
}
Expand All @@ -469,18 +471,18 @@ public class F
using System.Diagnostics.CodeAnalysis;
public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;
[RequiresAssemblyFiles(Message = ""Calls M1"")]
[RequiresAssemblyFiles(""Calls M1"")]
int M2() => M1();
}
class D
{
[RequiresAssemblyFiles(Message = ""Calls M1"")]
[RequiresAssemblyFiles(""Calls M1"")]
public int M3(C c) => c.M1();
public class E
{
[RequiresAssemblyFiles(Message = ""Calls M1"")]
[RequiresAssemblyFiles(""Calls M1"")]
public int M4(C c) => c.M1();
}
}
Expand Down Expand Up @@ -561,7 +563,7 @@ public Task FixInPropertyDecl ()

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

int M2 => M1();
Expand All @@ -572,10 +574,10 @@ public class C

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

[RequiresAssemblyFiles(Message = ""Calls M1"")]
[RequiresAssemblyFiles(""Calls M1"")]
int M2 => M1();
}";
return VerifyRequiresAssemblyFilesCodeFix (
Expand Down Expand Up @@ -642,7 +644,7 @@ public Task FixInLocalFunc ()

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

Action M2()
Expand All @@ -657,13 +659,13 @@ Action M2()

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

[RequiresAssemblyFiles(Message = ""Calls Wrapper"")]
[RequiresAssemblyFiles(""Calls Wrapper"")]
Action M2()
{
[global::System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute(Message = ""Calls M1"")] void Wrapper () => M1();
[global::System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute(""Calls M1"")] void Wrapper () => M1();
return Wrapper;
}
}";
Expand All @@ -688,7 +690,7 @@ public Task FixInCtor ()

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

public C () => M1();
Expand All @@ -699,7 +701,7 @@ public class C

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

[RequiresAssemblyFiles()]
Expand All @@ -724,7 +726,7 @@ public Task FixInEvent ()

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

public event EventHandler E1
Expand All @@ -742,7 +744,7 @@ public event EventHandler E1

public class C
{
[RequiresAssemblyFiles(Message = ""message"")]
[RequiresAssemblyFiles(""message"")]
public int M1() => 0;

[RequiresAssemblyFiles()]
Expand Down Expand Up @@ -773,7 +775,7 @@ public Task TestStaticCctorRequiresAssemblyFiles ()

class StaticCtor
{
[RequiresAssemblyFiles (Message = ""Message for --TestStaticCtor--"")]
[RequiresAssemblyFiles (""Message for --TestStaticCtor--"")]
static StaticCtor ()
{
}
Expand All @@ -799,7 +801,7 @@ class StaticCtorTriggeredByFieldAccess
{
public static int field;

[RequiresAssemblyFiles (Message = ""Message for --StaticCtorTriggeredByFieldAccess.Cctor--"")]
[RequiresAssemblyFiles (""Message for --StaticCtorTriggeredByFieldAccess.Cctor--"")]
static StaticCtorTriggeredByFieldAccess ()
{
field = 0;
Expand All @@ -826,12 +828,12 @@ public Task TestStaticCtorTriggeredByMethodCall ()

class StaticCtorTriggeredByMethodCall
{
[RequiresAssemblyFiles (Message = ""Message for --StaticCtorTriggeredByMethodCall.Cctor--"")]
[RequiresAssemblyFiles (""Message for --StaticCtorTriggeredByMethodCall.Cctor--"")]
static StaticCtorTriggeredByMethodCall ()
{
}

[RequiresAssemblyFiles (Message = ""Message for --StaticCtorTriggeredByMethodCall.TriggerStaticCtorMarking--"")]
[RequiresAssemblyFiles (""Message for --StaticCtorTriggeredByMethodCall.TriggerStaticCtorMarking--"")]
public void TriggerStaticCtorMarking ()
{
}
Expand Down
2 changes: 1 addition & 1 deletion test/ILLink.RoslynAnalyzer.Tests/TestCaseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class TestCaseUtils
public static readonly ReferenceAssemblies Net6PreviewAssemblies =
new ReferenceAssemblies (
"net6.0",
new PackageIdentity ("Microsoft.NETCore.App.Ref", "6.0.0-preview.5.21224.4"),
new PackageIdentity ("Microsoft.NETCore.App.Ref", "6.0.0-preview.7.21368.2"),
Path.Combine ("ref", "net6.0"))
.WithNuGetConfigFilePath (Path.Combine (TestCaseUtils.GetRepoRoot (), "NuGet.config"));

Expand Down
Loading