Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion Mono.Cecil.Cil/CodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void ReadFatMethod ()
public VariableDefinitionCollection ReadVariables (MetadataToken local_var_token)
{
var position = reader.position;
var variables = reader.ReadVariables (local_var_token);
var variables = reader.ReadVariables (local_var_token, method);
reader.position = position;

return variables;
Expand Down
43 changes: 35 additions & 8 deletions Mono.Cecil.Cil/MethodBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public bool HasVariables {
public Collection<VariableDefinition> Variables {
get {
if (variables == null)
Interlocked.CompareExchange (ref variables, new VariableDefinitionCollection (), null);
Interlocked.CompareExchange (ref variables, new VariableDefinitionCollection (this.method), null);

return variables;
}
Expand Down Expand Up @@ -134,13 +134,17 @@ public ILProcessor GetILProcessor ()

sealed class VariableDefinitionCollection : Collection<VariableDefinition> {

internal VariableDefinitionCollection ()
readonly MethodDefinition method;

internal VariableDefinitionCollection (MethodDefinition method)
{
this.method = method;
}

internal VariableDefinitionCollection (int capacity)
internal VariableDefinitionCollection (MethodDefinition method, int capacity)
: base (capacity)
{
this.method = method;
}

protected override void OnAdd (VariableDefinition item, int index)
Expand All @@ -151,9 +155,7 @@ protected override void OnAdd (VariableDefinition item, int index)
protected override void OnInsert (VariableDefinition item, int index)
{
item.index = index;

for (int i = index; i < size; i++)
items [i].index = i + 1;
UpdateVariableIndices (index, 1);
}

protected override void OnSet (VariableDefinition item, int index)
Expand All @@ -163,10 +165,35 @@ protected override void OnSet (VariableDefinition item, int index)

protected override void OnRemove (VariableDefinition item, int index)
{
UpdateVariableIndices (index + 1, -1, index);
item.index = -1;
}

for (int i = index + 1; i < size; i++)
items [i].index = i - 1;
void UpdateVariableIndices (int startIndex, int offset, int indexToRemove = -1)
{
for (int i = startIndex; i < size; i++)
items [i].index = i + offset;

var debug_info = method == null ? null : method.debug_info;
if (debug_info == null || debug_info.Scope == null)
return;

foreach (var scope in debug_info.GetScopes ()) {
if (scope.HasVariables) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please transform this condition into an early continue if there's no variable to reduce nesting.

var variables = scope.Variables;
for (int i = 0; i < variables.Count; i++) {
if (variables [i].Index == indexToRemove) {
variables.RemoveAt (i);
}
}

foreach (var variable in variables) {
if (variable.index.index.HasValue && variable.index.index.Value >= startIndex) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this looks a bit odd, I think we want to comment as to why we're doing this. In that case my understanding is that you want to update the indexes of variables that have not been resolved into a VariableDefinition, while a VariableIndex can represent both the resolved and not resolved case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully the new version is more readable (and I also added a comment) - and it now also handles removal of resolved variable debug infos.

variable.index.index += offset;
}
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil.Cil/Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public enum VariableAttributes : ushort {

public struct VariableIndex {
readonly VariableDefinition variable;
readonly int? index;
internal int? index;

public int Index {
get {
Expand Down
4 changes: 2 additions & 2 deletions Mono.Cecil/AssemblyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2141,7 +2141,7 @@ public CallSite ReadCallSite (MetadataToken token)
return call_site;
}

public VariableDefinitionCollection ReadVariables (MetadataToken local_var_token)
public VariableDefinitionCollection ReadVariables (MetadataToken local_var_token, MethodDefinition method = null)
{
if (!MoveTo (Table.StandAloneSig, local_var_token.RID))
return null;
Expand All @@ -2156,7 +2156,7 @@ public VariableDefinitionCollection ReadVariables (MetadataToken local_var_token
if (count == 0)
return null;

var variables = new VariableDefinitionCollection ((int) count);
var variables = new VariableDefinitionCollection (method, (int) count);

for (int i = 0; i < count; i++)
variables.Add (new VariableDefinition (reader.ReadTypeSignature ()));
Expand Down
77 changes: 77 additions & 0 deletions Test/Mono.Cecil.Tests/VariableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,45 @@ public void RemoveVariableIndex ()
Assert.AreEqual (1, z.Index);
}

[Test]
public void RemoveVariableWithDebugInfo ()
{
var object_ref = new TypeReference ("System", "Object", null, null, false);
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
var body = new MethodBody (method);
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ret);

var x = new VariableDefinition (object_ref);
var y = new VariableDefinition (object_ref);
var z = new VariableDefinition (object_ref);
var z2 = new VariableDefinition (object_ref);

body.Variables.Add (x);
body.Variables.Add (y);
body.Variables.Add (z);
body.Variables.Add (z2);

var scope = new ScopeDebugInformation (body.Instructions [0], body.Instructions [0]);
method.DebugInformation = new MethodDebugInformation (method) {
Scope = scope
};
scope.Variables.Add (new VariableDebugInformation (x.index, nameof (x)));
scope.Variables.Add (new VariableDebugInformation (y.index, nameof (y)));
scope.Variables.Add (new VariableDebugInformation (z.index, nameof (z)));
scope.Variables.Add (new VariableDebugInformation (z2, nameof (z2)));

body.Variables.Remove (y);

Assert.AreEqual (3, scope.Variables.Count);
Assert.AreEqual (x.Index, scope.Variables [0].Index);
Assert.AreEqual (nameof (x), scope.Variables [0].Name);
Assert.AreEqual (z.Index, scope.Variables [1].Index);
Assert.AreEqual (nameof (z), scope.Variables [1].Name);
Assert.AreEqual (z2.Index, scope.Variables [2].Index);
Assert.AreEqual (nameof (z2), scope.Variables [2].Name);
}

[Test]
public void InsertVariableIndex ()
{
Expand All @@ -104,5 +143,43 @@ public void InsertVariableIndex ()
Assert.AreEqual (1, y.Index);
Assert.AreEqual (2, z.Index);
}

[Test]
public void InsertVariableWithDebugInfo ()
{
var object_ref = new TypeReference ("System", "Object", null, null, false);
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
var body = new MethodBody (method);
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ret);

var x = new VariableDefinition (object_ref);
var y = new VariableDefinition (object_ref);
var z = new VariableDefinition (object_ref);
var z2 = new VariableDefinition (object_ref);

body.Variables.Add (x);
body.Variables.Add (z);
body.Variables.Add (z2);

var scope = new ScopeDebugInformation (body.Instructions [0], body.Instructions [0]);
method.DebugInformation = new MethodDebugInformation (method) {
Scope = scope
};
scope.Variables.Add (new VariableDebugInformation (x.index, nameof (x)));
scope.Variables.Add (new VariableDebugInformation (z.index, nameof (z)));
scope.Variables.Add (new VariableDebugInformation (z2, nameof (z2)));

body.Variables.Insert (1, y);

// Adding local variable doesn't add debug info for it (since there's no way to deduce the name of the variable)
Assert.AreEqual (3, scope.Variables.Count);
Assert.AreEqual (x.Index, scope.Variables [0].Index);
Assert.AreEqual (nameof (x), scope.Variables [0].Name);
Assert.AreEqual (z.Index, scope.Variables [1].Index);
Assert.AreEqual (nameof (z), scope.Variables [1].Name);
Assert.AreEqual (z2.Index, scope.Variables [2].Index);
Assert.AreEqual (nameof (z2), scope.Variables [2].Name);
}
}
}