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

Fix trimming of DebuggerDisplay with Name #92191

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions src/tools/illink/illink.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ILLink.Shared", "src\ILLink
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ILLink.RoslynAnalyzer.Tests.Generator", "test\ILLink.RoslynAnalyzer.Tests.Generator\ILLink.RoslynAnalyzer.Tests.Generator.csproj", "{3DDE7064-4B68-4979-8843-FDF4CE5A5140}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicDataFlow", "test\Mono.Linker.Tests.Cases\TopLevelStatements\BasicDataFlow\BasicDataFlow.csproj", "{7E243617-E35D-47AB-981A-9FCD96E1AC52}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicKeptValidation", "test\Mono.Linker.Tests.Cases\TopLevelStatements\BasicKeptValidation\BasicKeptValidation.csproj", "{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ILVerification", "..\..\coreclr\tools\ILVerification\ILVerification.csproj", "{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -203,6 +209,42 @@ Global
{3DDE7064-4B68-4979-8843-FDF4CE5A5140}.Release|x64.Build.0 = Release|Any CPU
{3DDE7064-4B68-4979-8843-FDF4CE5A5140}.Release|x86.ActiveCfg = Release|Any CPU
{3DDE7064-4B68-4979-8843-FDF4CE5A5140}.Release|x86.Build.0 = Release|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Debug|x64.ActiveCfg = Debug|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Debug|x64.Build.0 = Debug|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Debug|x86.ActiveCfg = Debug|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Debug|x86.Build.0 = Debug|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Release|Any CPU.Build.0 = Release|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Release|x64.ActiveCfg = Release|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Release|x64.Build.0 = Release|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Release|x86.ActiveCfg = Release|Any CPU
{7E243617-E35D-47AB-981A-9FCD96E1AC52}.Release|x86.Build.0 = Release|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Debug|x64.ActiveCfg = Debug|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Debug|x64.Build.0 = Debug|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Debug|x86.ActiveCfg = Debug|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Debug|x86.Build.0 = Debug|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Release|Any CPU.Build.0 = Release|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Release|x64.ActiveCfg = Release|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Release|x64.Build.0 = Release|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Release|x86.ActiveCfg = Release|Any CPU
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87}.Release|x86.Build.0 = Release|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Debug|x64.ActiveCfg = Debug|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Debug|x64.Build.0 = Debug|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Debug|x86.ActiveCfg = Debug|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Debug|x86.Build.0 = Debug|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Release|Any CPU.Build.0 = Release|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Release|x64.ActiveCfg = Release|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Release|x64.Build.0 = Release|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Release|x86.ActiveCfg = Release|Any CPU
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -218,11 +260,15 @@ Global
{8DA71B3B-5809-44E5-A018-5DE5C6FF6C2A} = {03EB085F-3E2E-4A68-A7DF-951ADF59A0CC}
{6D20F334-B7E4-4585-854B-8A0E2B29B4AA} = {AA0569FB-73E9-4B42-9A19-714BB1229DAE}
{3DDE7064-4B68-4979-8843-FDF4CE5A5140} = {C2969923-7BAA-4FE4-853C-F670B0D3C6C8}
{7E243617-E35D-47AB-981A-9FCD96E1AC52} = {C2969923-7BAA-4FE4-853C-F670B0D3C6C8}
{17F0B7BF-C150-4FFE-8D40-8A2FB2C8AF87} = {C2969923-7BAA-4FE4-853C-F670B0D3C6C8}
{92DA00DE-8CFA-4AE9-B622-39BDC83CB8B2} = {C2969923-7BAA-4FE4-853C-F670B0D3C6C8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E43A3901-42B0-48CA-BB36-5CD40A99A6EE}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
test\Trimming.Tests.Shared\Trimming.Tests.Shared.projitems*{400a1561-b6b6-482d-9e4c-3ddaede5bd07}*SharedItemsImports = 5
src\ILLink.Shared\ILLink.Shared.projitems*{dd28e2b1-057b-4b4d-a04d-b2ebd9e76e46}*SharedItemsImports = 5
src\ILLink.Shared\ILLink.Shared.projitems*{f1a44a78-34ee-408b-8285-9a26f0e7d4f2}*SharedItemsImports = 5
src\ILLink.Shared\ILLink.Shared.projitems*{ff598e93-8e9e-4091-9f50-61a7572663ae}*SharedItemsImports = 13
Expand Down
110 changes: 60 additions & 50 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,70 +2265,80 @@ void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
void MarkTypeWithDebuggerDisplayAttribute (TypeDefinition type, CustomAttribute attribute)
{
if (Context.KeepMembersForDebugger) {

// Members referenced by the DebuggerDisplayAttribute are kept even if the attribute may not be.
// Record a logical dependency on the attribute so that we can blame it for the kept members below.
Tracer.AddDirectDependency (attribute, new DependencyInfo (DependencyKind.CustomAttribute, type), marked: false);

string displayString = (string) attribute.ConstructorArguments[0].Value;
if (string.IsNullOrEmpty (displayString))
return;
MarkTypeWithDebuggerDisplayAttributeValue(type, attribute, (string) attribute.ConstructorArguments[0].Value);
if (attribute.HasProperties) {
foreach (var property in attribute.Properties) {
if (property.Name == "Name") {
MarkTypeWithDebuggerDisplayAttributeValue (type, attribute, (string) property.Argument.Value);
}
}
}
}
}

foreach (Match match in DebuggerDisplayAttributeValueRegex ().Matches (displayString)) {
// Remove '{' and '}'
string realMatch = match.Value.Substring (1, match.Value.Length - 2);
void MarkTypeWithDebuggerDisplayAttributeValue (TypeDefinition type, CustomAttribute attribute, string? displayString)
{
if (string.IsNullOrEmpty (displayString))
return;

// Remove ",nq" suffix if present
// (it asks the expression evaluator to remove the quotes when displaying the final value)
if (ContainsNqSuffixRegex ().IsMatch (realMatch)) {
realMatch = realMatch.Substring (0, realMatch.LastIndexOf (','));
}
foreach (Match match in DebuggerDisplayAttributeValueRegex ().Matches (displayString)) {
// Remove '{' and '}'
string realMatch = match.Value.Substring (1, match.Value.Length - 2);

if (realMatch.EndsWith ("()")) {
string methodName = realMatch.Substring (0, realMatch.Length - 2);
// Remove ",nq" suffix if present
// (it asks the expression evaluator to remove the quotes when displaying the final value)
if (ContainsNqSuffixRegex ().IsMatch (realMatch)) {
realMatch = realMatch.Substring (0, realMatch.LastIndexOf (','));
}

// It's a call to a method on some member. Handling this scenario robustly would be complicated and a decent bit of work.
//
// 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 ('.'))
continue;
if (realMatch.EndsWith ("()")) {
string methodName = realMatch.Substring (0, realMatch.Length - 2);

MethodDefinition? method = GetMethodWithNoParameters (type, methodName);
if (method != null) {
MarkMethod (method, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
continue;
}
} else {
FieldDefinition? field = GetField (type, realMatch);
if (field != null) {
MarkField (field, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
continue;
}
// It's a call to a method on some member. Handling this scenario robustly would be complicated and a decent bit of work.
//
// 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 ('.'))
continue;

PropertyDefinition? property = GetProperty (type, realMatch);
if (property != null) {
if (property.GetMethod != null) {
MarkMethod (property.GetMethod, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
}
if (property.SetMethod != null) {
MarkMethod (property.SetMethod, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
}
continue;
}
MethodDefinition? method = GetMethodWithNoParameters (type, methodName);
if (method != null) {
MarkMethod (method, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
continue;
}
} else {
FieldDefinition? field = GetField (type, realMatch);
if (field != null) {
MarkField (field, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
continue;
}

while (true) {
// Currently if we don't understand the DebuggerDisplayAttribute we mark everything on the type
// This can be improved: dotnet/linker/issues/1873
MarkMethods (type, new DependencyInfo (DependencyKind.KeptForSpecialAttribute, attribute));
MarkFields (type, includeStatic: true, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute));
if (Context.TryResolve (type.BaseType) is not TypeDefinition baseType)
break;
type = baseType;
PropertyDefinition? property = GetProperty (type, realMatch);
if (property != null) {
if (property.GetMethod != null) {
MarkMethod (property.GetMethod, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
}
if (property.SetMethod != null) {
MarkMethod (property.SetMethod, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute), ScopeStack.CurrentScope.Origin);
}
continue;
}
return;
}

while (true) {
// Currently if we don't understand the DebuggerDisplayAttribute we mark everything on the type
// This can be improved: dotnet/linker/issues/1873
MarkMethods (type, new DependencyInfo (DependencyKind.KeptForSpecialAttribute, attribute));
MarkFields (type, includeStatic: true, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute));
if (Context.TryResolve (type.BaseType) is not TypeDefinition baseType)
break;
type = baseType;
}
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static void Main ()
var foo = new Foo ();
var bar = new Bar ();
var baz = new Baz ();
var fooBaz = new FooBaz ();
}

[Kept]
Expand Down Expand Up @@ -39,5 +40,13 @@ public int Method ()
class Baz
{
}

[Kept]
[KeptMember (".ctor()")]
[DebuggerDisplay ("_", Name="{Property}")]
class FooBaz
{
public int Property { get; set; }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static void Main ()
{
var foo = new Foo ();
var bar = new Bar ();
var fooBaz = new FooBaz ();
}

[Kept]
Expand All @@ -38,5 +39,16 @@ public int Method ()
return 1;
}
}

[Kept]
[KeptMember (".ctor()")]
[KeptAttributeAttribute (typeof (DebuggerDisplayAttribute))]
[DebuggerDisplay ("_", Name="{Property}")]
class FooBaz
{
[Kept]
[KeptBackingField]
public int Property { [Kept] get; [Kept] set; }
}
}
}
}