Skip to content

Commit 28f2b0e

Browse files
author
Paulo Janotti
committed
renames and cleanup
1 parent 7dd7c76 commit 28f2b0e

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

src/coverlet.core/Instrumentation/Instrumenter.cs

+21-41
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ internal class Instrumenter
2323
private readonly string[] _includeFilters;
2424
private readonly string[] _excludedFiles;
2525
private InstrumenterResult _result;
26-
private FieldDefinition _customModuleTrackerHitsArray;
27-
private FieldDefinition _customModuleTrackerHitsFilePath;
28-
private ILProcessor _customModuleTrackerClassConstructorIl;
26+
private FieldDefinition _customTrackerHitsArray;
27+
private FieldDefinition _customTrackerHitsFilePath;
28+
private ILProcessor _customTrackerClassConstructorIl;
2929
private TypeDefinition _customTrackerTypeDef;
30-
private MethodReference _cachedInterlockedIncMethod;
30+
private MethodReference _customTrackerRecordHitMethod;
3131

3232
public Instrumenter(string module, string identifier, string[] excludeFilters, string[] includeFilters, string[] excludedFiles)
3333
{
@@ -83,12 +83,12 @@ private void InstrumentModule()
8383
}
8484

8585
// Fixup the custom tracker class constructor, according to all instrumented types
86-
Instruction lastInstr = _customModuleTrackerClassConstructorIl.Body.Instructions.Last();
87-
_customModuleTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Ldc_I4, _result.HitCandidates.Count));
88-
_customModuleTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Newarr, module.TypeSystem.Int32));
89-
_customModuleTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Stsfld, _customModuleTrackerHitsArray));
90-
_customModuleTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Ldstr, _result.HitsFilePath));
91-
_customModuleTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Stsfld, _customModuleTrackerHitsFilePath));
86+
Instruction lastInstr = _customTrackerClassConstructorIl.Body.Instructions.Last();
87+
_customTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Ldc_I4, _result.HitCandidates.Count));
88+
_customTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Newarr, module.TypeSystem.Int32));
89+
_customTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Stsfld, _customTrackerHitsArray));
90+
_customTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Ldstr, _result.HitsFilePath));
91+
_customTrackerClassConstructorIl.InsertBefore(lastInstr, Instruction.Create(OpCodes.Stsfld, _customTrackerHitsFilePath));
9292

9393
module.Write(stream);
9494
}
@@ -97,9 +97,9 @@ private void InstrumentModule()
9797

9898
private void AddCustomModuleTrackerToModule(ModuleDefinition module)
9999
{
100-
using (AssemblyDefinition xad = AssemblyDefinition.ReadAssembly(typeof(Instrumenter).Assembly.Location))
100+
using (AssemblyDefinition coverletInstrumentationAssembly = AssemblyDefinition.ReadAssembly(typeof(Instrumenter).Assembly.Location))
101101
{
102-
TypeDefinition moduleTrackerTemplate = xad.MainModule.GetType(
102+
TypeDefinition moduleTrackerTemplate = coverletInstrumentationAssembly.MainModule.GetType(
103103
"Coverlet.Core.Instrumentation", nameof(ModuleTrackerTemplate));
104104

105105
_customTrackerTypeDef = new TypeDefinition(
@@ -114,9 +114,9 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
114114
_customTrackerTypeDef.Fields.Add(fieldClone);
115115

116116
if (fieldClone.Name == "HitsArray")
117-
_customModuleTrackerHitsArray = fieldClone;
117+
_customTrackerHitsArray = fieldClone;
118118
else if (fieldClone.Name == "HitsFilePath")
119-
_customModuleTrackerHitsFilePath = fieldClone;
119+
_customTrackerHitsFilePath = fieldClone;
120120
}
121121

122122
foreach (MethodDefinition methodDef in moduleTrackerTemplate.Methods)
@@ -140,7 +140,7 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
140140

141141
ILProcessor ilProcessor = methodOnCustomType.Body.GetILProcessor();
142142
if (methodDef.Name == ".cctor")
143-
_customModuleTrackerClassConstructorIl = ilProcessor;
143+
_customTrackerClassConstructorIl = ilProcessor;
144144

145145
foreach (Instruction instr in methodDef.Body.Instructions)
146146
{
@@ -179,8 +179,8 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
179179
module.Types.Add(_customTrackerTypeDef);
180180
}
181181

182-
Debug.Assert(_customModuleTrackerHitsArray != null);
183-
Debug.Assert(_customModuleTrackerClassConstructorIl != null);
182+
Debug.Assert(_customTrackerHitsArray != null);
183+
Debug.Assert(_customTrackerClassConstructorIl != null);
184184
}
185185

186186
private void InstrumentType(TypeDefinition type)
@@ -328,40 +328,20 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
328328

329329
private Instruction AddInstrumentationInstructions(MethodDefinition method, ILProcessor processor, Instruction instruction, int hitEntryIndex)
330330
{
331-
if (_cachedInterlockedIncMethod == null)
331+
if (_customTrackerRecordHitMethod == null)
332332
{
333-
_cachedInterlockedIncMethod = new MethodReference(
333+
_customTrackerRecordHitMethod = new MethodReference(
334334
"RecordHit", method.Module.TypeSystem.Void, _customTrackerTypeDef);
335-
_cachedInterlockedIncMethod.Parameters.Add(new ParameterDefinition(method.Module.TypeSystem.Int32));
335+
_customTrackerRecordHitMethod.Parameters.Add(new ParameterDefinition(method.Module.TypeSystem.Int32));
336336
}
337337

338338
var indxInstr = Instruction.Create(OpCodes.Ldc_I4, hitEntryIndex);
339-
var callInstr = Instruction.Create(OpCodes.Call, _cachedInterlockedIncMethod);
339+
var callInstr = Instruction.Create(OpCodes.Call, _customTrackerRecordHitMethod);
340340

341341
processor.InsertBefore(instruction, callInstr);
342342
processor.InsertBefore(callInstr, indxInstr);
343343

344344
return indxInstr;
345-
//if (_cachedInterlockedIncMethod == null)
346-
//{
347-
// _cachedInterlockedIncMethod = new MethodReference(
348-
// "Increment", method.Module.TypeSystem.Int32, method.Module.ImportReference(typeof(System.Threading.Interlocked)));
349-
// _cachedInterlockedIncMethod.Parameters.Add(new ParameterDefinition(new ByReferenceType(method.Module.TypeSystem.Int32)));
350-
//}
351-
352-
//var sfldInstr = Instruction.Create(OpCodes.Ldsfld, _customModuleTrackerHitsArray);
353-
//var indxInstr = Instruction.Create(OpCodes.Ldc_I4, hitEntryIndex);
354-
//var arefInstr = Instruction.Create(OpCodes.Ldelema, method.Module.TypeSystem.Int32);
355-
//var callInstr = Instruction.Create(OpCodes.Call, _cachedInterlockedIncMethod);
356-
//var popInstr = Instruction.Create(OpCodes.Pop);
357-
358-
//processor.InsertBefore(instruction, popInstr);
359-
//processor.InsertBefore(popInstr, callInstr);
360-
//processor.InsertBefore(callInstr, arefInstr);
361-
//processor.InsertBefore(arefInstr, indxInstr);
362-
//processor.InsertBefore(indxInstr, sfldInstr);
363-
364-
//return sfldInstr;
365345
}
366346

367347
private static void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)

0 commit comments

Comments
 (0)