Skip to content

Commit

Permalink
Fix local scope bug
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Dec 1, 2024
1 parent 776fe7f commit 83a0608
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Underanalyzer/Decompiler/AST/Nodes/AssignNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ Variable is VariableNode
{
// We have a local variable which is (at least) declared by the time of this assignment.
LocalScope currentLocalScope = cleaner.TopFragmentContext!.CurrentLocalScope!;
if (currentLocalScope.DeclaredLocals.Add(localName))
if (!currentLocalScope.LocalDeclaredInAnyParentOrSelf(localName) &&
currentLocalScope.DeclaredLocals.Add(localName))
{
// Track this AssignNode to potentially generate a declaration later.
currentLocalScope.FirstLocalAssignments.Add(localName, this);
Expand Down
56 changes: 56 additions & 0 deletions UnderanalyzerTest/DecompileContext.DecompileToString.Locals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,62 @@ pop.v.i local.i
);
}

[Fact]
public void TestSwitch4()
{
TestUtil.VerifyDecompileResult(
"""
:[0]
pushi.e 0
pop.v.i local.a
push.v self.b
dup.v 0
pushi.e 1
cmp.i.v EQ
bt [3]
:[1]
dup.v 0
pushi.e 2
cmp.i.v EQ
bt [4]
:[2]
b [5]
:[3]
pushi.e 1
pop.v.i local.a
b [5]
:[4]
pushi.e 2
pop.v.i local.a
b [5]
:[5]
popz.v
pushloc.v local.a
call.i show_debug_message 1
popz.v
""",
"""
var a = 0;
switch (b)
{
case 1:
a = 1;
break;
case 2:
a = 2;
break;
}
show_debug_message(a);
"""
);
}

[Fact]
public void TestStruct()
{
Expand Down

0 comments on commit 83a0608

Please sign in to comment.