@@ -23,11 +23,11 @@ internal class Instrumenter
23
23
private readonly string [ ] _includeFilters ;
24
24
private readonly string [ ] _excludedFiles ;
25
25
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 ;
29
29
private TypeDefinition _customTrackerTypeDef ;
30
- private MethodReference _cachedInterlockedIncMethod ;
30
+ private MethodReference _customTrackerRecordHitMethod ;
31
31
32
32
public Instrumenter ( string module , string identifier , string [ ] excludeFilters , string [ ] includeFilters , string [ ] excludedFiles )
33
33
{
@@ -83,12 +83,12 @@ private void InstrumentModule()
83
83
}
84
84
85
85
// 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 ) ) ;
92
92
93
93
module . Write ( stream ) ;
94
94
}
@@ -97,9 +97,9 @@ private void InstrumentModule()
97
97
98
98
private void AddCustomModuleTrackerToModule ( ModuleDefinition module )
99
99
{
100
- using ( AssemblyDefinition xad = AssemblyDefinition . ReadAssembly ( typeof ( Instrumenter ) . Assembly . Location ) )
100
+ using ( AssemblyDefinition coverletInstrumentationAssembly = AssemblyDefinition . ReadAssembly ( typeof ( Instrumenter ) . Assembly . Location ) )
101
101
{
102
- TypeDefinition moduleTrackerTemplate = xad . MainModule . GetType (
102
+ TypeDefinition moduleTrackerTemplate = coverletInstrumentationAssembly . MainModule . GetType (
103
103
"Coverlet.Core.Instrumentation" , nameof ( ModuleTrackerTemplate ) ) ;
104
104
105
105
_customTrackerTypeDef = new TypeDefinition (
@@ -114,9 +114,9 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
114
114
_customTrackerTypeDef . Fields . Add ( fieldClone ) ;
115
115
116
116
if ( fieldClone . Name == "HitsArray" )
117
- _customModuleTrackerHitsArray = fieldClone ;
117
+ _customTrackerHitsArray = fieldClone ;
118
118
else if ( fieldClone . Name == "HitsFilePath" )
119
- _customModuleTrackerHitsFilePath = fieldClone ;
119
+ _customTrackerHitsFilePath = fieldClone ;
120
120
}
121
121
122
122
foreach ( MethodDefinition methodDef in moduleTrackerTemplate . Methods )
@@ -140,7 +140,7 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
140
140
141
141
ILProcessor ilProcessor = methodOnCustomType . Body . GetILProcessor ( ) ;
142
142
if ( methodDef . Name == ".cctor" )
143
- _customModuleTrackerClassConstructorIl = ilProcessor ;
143
+ _customTrackerClassConstructorIl = ilProcessor ;
144
144
145
145
foreach ( Instruction instr in methodDef . Body . Instructions )
146
146
{
@@ -179,8 +179,8 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
179
179
module . Types . Add ( _customTrackerTypeDef ) ;
180
180
}
181
181
182
- Debug . Assert ( _customModuleTrackerHitsArray != null ) ;
183
- Debug . Assert ( _customModuleTrackerClassConstructorIl != null ) ;
182
+ Debug . Assert ( _customTrackerHitsArray != null ) ;
183
+ Debug . Assert ( _customTrackerClassConstructorIl != null ) ;
184
184
}
185
185
186
186
private void InstrumentType ( TypeDefinition type )
@@ -328,40 +328,20 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
328
328
329
329
private Instruction AddInstrumentationInstructions ( MethodDefinition method , ILProcessor processor , Instruction instruction , int hitEntryIndex )
330
330
{
331
- if ( _cachedInterlockedIncMethod == null )
331
+ if ( _customTrackerRecordHitMethod == null )
332
332
{
333
- _cachedInterlockedIncMethod = new MethodReference (
333
+ _customTrackerRecordHitMethod = new MethodReference (
334
334
"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 ) ) ;
336
336
}
337
337
338
338
var indxInstr = Instruction . Create ( OpCodes . Ldc_I4 , hitEntryIndex ) ;
339
- var callInstr = Instruction . Create ( OpCodes . Call , _cachedInterlockedIncMethod ) ;
339
+ var callInstr = Instruction . Create ( OpCodes . Call , _customTrackerRecordHitMethod ) ;
340
340
341
341
processor . InsertBefore ( instruction , callInstr ) ;
342
342
processor . InsertBefore ( callInstr , indxInstr ) ;
343
343
344
344
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;
365
345
}
366
346
367
347
private static void ReplaceInstructionTarget ( Instruction instruction , Instruction oldTarget , Instruction newTarget )
0 commit comments