Skip to content

Commit

Permalink
Fix #3134: Include newobj, initobj and call instructions in TypeInsta…
Browse files Browse the repository at this point in the history
…ntiatedByAnalyzer
  • Loading branch information
siegfriedpammer committed Oct 6, 2024
1 parent e26a173 commit 280d173
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions ICSharpCode.ILSpyX/Analyzers/Builtin/TypeInstantiatedByAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using System.Linq;
using System.Reflection.Metadata;

using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Disassembler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
Expand Down Expand Up @@ -106,47 +105,67 @@ bool ScanMethodBody(ITypeDefinition analyzedEntity, IMethod method, MethodBodyBl
while (blob.RemainingBytes > 0)
{
ILOpCode opCode;
EntityHandle handle;
try
{
opCode = blob.DecodeOpCode();
if (!CanBeReference(opCode))
switch (opCode)
{
blob.SkipOperand(opCode);
continue;
case ILOpCode.Newobj:
case ILOpCode.Call when analyzedEntity.Kind == TypeKind.Struct:
case ILOpCode.Initobj:
handle = MetadataTokenHelpers.EntityHandleOrNil(blob.ReadInt32());
break;
default:
blob.SkipOperand(opCode);
continue;
}
}
catch (BadImageFormatException)
{
return false;
}
EntityHandle methodHandle = MetadataTokenHelpers.EntityHandleOrNil(blob.ReadInt32());
if (!methodHandle.Kind.IsMemberKind())
continue;
IMethod ctor;
}
ITypeDefinition? foundTypeDefinition;
try
{
ctor = module.ResolveMethod(methodHandle, genericContext);
switch (handle.Kind)
{
case HandleKind.MethodDefinition:
case HandleKind.MemberReference:
var ctor = module.ResolveMethod(handle, genericContext);
if (ctor == null || !ctor.IsConstructor)
{
continue;
}
foundTypeDefinition = ctor.DeclaringTypeDefinition;
break;
case HandleKind.TypeDefinition:
case HandleKind.TypeReference:
foundTypeDefinition = module.ResolveType(handle, genericContext)?.GetDefinition();
break;
default:
continue;
}
}
catch (BadImageFormatException)
{
continue;
}
if (ctor == null || !ctor.IsConstructor)
if (foundTypeDefinition?.ParentModule == null)
{
continue;
}

if (ctor.DeclaringTypeDefinition?.MetadataToken == analyzedEntity.MetadataToken
&& ctor.ParentModule?.MetadataFile == analyzedEntity.ParentModule!.MetadataFile)
if (foundTypeDefinition.MetadataToken == analyzedEntity.MetadataToken
&& foundTypeDefinition.ParentModule.MetadataFile == analyzedEntity.ParentModule!.MetadataFile)
{
return true;
}
}

return false;
}

bool CanBeReference(ILOpCode opCode)
{
return opCode == ILOpCode.Newobj || opCode == ILOpCode.Initobj;
}

public bool Show(ISymbol symbol) => symbol is ITypeDefinition entity && !entity.IsAbstract && !entity.IsStatic;
}
}

0 comments on commit 280d173

Please sign in to comment.