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

Keep compiler-generated code that is still referenced #2684

Merged
merged 4 commits into from
Apr 30, 2022
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
150 changes: 92 additions & 58 deletions ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,9 +1244,12 @@ EntityDeclaration DoDecompile(ITypeDefinition typeDef, DecompileRun decompileRun
{
Debug.Assert(decompilationContext.CurrentTypeDefinition == typeDef);
var watch = System.Diagnostics.Stopwatch.StartNew();
var entityMap = new MultiDictionary<IEntity, EntityDeclaration>();
var workList = new Queue<IEntity>();
TypeSystemAstBuilder typeSystemAstBuilder;
try
{
var typeSystemAstBuilder = CreateAstBuilder(decompileRun.Settings);
typeSystemAstBuilder = CreateAstBuilder(decompileRun.Settings);
var entityDecl = typeSystemAstBuilder.ConvertEntity(typeDef);
var typeDecl = entityDecl as TypeDeclaration;
if (typeDecl == null)
Expand Down Expand Up @@ -1289,16 +1292,6 @@ EntityDeclaration DoDecompile(ITypeDefinition typeDef, DecompileRun decompileRun
}
}

foreach (var type in typeDef.NestedTypes)
{
if (!type.MetadataToken.IsNil && !MemberIsHidden(module.PEFile, type.MetadataToken, settings))
{
var nestedType = DoDecompile(type, decompileRun, decompilationContext.WithCurrentTypeDefinition(type));
SetNewModifier(nestedType);
typeDecl.Members.Add(nestedType);
}
}

decompileRun.EnumValueDisplayMode = typeDef.Kind == TypeKind.Enum
? DetectBestEnumValueDisplayMode(typeDef, module.PEFile)
: null;
Expand All @@ -1309,60 +1302,38 @@ EntityDeclaration DoDecompile(ITypeDefinition typeDef, DecompileRun decompileRun

// For COM interop scenarios, the relative order of virtual functions/properties matters:
IEnumerable<IMember> allOrderedMembers = RequiresNativeOrdering(typeDef) ? GetMembersWithNativeOrdering(typeDef) :
fieldsAndProperties.Concat<IMember>(typeDef.Events).Concat<IMember>(typeDef.Methods);
fieldsAndProperties.Concat(typeDef.Events).Concat(typeDef.Methods);

var allOrderedEntities = typeDef.NestedTypes.Concat<IEntity>(allOrderedMembers);

foreach (var member in allOrderedMembers)
// Decompile members that are not compiler-generated.
foreach (var entity in allOrderedEntities)
{
if (member is IField || member is IProperty)
if (entity.MetadataToken.IsNil || MemberIsHidden(module.PEFile, entity.MetadataToken, settings))
{
var fieldOrProperty = member;
if (fieldOrProperty.MetadataToken.IsNil || MemberIsHidden(module.PEFile, fieldOrProperty.MetadataToken, settings))
{
continue;
}
if (fieldOrProperty is IField field)
{
if (typeDef.Kind == TypeKind.Enum && !field.IsConst)
continue;
var memberDecl = DoDecompile(field, decompileRun, decompilationContext.WithCurrentMember(field));
typeDecl.Members.Add(memberDecl);
}
else if (fieldOrProperty is IProperty property)
{
if (recordDecompiler?.PropertyIsGenerated(property) == true)
{
continue;
}
var propDecl = DoDecompile(property, decompileRun, decompilationContext.WithCurrentMember(property));
typeDecl.Members.Add(propDecl);
}
}
else if (member is IMethod method)
{
if (recordDecompiler?.MethodIsGenerated(method) == true)
{
continue;
}
if (!method.MetadataToken.IsNil && !MemberIsHidden(module.PEFile, method.MetadataToken, settings))
{
var memberDecl = DoDecompile(method, decompileRun, decompilationContext.WithCurrentMember(method));
typeDecl.Members.Add(memberDecl);
typeDecl.Members.AddRange(AddInterfaceImplHelpers(memberDecl, method, typeSystemAstBuilder));
}
}
else if (member is IEvent @event)
{
if ([email protected] && !MemberIsHidden(module.PEFile, @event.MetadataToken, settings))
{
var eventDecl = DoDecompile(@event, decompileRun, decompilationContext.WithCurrentMember(@event));
typeDecl.Members.Add(eventDecl);
}
continue;
}
else
DoDecompileMember(entity, recordDecompiler);
}

// Decompile compiler-generated members that are still needed.
while (workList.Count > 0)
{
var entity = workList.Dequeue();
if (entityMap.Contains(entity) || entity.MetadataToken.IsNil)
{
throw new ArgumentOutOfRangeException("Unexpected member type");
// Member is already decompiled.
continue;
}
DoDecompileMember(entity, recordDecompiler);
}

// Add all decompiled members to syntax tree in the correct order.
foreach (var member in allOrderedEntities)
{
typeDecl.Members.AddRange(entityMap[member]);
}

if (typeDecl.Members.OfType<IndexerDeclaration>().Any(idx => idx.PrivateImplementationType.IsNull))
{
// Remove the [DefaultMember] attribute if the class contains indexers
Expand Down Expand Up @@ -1421,6 +1392,69 @@ EntityDeclaration DoDecompile(ITypeDefinition typeDef, DecompileRun decompileRun
watch.Stop();
Instrumentation.DecompilerEventSource.Log.DoDecompileTypeDefinition(typeDef.FullName, watch.ElapsedMilliseconds);
}

void DoDecompileMember(IEntity entity, RecordDecompiler recordDecompiler)
{
EntityDeclaration entityDecl;
switch (entity)
{
case IField field:
if (typeDef.Kind == TypeKind.Enum && !field.IsConst)
{
return;
}
entityDecl = DoDecompile(field, decompileRun, decompilationContext.WithCurrentMember(field));
entityMap.Add(field, entityDecl);
break;
case IProperty property:
if (recordDecompiler?.PropertyIsGenerated(property) == true)
{
return;
}
entityDecl = DoDecompile(property, decompileRun, decompilationContext.WithCurrentMember(property));
entityMap.Add(property, entityDecl);
break;
case IMethod method:
if (recordDecompiler?.MethodIsGenerated(method) == true)
{
return;
}
entityDecl = DoDecompile(method, decompileRun, decompilationContext.WithCurrentMember(method));
entityMap.Add(method, entityDecl);
foreach (var helper in AddInterfaceImplHelpers(entityDecl, method, typeSystemAstBuilder))
{
entityMap.Add(method, helper);
}
break;
case IEvent @event:
entityDecl = DoDecompile(@event, decompileRun, decompilationContext.WithCurrentMember(@event));
entityMap.Add(@event, entityDecl);
break;
case ITypeDefinition type:
entityDecl = DoDecompile(type, decompileRun, decompilationContext.WithCurrentTypeDefinition(type));
SetNewModifier(entityDecl);
entityMap.Add(type, entityDecl);
break;
default:
throw new ArgumentOutOfRangeException("Unexpected member type");
}

foreach (var node in entityDecl.Descendants)
{
var rr = node.GetResolveResult();
if (rr is MemberResolveResult mrr
&& mrr.Member.DeclaringTypeDefinition == typeDef
&& !(mrr.Member is IMethod { IsLocalFunction: true }))
{
workList.Enqueue(mrr.Member);
}
else if (rr is TypeResolveResult trr
&& trr.Type.GetDefinition()?.DeclaringTypeDefinition == typeDef)
{
workList.Enqueue(trr.Type.GetDefinition());
}
}
}
}

EnumValueDisplayMode DetectBestEnumValueDisplayMode(ITypeDefinition typeDef, PEFile module)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,19 @@ PropertyDeclaration TransformAutomaticProperty(PropertyDeclaration propertyDecla
propertyDeclaration.Modifiers &= ~Modifiers.Readonly;
propertyDeclaration.Getter.Modifiers &= ~Modifiers.Readonly;

// Add C# 7.3 attributes on backing field:
var attributes = field.GetAttributes()
.Where(a => !attributeTypesToRemoveFromAutoProperties.Contains(a.AttributeType.FullName))
.Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();
if (attributes.Length > 0)
var fieldDecl = propertyDeclaration.Parent?.Children.OfType<FieldDeclaration>()
.FirstOrDefault(fd => field.Equals(fd.GetSymbol()));
if (fieldDecl != null)
{
var section = new AttributeSection {
AttributeTarget = "field"
};
section.Attributes.AddRange(attributes);
propertyDeclaration.Attributes.Add(section);
fieldDecl.Remove();
// Add C# 7.3 attributes on backing field:
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.CompilerGenerated);
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.DebuggerBrowsable);
foreach (var section in fieldDecl.Attributes)
{
section.AttributeTarget = "field";
propertyDeclaration.Attributes.Add(section.Detach());
}
}
}
// Since the property instance is not changed, we can continue in the visitor as usual, so return null
Expand Down Expand Up @@ -1010,23 +1012,17 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev)
ed.Variables.Add(new VariableInitializer(ev.Name));
ed.CopyAnnotationsFrom(ev);

