Skip to content

Commit

Permalink
Analyzers/AnalyzerScope loops endlessly when analysing public inner c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
yzdeveloper committed Feb 14, 2024
1 parent 84c5e63 commit 808cef9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
52 changes: 52 additions & 0 deletions ILSpy.Tests/Analyzers/AnalyserScopeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Linq;
using System.Threading.Tasks;

using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpy.Analyzers;

using Mono.Cecil;

using NUnit.Framework;

namespace ICSharpCode.ILSpy.Tests.Analyzers
{

[TestFixture]
public class AnalyzerScopeTests
{
public class TestClass
{

}


[Test]
public void WhenPublicNestedClass_ThenNotInfiniteLoop()
{
// Given
ILSpyX.AssemblyList assemblyList = new ILSpyX.AssemblyList();
var file = new PEFile(this.GetType().Assembly.Location);
var td = file.Metadata.TypeDefinitions.First(td => td.GetFullTypeName(file.Metadata).Name == nameof(TestClass));

Decompiler.Metadata.IAssemblyResolver assemblyResolver = new UniversalAssemblyResolver(null, false, null);
ICompilation compilation = new DecompilerTypeSystem(file, assemblyResolver);
ITypeResolveContext context = new CSharpResolver(compilation);
var module = ((IModuleReference)file).Resolve(context) as MetadataModule;
IEntity entity = module.GetDefinition(td);

// When
var task = Task.Run(() => {
var target = new AnalyzerScope(assemblyList, entity);
});

var result = Task.WaitAny(new[] { task, Task.Delay(500) }); // 0.5 seconds

// Then
Assert.That(result == 0, "The constructor should completes in no longer than 10 seconds");
}

}
}
1 change: 1 addition & 0 deletions ILSpy.Tests/ILSpy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Analyzers\AnalyserScopeTests.cs" />
<Compile Include="Analyzers\MemberImplementsInterfaceAnalyzerTests.cs" />
<Compile Include="Analyzers\MethodUsesAnalyzerTests.cs" />
<Compile Include="Analyzers\TestCases\MainAssembly.cs" />
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Analyzers/AnalyzerScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static void DetermineEffectiveAccessibility(IEntity input, out ITypeDefinition t
while (typeScope != null && !accessibility.LessThanOrEqual(Accessibility.Private))
{
accessibility = accessibility.Intersect(typeScope.Accessibility);
typeScope = prevTypeScope.DeclaringTypeDefinition;
typeScope = typeScope.DeclaringTypeDefinition;
}
if (typeScope == null)
{
Expand Down

0 comments on commit 808cef9

Please sign in to comment.