From 2b4f9e415bcde7752e9cefd7902bc2444e351c1a Mon Sep 17 00:00:00 2001 From: Simon Potter Date: Thu, 26 Apr 2018 16:37:13 -0400 Subject: [PATCH] Ensure disposables are wrapped in using statements. Avoids locking on file handles. --- .../Helpers/InstrumentationHelper.cs | 3 +- .../Instrumentation/Instrumenter.cs | 39 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/coverlet.core/Helpers/InstrumentationHelper.cs b/src/coverlet.core/Helpers/InstrumentationHelper.cs index f88af59..6e19744 100644 --- a/src/coverlet.core/Helpers/InstrumentationHelper.cs +++ b/src/coverlet.core/Helpers/InstrumentationHelper.cs @@ -19,7 +19,8 @@ namespace Coverlet.Core.Helpers public static bool HasPdb(string module) { - using (var peReader = new PEReader(File.OpenRead(module))) + using (var moduleStream = File.OpenRead(module)) + using (var peReader = new PEReader(moduleStream)) { foreach (var entry in peReader.ReadDebugDirectory()) { diff --git a/src/coverlet.core/Instrumentation/Instrumenter.cs b/src/coverlet.core/Instrumentation/Instrumenter.cs index 27f1720..27b2ed4 100644 --- a/src/coverlet.core/Instrumentation/Instrumenter.cs +++ b/src/coverlet.core/Instrumentation/Instrumenter.cs @@ -1,9 +1,7 @@ -using System.Collections.Generic; using System.IO; using System.Linq; using Coverlet.Core.Helpers; -using Coverlet.Core.Extensions; using Mono.Cecil; using Mono.Cecil.Cil; @@ -13,8 +11,8 @@ namespace Coverlet.Core.Instrumentation { internal class Instrumenter { - private string _module; - private string _identifier; + private readonly string _module; + private readonly string _identifier; private InstrumenterResult _result; public Instrumenter(string module, string identifier) @@ -48,25 +46,26 @@ namespace Coverlet.Core.Instrumentation private void InstrumentModule() { using (var stream = new FileStream(_module, FileMode.Open, FileAccess.ReadWrite)) + using (var resolver = new DefaultAssemblyResolver()) { - var resolver = new DefaultAssemblyResolver(); resolver.AddSearchDirectory(Path.GetDirectoryName(_module)); var parameters = new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver }; - ModuleDefinition module = ModuleDefinition.ReadModule(stream, parameters); - - foreach (var type in module.GetTypes()) + using (var module = ModuleDefinition.ReadModule(stream, parameters)) { - if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage")) - continue; - - foreach (var method in type.Methods) + foreach (var type in module.GetTypes()) { - if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage")) - InstrumentMethod(method); - } - } + if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage")) + continue; - module.Write(stream); + foreach (var method in type.Methods) + { + if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage")) + InstrumentMethod(method); + } + } + + module.Write(stream); + } } } @@ -138,7 +137,7 @@ namespace Coverlet.Core.Instrumentation return pathInstr; } - private bool IsBranchTarget(ILProcessor processor, Instruction instruction) + private static bool IsBranchTarget(ILProcessor processor, Instruction instruction) { foreach (var _instruction in processor.Body.Instructions) { @@ -155,7 +154,7 @@ namespace Coverlet.Core.Instrumentation return false; } - private void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget) + private static void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget) { if (instruction.Operand is Instruction _instruction) { @@ -175,7 +174,7 @@ namespace Coverlet.Core.Instrumentation } } - private void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget) + private static void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget) { if (handler.FilterStart == oldTarget) handler.FilterStart = newTarget;