if (ev.GetSymbol() is IEvent eventDef)
var fieldDecl = ev.Parent?.Children.OfType<FieldDeclaration>()
.FirstOrDefault(fd => fd.Variables.Single().Name == ev.Name);
if (fieldDecl != null)
{
IField field = eventDef.DeclaringType.GetFields(f => f.Name == ev.Name, GetMemberOptions.IgnoreInheritedMembers).SingleOrDefault();
if (field != null)
fieldDecl.Remove();
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.CompilerGenerated);
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.DebuggerBrowsable);
foreach (var section in fieldDecl.Attributes)
{
ed.AddAnnotation(field);
var attributes = field.GetAttributes()
.Where(a => !attributeTypesToRemoveFromAutoEvents.Contains(a.AttributeType.FullName))
.Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();
if (attributes.Length > 0)
{
var section = new AttributeSection {
AttributeTarget = "field"
};
section.Attributes.AddRange(attributes);
ed.Attributes.Add(section);
}
section.AttributeTarget = "field";
ed.Attributes.Add(section.Detach());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public enum KnownAttribute
SpecialName,
DebuggerHidden,
DebuggerStepThrough,
DebuggerBrowsable,

// Assembly attributes:
AssemblyVersion,
Expand Down Expand Up @@ -124,6 +125,7 @@ static class KnownAttributes
new TopLevelTypeName("System.Runtime.CompilerServices", nameof(SpecialNameAttribute)),
new TopLevelTypeName("System.Diagnostics", nameof(DebuggerHiddenAttribute)),
new TopLevelTypeName("System.Diagnostics", nameof(DebuggerStepThroughAttribute)),
new TopLevelTypeName("System.Diagnostics", nameof(DebuggerBrowsableAttribute)),
// Assembly attributes:
new TopLevelTypeName("System.Reflection", nameof(AssemblyVersionAttribute)),
new TopLevelTypeName("System.Runtime.CompilerServices", nameof(InternalsVisibleToAttribute)),
Expand Down
2 changes: 1 addition & 1 deletion ICSharpCode.Decompiler/Util/MultiDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ IEnumerable<TValue> ILookup<TKey, TValue>.this[TKey key] {
get { return this[key]; }
}

bool ILookup<TKey, TValue>.Contains(TKey key)
public bool Contains(TKey key)
{
return dict.ContainsKey(key);
}
Expand Down