This repository was archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathBasePersistance.cs
1013 lines (923 loc) · 45.2 KB
/
BasePersistance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using OpenCover.Framework.Communication;
using OpenCover.Framework.Model;
using OpenCover.Framework.Utility;
using log4net;
namespace OpenCover.Framework.Persistance
{
/// <summary>
/// A basic layer that aggregates the data
/// </summary>
public abstract class BasePersistance : IPersistance
{
private static readonly object Protection = new object();
/// <summary>
/// Provides subclasses access to the command line object
/// </summary>
protected readonly ICommandLine CommandLine;
private readonly ILog _logger;
private uint _trackedMethodId;
private readonly Dictionary<Module, Dictionary<int, KeyValuePair<Class, Method>>> _moduleMethodMap = new Dictionary<Module, Dictionary<int, KeyValuePair<Class, Method>>>();
private static readonly ILog DebugLogger = LogManager.GetLogger("DebugLogger");
/// <summary>
/// constructor
/// </summary>
/// <param name="commandLine"></param>
/// <param name="logger"></param>
protected BasePersistance(ICommandLine commandLine, ILog logger)
{
CommandLine = commandLine;
_logger = logger ?? DebugLogger;
CoverageSession = new CoverageSession();
_trackedMethodId = 0;
}
/// <summary>
/// A coverage session
/// </summary>
public CoverageSession CoverageSession { get; private set; }
/// <summary>
/// Add the <see cref="Module"/> to the current session
/// </summary>
/// <param name="module"></param>
public void PersistModule(Module module)
{
lock (Protection)
{
if (module == null)
return;
module.Classes = module.Classes ?? new Class[0];
if (CommandLine.MergeByHash)
{
var modules = CoverageSession.Modules ?? new Module[0];
var existingModule = modules.FirstOrDefault(x => x.ModuleHash == module.ModuleHash);
if (existingModule != null)
{
var exists = existingModule.Aliases
.Any(x => x.Equals(module.ModulePath, StringComparison.InvariantCultureIgnoreCase));
if (!exists)
{
existingModule.Aliases.Add(module.ModulePath);
}
return;
}
}
_moduleMethodMap[module] = new Dictionary<int, KeyValuePair<Class, Method>>();
BuildMethodMapForModule(module);
var list = new List<Module>(CoverageSession.Modules ?? new Module[0]) { module };
CoverageSession.Modules = list.ToArray();
}
}
/// <summary>
/// Clear the current coverage session data
/// </summary>
protected void ClearCoverageSession()
{
_moduleMethodMap.Clear();
CoverageSession = new CoverageSession();
InstrumentationPoint.Clear();
}
/// <summary>
///
/// </summary>
/// <param name="session"></param>
protected void ReassignCoverageSession(CoverageSession session)
{
_moduleMethodMap.Clear();
CoverageSession = session;
CoverageSession.Summary = new Summary();
foreach (var module in CoverageSession.Modules)
{
BuildMethodMapForModule(module);
module.Summary = new Summary();
foreach (var @class in module.Classes)
{
@class.Summary = new Summary();
foreach (var method in @class.Methods)
{
method.Summary = new Summary();
if (method.SequencePoints.Any() && method.SequencePoints[0].Offset == method.MethodPoint.Offset)
{
var point = new[] { method.SequencePoints[0], (SequencePoint)method.MethodPoint }
.OrderBy(x => x.OrigSequencePoint)
.First();
method.MethodPoint = point;
method.SequencePoints[0] = point;
}
}
}
}
InstrumentationPoint.ResetAfterLoading();
File.ResetAfterLoading();
}
private void BuildMethodMapForModule(Module module)
{
_moduleMethodMap[module] = new Dictionary<int, KeyValuePair<Class, Method>>();
foreach (var @class in module.Classes)
{
foreach (var method in @class.Methods)
{
_moduleMethodMap[module][method.MetadataToken] = new KeyValuePair<Class, Method>(@class, method);
}
}
}
/// <summary>
/// Is the module being tracked
/// </summary>
/// <param name="modulePath"></param>
/// <returns></returns>
public bool IsTracking(string modulePath)
{
lock (Protection)
{
return CoverageSession.Modules.Any(x => x.Aliases.Any(path => path.Equals(modulePath, StringComparison.InvariantCultureIgnoreCase)) &&
!x.ShouldSerializeSkippedDueTo());
}
}
/// <summary>
/// we are done and the data needs one last clean up
/// </summary>
public virtual void Commit()
{
if (CoverageSession.Modules == null)
return;
MarkSkippedMethods();
TransformSequences();
CalculateCoverage();
if (CommandLine.HideSkipped == null || !CommandLine.HideSkipped.Any())
return;
foreach (var skippedMethod in CommandLine.HideSkipped.OrderBy(x => x))
ProcessSkippedAction(skippedMethod);
}
private void ProcessSkippedAction(SkippedMethod skippedMethod)
{
switch (skippedMethod)
{
case SkippedMethod.File:
RemoveSkippedMethods(SkippedMethod.File);
RemoveEmptyClasses();
RemoveUnreferencedFiles();
break;
case SkippedMethod.Filter:
RemoveSkippedModules(SkippedMethod.Filter);
RemoveSkippedClasses(SkippedMethod.Filter);
break;
case SkippedMethod.FolderExclusion:
RemoveSkippedModules(SkippedMethod.FolderExclusion);
break;
case SkippedMethod.MissingPdb:
RemoveSkippedModules(SkippedMethod.MissingPdb);
break;
case SkippedMethod.Attribute:
RemoveSkippedClasses(SkippedMethod.Attribute);
RemoveSkippedMethods(SkippedMethod.Attribute);
RemoveEmptyClasses();
break;
case SkippedMethod.AutoImplementedProperty:
RemoveSkippedMethods(SkippedMethod.AutoImplementedProperty);
RemoveEmptyClasses();
break;
case SkippedMethod.Delegate:
RemoveSkippedMethods(SkippedMethod.Delegate);
RemoveEmptyClasses();
break;
case SkippedMethod.FSharpInternal:
RemoveSkippedMethods(SkippedMethod.FSharpInternal);
RemoveEmptyClasses();
break;
}
}
private void RemoveSkippedModules(SkippedMethod skipped)
{
if (CoverageSession.Modules == null)
return;
var modules = CoverageSession.Modules;
modules = modules.Where(x => x.SkippedDueTo != skipped).ToArray();
CoverageSession.Modules = modules;
}
private void RemoveSkippedClasses(SkippedMethod skipped)
{
if (CoverageSession.Modules == null)
return;
foreach (var module in CoverageSession.Modules)
{
if (module.Classes == null)
continue;
var classes = module.Classes.Where(x => x.SkippedDueTo != skipped).ToArray();
module.Classes = classes;
}
}
private void RemoveSkippedMethods(SkippedMethod skipped)
{
if (CoverageSession.Modules == null)
return;
foreach (var module in CoverageSession.Modules)
{
if (module.Classes == null)
continue;
foreach (var @class in module.Classes)
{
if (@class.Methods == null)
continue;
var methods = @class.Methods.Where(x => x.SkippedDueTo != skipped).ToArray();
@class.Methods = methods;
}
}
}
private void RemoveEmptyClasses()
{
if (CoverageSession.Modules == null)
return;
foreach (var module in CoverageSession.Modules)
{
if (module.Classes == null)
continue;
module.Classes = module.Classes.Where(@class => @class.Methods != null && @class.Methods.Any()).ToArray();
}
}
private void RemoveUnreferencedFiles()
{
if (CoverageSession.Modules == null)
return;
foreach (var module in CoverageSession.Modules)
{
module.Files = (from file in module.Files ?? new File[0]
from @class in module.Classes ?? new Class[0]
where (@class.Methods ?? new Method[0]).Where(x => x.FileRef != null).Any(x => x.FileRefUniqueId == file.UniqueId)
select file).Distinct().ToArray();
}
}
private void MarkSkippedMethods()
{
foreach (var method in from @class in
(from module in CoverageSession.Modules
from @class in module.Classes ?? new Class[0]
select @class)
where @class.Methods.Any(m => m.ShouldSerializeSkippedDueTo())
where @class.Methods.All(m => m.FileRef == null)
from method in @class.Methods.Where(x => !x.ShouldSerializeSkippedDueTo())
select method)
{
method.MarkAsSkipped(SkippedMethod.Inferred);
}
}
private void CalculateCoverage()
{
foreach (var module in CoverageSession.Modules.Where(x => x != null && !x.ShouldSerializeSkippedDueTo()))
{
foreach (var @class in (module.Classes ?? new Class[0]).Where(x => x != null && !x.ShouldSerializeSkippedDueTo()))
{
foreach (var method in (@class.Methods ?? new Method[0]).Where(x => x != null && !x.ShouldSerializeSkippedDueTo()))
ProcessMethodData(method, @class);
ProcessClassData(@class, module);
}
ProcessModuleData(module);
}
CalculateCoverage(CoverageSession.Summary);
}
private void ProcessModuleData(Module module)
{
AddPoints(CoverageSession.Summary, module.Summary);
CalculateCoverage(module.Summary);
if (CoverageSession.Summary.MinCrapScore == 0)
{
CoverageSession.Summary.MinCrapScore = module.Summary.MinCrapScore;
}
CoverageSession.Summary.MinCrapScore = Math.Min(CoverageSession.Summary.MinCrapScore, module.Summary.MinCrapScore);
CoverageSession.Summary.MaxCrapScore = Math.Max(CoverageSession.Summary.MaxCrapScore, module.Summary.MaxCrapScore);
if (CoverageSession.Summary.MinCyclomaticComplexity == 0)
CoverageSession.Summary.MinCyclomaticComplexity = module.Summary.MinCyclomaticComplexity;
CoverageSession.Summary.MinCyclomaticComplexity = Math.Min(CoverageSession.Summary.MinCyclomaticComplexity,
module.Summary.MinCyclomaticComplexity);
CoverageSession.Summary.MaxCyclomaticComplexity = Math.Max(CoverageSession.Summary.MaxCyclomaticComplexity,
module.Summary.MaxCyclomaticComplexity);
}
private static void ProcessClassData(Class @class, Module module)
{
@class.Summary.NumClasses = (@class.Summary.NumMethods > 0) ? 1 : 0;
@class.Summary.VisitedClasses = (@class.Summary.VisitedMethods > 0) ? 1 : 0;
AddPoints(module.Summary, @class.Summary);
CalculateCoverage(@class.Summary);
if (module.Summary.MinCrapScore == 0)
{
module.Summary.MinCrapScore = @class.Summary.MinCrapScore;
}
module.Summary.MinCrapScore = Math.Min(module.Summary.MinCrapScore, @class.Summary.MinCrapScore);
module.Summary.MaxCrapScore = Math.Max(module.Summary.MaxCrapScore, @class.Summary.MaxCrapScore);
if (module.Summary.MinCyclomaticComplexity == 0)
module.Summary.MinCyclomaticComplexity = @class.Summary.MinCyclomaticComplexity;
module.Summary.MinCyclomaticComplexity = Math.Min(module.Summary.MinCyclomaticComplexity,
@class.Summary.MinCyclomaticComplexity);
module.Summary.MaxCyclomaticComplexity = Math.Max(module.Summary.MaxCyclomaticComplexity,
@class.Summary.MaxCyclomaticComplexity);
}
private static void ProcessMethodData(Method method, Class @class)
{
if (method.MethodPoint != null)
{
method.Visited = (method.MethodPoint.VisitCount > 0);
}
method.Summary.NumBranchPoints = method.BranchPoints.Length;
method.Summary.VisitedBranchPoints = method.BranchPoints.Count(pt => pt.VisitCount != 0);
method.Summary.NumSequencePoints = method.SequencePoints.Length;
method.Summary.VisitedSequencePoints = method.SequencePoints.Count(pt => pt.VisitCount != 0);
if (method.Summary.NumSequencePoints > 0)
method.Summary.NumBranchPoints += 1;
if (method.Summary.VisitedSequencePoints > 0)
method.Summary.VisitedBranchPoints += 1;
if (method.FileRef != null)
{
method.Summary.NumMethods = 1;
method.Summary.VisitedMethods = (method.Visited) ? 1 : 0;
}
AddPoints(@class.Summary, method.Summary);
CalculateCoverage(method.Summary);
method.SequenceCoverage = method.Summary.SequenceCoverage;
method.BranchCoverage = method.Summary.BranchCoverage;
CalculateCrapScore(method, @class);
CalculateNPathComplexity(method);
CalculateCyclomaticComplexity(method, @class);
}
private static void CalculateCrapScore(Method method, Class @class)
{
// CRAP score calculation
// CRAP(m) = CC(m)^2 * U(m)^3 + CC(m)
// see https://blog.ndepend.com/crap-metric-thing-tells-risk-code/
var cyclomaticComplexitySquared = (decimal)Math.Pow(method.CyclomaticComplexity, 2);
var precentageOfUncoveredCodeCubed = (decimal)Math.Pow(1.0 - (double)(method.SequenceCoverage / (decimal)100.0), 3.0);
method.CrapScore = Math.Round((cyclomaticComplexitySquared * precentageOfUncoveredCodeCubed) +
method.CyclomaticComplexity, 2);
// TODO: is 0 a possible crap score?
method.Summary.MinCrapScore = Math.Max(0, method.CrapScore);
method.Summary.MaxCrapScore = method.Summary.MinCrapScore;
if (@class.Summary.MinCrapScore == 0)
{
@class.Summary.MinCrapScore = method.Summary.MinCrapScore;
}
@class.Summary.MinCrapScore = Math.Min(@class.Summary.MinCrapScore, method.CrapScore);
@class.Summary.MaxCrapScore = Math.Max(@class.Summary.MaxCrapScore, method.CrapScore);
}
private static void CalculateCyclomaticComplexity(Method method, Class @class)
{
method.Summary.MinCyclomaticComplexity = Math.Max(1, method.CyclomaticComplexity);
method.Summary.MaxCyclomaticComplexity = method.Summary.MinCyclomaticComplexity;
if (@class.Summary.MinCyclomaticComplexity == 0)
@class.Summary.MinCyclomaticComplexity = method.Summary.MinCyclomaticComplexity;
@class.Summary.MinCyclomaticComplexity = Math.Min(@class.Summary.MinCyclomaticComplexity,
method.CyclomaticComplexity);
@class.Summary.MaxCyclomaticComplexity = Math.Max(@class.Summary.MaxCyclomaticComplexity,
method.CyclomaticComplexity);
}
private static void CalculateNPathComplexity(Method method)
{
method.NPathComplexity = 0;
var nPaths = new Dictionary<int, int>();
if (method.BranchPoints.Any())
{
foreach (var bp in method.BranchPoints.Where(b => b != null))
{
if (nPaths.ContainsKey(bp.Offset))
{
nPaths[bp.Offset] += 1;
}
else
{
nPaths.Add(bp.Offset, 1);
}
}
}
foreach (var branches in nPaths.Values)
{
if (method.NPathComplexity == 0)
{
method.NPathComplexity = branches;
}
else
{
try
{
method.NPathComplexity = checked(method.NPathComplexity * branches);
}
catch (OverflowException)
{
method.NPathComplexity = int.MaxValue;
break;
}
}
}
}
private static void MapFileReferences(IEnumerable<IDocumentReference> points, IDictionary<string, uint> filesDictionary)
{
foreach (var pt in points.Where(p => p.FileId == 0))
{
filesDictionary.TryGetValue(pt.Document ?? "", out uint fileid);
pt.FileId = fileid;
// clear document if FileId is found
pt.Document = pt.FileId != 0 ? null : pt.Document;
}
}
private static void CalculateCoverage(Summary summary)
{
if (summary.NumSequencePoints > 0)
summary.SequenceCoverage = Math.Round((summary.VisitedSequencePoints * 100m) / summary.NumSequencePoints, 2);
if (summary.NumBranchPoints > 0)
summary.BranchCoverage = Math.Round((summary.VisitedBranchPoints * 100m) / summary.NumBranchPoints, 2);
}
private static void AddPoints(Summary parent, Summary child)
{
parent.NumBranchPoints += child.NumBranchPoints;
parent.VisitedBranchPoints += child.VisitedBranchPoints;
parent.NumSequencePoints += child.NumSequencePoints;
parent.VisitedSequencePoints += child.VisitedSequencePoints;
parent.NumClasses += child.NumClasses;
parent.VisitedClasses += child.VisitedClasses;
parent.NumMethods += child.NumMethods;
parent.VisitedMethods += child.VisitedMethods;
}
/// <summary>
/// Get the sequence points for a function
/// </summary>
/// <param name="modulePath"></param>
/// <param name="functionToken"></param>
/// <param name="sequencePoints"></param>
/// <returns></returns>
public bool GetSequencePointsForFunction(string modulePath, int functionToken, out InstrumentationPoint[] sequencePoints)
{
sequencePoints = new InstrumentationPoint[0];
var method = GetMethod(modulePath, functionToken, out Class @class);
if (method != null && method.SequencePoints.Any())
{
System.Diagnostics.Debug.WriteLine("Getting Sequence points for {0}({1})", method.FullName, method.MetadataToken);
var points = new List<InstrumentationPoint>();
if (!(method.MethodPoint is SequencePoint))
points.Add(method.MethodPoint);
points.AddRange(method.SequencePoints);
sequencePoints = points.ToArray();
return true;
}
return false;
}
/// <summary>
/// Get the branch ponts for a function
/// </summary>
/// <param name="modulePath"></param>
/// <param name="functionToken"></param>
/// <param name="branchPoints"></param>
/// <returns></returns>
public bool GetBranchPointsForFunction(string modulePath, int functionToken, out BranchPoint[] branchPoints)
{
branchPoints = new BranchPoint[0];
var method = GetMethod(modulePath, functionToken, out Class @class);
if (method != null && method.BranchPoints.Any())
{
System.Diagnostics.Debug.WriteLine("Getting Branch points for {0}({1})", method.FullName, method.MetadataToken);
branchPoints = method.BranchPoints.ToArray();
return true;
}
return false;
}
/// <summary>
/// Get <see cref="Method"/> data for a function
/// </summary>
/// <param name="modulePath"></param>
/// <param name="functionToken"></param>
/// <param name="class"></param>
/// <returns></returns>
private Method GetMethod(string modulePath, int functionToken, out Class @class)
{
@class = null;
lock (Protection)
{
var module = CoverageSession.Modules
.FirstOrDefault(x => x.Aliases.Any(path => path.Equals(modulePath, StringComparison.InvariantCultureIgnoreCase)));
if (module == null)
return null;
if (!_moduleMethodMap[module].ContainsKey(functionToken))
return null;
var pair = _moduleMethodMap[module][functionToken];
@class = pair.Key;
return pair.Value;
}
}
/// <summary>
/// Get the class name of a function
/// </summary>
/// <param name="modulePath"></param>
/// <param name="functionToken"></param>
/// <returns></returns>
public string GetClassFullName(string modulePath, int functionToken)
{
GetMethod(modulePath, functionToken, out Class @class);
return @class?.FullName;
}
/// <summary>
/// Save the visit data to the session model
/// </summary>
/// <param name="data"></param>
public void SaveVisitData(byte[] data)
{
var nCount = BitConverter.ToUInt32(data, 0);
if (nCount > (data.Count() / 4) - 1)
{
_logger.ErrorFormat("Failed to process points as count ({0}) exceeded available buffer size ({1})",
nCount, (data.Count() / 4) - 1);
return;
}
for (int i = 0, idx = 4; i < nCount; i++, idx += 4)
{
var spid = BitConverter.ToUInt32(data, idx);
if (spid < (uint)MSG_IdType.IT_MethodEnter)
{
if (!InstrumentationPoint.AddVisitCount(spid, _trackedMethodId, 1))
{
_logger.ErrorFormat("Failed to add a visit to {0} with tracking method {1}. Max point count is {2}",
spid, _trackedMethodId, InstrumentationPoint.Count);
}
}
else
{
var tmId = spid & (uint)MSG_IdType.IT_Mask;
_trackedMethodId = (spid & (uint)MSG_IdType.IT_MethodEnter) != 0 ? tmId : 0;
}
}
}
/// <summary>
/// determine if the method (test method) should be tracked
/// </summary>
/// <param name="modulePath"></param>
/// <param name="assemblyName"></param>
/// <param name="functionToken"></param>
/// <param name="uniqueId"></param>
/// <returns></returns>
public bool GetTrackingMethod(string modulePath, string assemblyName, int functionToken, out uint uniqueId)
{
lock (Protection)
{
uniqueId = 0;
foreach (var module in CoverageSession.Modules
.Where(x => x.TrackedMethods != null)
.Where(x => x.Aliases.Contains(modulePath)))
{
foreach (var trackedMethod in module.TrackedMethods)
{
if (trackedMethod.MetadataToken == functionToken)
{
uniqueId = trackedMethod.UniqueId;
return true;
}
}
}
}
return false;
}
private void TransformSequences()
{
var sessionModulesQuery = CoverageSession.Modules.Where(module => module != null && !module.ShouldSerializeSkippedDueTo());
foreach (var module in sessionModulesQuery)
{
var moduleClassesQuery = (module.Classes ?? new Class[0]).Where(x => x != null && !x.ShouldSerializeSkippedDueTo());
var moduleMethodsQuery = moduleClassesQuery.SelectMany(@class => (@class.Methods ?? new Method[0])).Where(x => x != null && !x.ShouldSerializeSkippedDueTo());
var methods = moduleMethodsQuery.ToArray();
if (methods.Any())
{
var sourceRepository = new SourceRepository(); // module sources
// keep transformations in this order!
TransformSequences_Initialize(methods);
TransformSequences_JoinWithBranches(methods);
TransformSequences_AddSources(module.Files, methods, sourceRepository);
TransformSequences_RemoveCompilerGeneratedBranches(methods, sourceRepository, module.ModuleTime);
TransformSequences_RemoveFalsePositiveUnvisited(methods, sourceRepository, module.ModuleTime);
TransformSequences_ReduceBranches(methods); // last
}
}
}
static void TransformSequences_Initialize(IEnumerable<Method> methods)
{
foreach (var method in methods)
{
// No sequences in method, but branches present? => remove branches
if (method.SequencePoints.Length == 0 && method.BranchPoints.Length != 0)
{
method.BranchPoints = new BranchPoint[0];
}
}
}
private static void TransformSequences_JoinWithBranches(IEnumerable<Method> methods)
{
foreach (var method in methods)
{
if (method.SequencePoints.Length != 0 && method.BranchPoints.Length != 0)
{
// Quick match branches to sequence using SP&BP sort order by IL offset
// SP & BP are sorted by offset and code below expect both SP & BP to be sorted by offset
// ATTN: Sorted again to prevent future bugs if order of SP & BP is changed!
method.SequencePoints = method.SequencePoints.OrderBy(sp => sp.Offset).ToArray();
method.BranchPoints = method.BranchPoints.OrderBy(bp => bp.Offset).ToArray();
// Use stack because Stack.Pop is constant time
var branchStack = new Stack<BranchPoint>(method.BranchPoints);
// Join offset matching BranchPoints with SequencePoint "parent"
// Exclude all branches where BranchPoint.Offset < first method.SequencePoints.Offset
// Reverse() starts loop from highest offset to lowest
foreach (SequencePoint spParent in method.SequencePoints.Reverse())
{
// create branchPoints "child" list
spParent.BranchPoints = new List<BranchPoint>();
// if BranchPoint.Offset is >= SequencePoint.Offset
// then move BranchPoint from stack to "child" list (Pop/Add)
while (branchStack.Count != 0 && branchStack.Peek().Offset >= spParent.Offset)
{
spParent.BranchPoints.Add(branchStack.Pop());
}
}
}
}
}
private static void TransformSequences_AddSources(IEnumerable<File> files, IEnumerable<Method> methods, IDictionary<uint, CodeCoverageStringTextSource> sourceRepository)
{
if (files == null || !files.Any())
return;
// Dictionary with stored source file names per module
var filesDictionary = new Dictionary<string, uint>();
foreach (var file in files.
Where(file => !String.IsNullOrWhiteSpace(file.FullPath)
&& !filesDictionary.ContainsKey(file.FullPath)))
{
var source = CodeCoverageStringTextSource.GetSource(file.FullPath); // never reurns null
if (source.FileType == FileType.CSharp)
sourceRepository.Add(file.UniqueId, source);
filesDictionary.Add(file.FullPath, file.UniqueId);
}
foreach (var method in methods)
{
if (method.SequencePoints.Length != 0)
MapFileReferences(method.SequencePoints, filesDictionary);
if (method.BranchPoints.Length != 0)
MapFileReferences(method.BranchPoints, filesDictionary);
}
}
private static void TransformSequences_RemoveCompilerGeneratedBranches(IEnumerable<Method> methods, SourceRepository sourceRepository, DateTime moduleTime)
{
foreach (var method in methods
.Where(m => m.FileRefUniqueId != 0
&& m.SequencePoints.Length != 0))
{
// Get method source if availabile
var source = sourceRepository.GetCodeCoverageStringTextSource(method.FileRefUniqueId);
if (source != null)
{
if (source.IsChanged(moduleTime))
{
("Source file is modified: " + source.FilePath).InformUser();
return;
}
if (!TransformSequences_RemoveCompilerGeneratedBranches(method, source))
{
return; // empty sequence found -> source.IsChanged (failed access to file-times)
}
}
}
}
private static bool TransformSequences_RemoveCompilerGeneratedBranches(Method method, CodeCoverageStringTextSource source)
{
// Do we have C# source?
if (source.FileType == FileType.CSharp)
{
if (source.FileFound)
{
// initialize offset with unreachable values
long startOffset = long.MinValue;
long finalOffset = long.MaxValue;
if (!method.IsGenerated)
{
// fill offsets with values
TransformSequences_RemoveCompilerGeneratedBranches(method, source, ref startOffset, ref finalOffset);
}
if (!TransformSequences_RemoveCompilerGeneratedBranches(method, source, startOffset, finalOffset))
{
return false; // return error/failure to caller
}
}
else
{
// Do as much possible without source
// This will remove generated branches within "{", "}" and "in" (single-line SequencePoints)
// but cannot remove Code Contract ccrewite generated branches
foreach (var sp in method.SequencePoints)
{
if (sp.BranchPoints.Count != 0
&& sp.StartLine == sp.EndLine
&& sp.EndColumn - sp.StartColumn <= 2)
{
// Zero, one or two character sequence point should not contain branches
// Never found 0 character sequencePoint
// Never found 1 character sequencePoint except "{" and "}"
// Never found 2 character sequencePoint except "in" keyword
// Afaik, c# cannot express branch condition in one or two characters of source code
// Keyword "do" does not generate SequencePoint
sp.BranchPoints = new List<BranchPoint>();
}
}
}
}
return true;
}
private static void TransformSequences_RemoveCompilerGeneratedBranches(Method method, CodeCoverageStringTextSource source, ref long startOffset, ref long finalOffset)
{
// order SequencePoints by source order (Line/Column)
var sourceLineOrderedSps = method.SequencePoints.OrderBy(sp => sp.StartLine).ThenBy(sp => sp.StartColumn).Where(sp => sp.FileId == method.FileRefUniqueId).ToArray();
// get "{" if on first two positions
for (int index = 0; index < Math.Min(2, sourceLineOrderedSps.Length); index++)
{
if (source.GetText(sourceLineOrderedSps[0]) == "{")
{
startOffset = sourceLineOrderedSps[0].Offset;
break;
}
}
// get "}" if on last position
if (source.GetText(sourceLineOrderedSps.Last()) == "}")
{
finalOffset = sourceLineOrderedSps.Last().Offset;
}
}
// Compiled for speed, treat as .Singleline for multiline SequencePoint, do not waste time to capture Groups (speed)
private static readonly RegexOptions regexOptions = RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture;
// "Contract" and "." and "Requires/Ensures" can be separated by spaces and newlines (valid c# syntax)!
private static readonly Regex contractRegex = new Regex(@"^Contract\s*\.\s*(Requi|Ensu)res", regexOptions);
private static bool TransformSequences_RemoveCompilerGeneratedBranches(Method method, CodeCoverageStringTextSource source, long startOffset, long finalOffset)
{
// foreach sequence point
foreach (var sp in method.SequencePoints
.Where(sp => sp.FileId == method.FileRefUniqueId
&& sp.BranchPoints.Count != 0))
{
if (sp.Offset <= startOffset || sp.Offset >= finalOffset)
{
// doRemoveBranches where .Offset <= startOffset"{" or finalOffset"}" <= .Offset
// this will exclude "{" and "}" compiler generated branches and majority of ccrewrite Code-Contract's
sp.BranchPoints = new List<BranchPoint>();
}
else
{ // branches not removed
// check for other options by reading SequencePoint source
var text = source.GetText(sp); // text is never null
if (text.Length == 0)
{
("Empty sequence-point at line: " + sp.StartLine + " column: " + sp.StartColumn).InformUser();
("Source file: " + source.FilePath).InformUser();
return false; // signal error to caller's caller loop (break)
}
// Contract.Requires/Ensures is occasionally left inside method offset
// Quick check for "C" before using Regex
// Use Regex here! "Contract" and "." and "Requires/Ensures"
// can be separated by spaces and newlines
if (text[0] == 'C' && contractRegex.IsMatch(text))
{
sp.BranchPoints = new List<BranchPoint>();
}
// "in" keyword?
if (text == "in")
{
// Remove generated ::MoveNext branches within "in" keyword
// Not always removed in CecilSymbolManager (enumerated KeyValuePair)
sp.BranchPoints = new List<BranchPoint>();
}
}
}
return true;
}
private static void TransformSequences_RemoveFalsePositiveUnvisited(IEnumerable<Method> methods, SourceRepository sourceRepository, DateTime moduleTime)
{
var sequencePointsSet = new HashSet<SequencePoint>(new SequencePointComparer());
var toRemoveMethodSequencePoint = new List<Tuple<Method, SequencePoint>>();
// Initialise sequencePointsSet
TransformSequences_RemoveFalsePositiveUnvisited(methods, sequencePointsSet);
// Check generated methods
foreach (var method in methods
.Where(m => m.FileRefUniqueId != 0
&& m.SequencePoints.Length != 0
&& m.IsGenerated))
{
// Select duplicate and false-positive unvisited sequence points
// (Sequence point duplicated by generated method and left unvisited)
foreach (var sp in method.SequencePoints
.Where(sp => sp.FileId == method.FileRefUniqueId
&& sp.VisitCount == 0))
{
if (sequencePointsSet.Contains(sp))
{
// Unvisited duplicate found, add to remove list
toRemoveMethodSequencePoint.Add(new Tuple<Method, SequencePoint>(method, sp));
}
}
// Get method source if availabile
var source = sourceRepository.GetCodeCoverageStringTextSource(method.FileRefUniqueId);
if (source != null && !source.IsChanged(moduleTime))
{
TransformSequences_RemoveFalsePositiveUnvisited(method, source, toRemoveMethodSequencePoint);
}
}
// Remove selected SequencePoints
foreach (var tuple in toRemoveMethodSequencePoint)
{
tuple.Item1.SequencePoints = tuple.Item1.SequencePoints.Where(sp => sp != tuple.Item2).ToArray();
}
}
private static void TransformSequences_RemoveFalsePositiveUnvisited(IEnumerable<Method> methods, ISet<SequencePoint> sequencePointsSet)
{
// From Methods with Source and visited SequencePoints
var sequencePointsQuery = methods.Where(m => m.FileRefUniqueId != 0 && m.SequencePoints.Length != 0).SelectMany(m => m.SequencePoints).Where(sp => sp.FileId != 0 && sp.VisitCount != 0);
// Add unique visited SequencePoints to HashSet
foreach (var sp in sequencePointsQuery)
{
if (!sequencePointsSet.Contains(sp))
{
sequencePointsSet.Add(sp);
}
}
}
private static void TransformSequences_RemoveFalsePositiveUnvisited(Method method, CodeCoverageStringTextSource source, ICollection<Tuple<Method, SequencePoint>> toRemoveMethodSequencePoint)
{
// Select false unvisited right-curly-braces at generated "MoveNext" method
// (Curly braces moved to generated "MoveNext" method and left unvisited)
// Source is required here to identify curly braces
if (method.CallName == "MoveNext" && source.FileFound && source.FileType == FileType.CSharp)
{
int countDown = 2; // remove up to two last right-curly-braces
foreach (var sp in method.SequencePoints.Reverse())
{
if (sp.FileId == method.FileRefUniqueId
&& sp.IsSingleCharSequencePoint
&& sp.VisitCount == 0)
{ // unvisited only
if (countDown > 0)
{
if (source.GetText(sp) == "}")
{
toRemoveMethodSequencePoint.Add(new Tuple<Method, SequencePoint>(method, sp));
countDown -= 1;
}
}
else
{
break;
}
}
}
}
}
/// <summary>
/// Computes reduced SequencePoint branch coverage
/// by finding common exit offset (switch/case)
/// </summary>
/// <param name="methods"></param>
private static void TransformSequences_ReduceBranches(IEnumerable<Method> methods)
{
foreach (var method in methods)
{
// Collection of validBranchPoints (child/connected to parent SequencePoint)
var validBranchPoints = new List<BranchPoint>();
var branchExits = new Dictionary<int, BranchPoint>();
foreach (var sp in method.SequencePoints)
{
// SequencePoint has branches attached?
if (sp.BranchPoints.Count != 0)
{
// Merge sp.BranchPoints using EndOffset as branchExits key
branchExits.Clear();
foreach (var branchPoint in sp.BranchPoints)
{
if (!branchExits.ContainsKey(branchPoint.EndOffset))
{
branchExits[branchPoint.EndOffset] = branchPoint;
// insert branch
}
else
{
branchExits[branchPoint.EndOffset].VisitCount += branchPoint.VisitCount;
// update branch
}
}
// Update SequencePoint counters
sp.BranchExitsCount = 0;
sp.BranchExitsVisit = 0;
foreach (var branchPoint in branchExits.Values)
{
sp.BranchExitsCount += 1;
sp.BranchExitsVisit += branchPoint.VisitCount == 0 ? 0 : 1;
}
// Add to validBranchPoints