Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 44b7357

Browse files
author
Dragan
committed
CodeCoverage: Apply ccrewrite filter on C# only
Apply Branch-Coverage Code-Contract ccrewrite filter only to C#(.cs) files
1 parent 164af14 commit 44b7357

1 file changed

Lines changed: 54 additions & 42 deletions

File tree

src/AddIns/Analysis/CodeCoverage/Project/Src/CodeCoverageMethodElement.cs

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void Init()
9292
if (results != null) {
9393
this.FileName = results.GetFileName(this.FileID);
9494
try {
95-
this.FileNameExt = Path.GetExtension(this.FileName);
95+
this.FileNameExt = Path.GetExtension(this.FileName).ToLowerInvariant();
9696
}
9797
catch {}
9898
if (cacheFileName != this.FileName) {
@@ -226,14 +226,19 @@ void GetSequencePointContent(CodeCoverageSequencePoint sp)
226226
// -> this method SP with lowest Line/Column
227227
void getBodyStartSP() {
228228
if (this.SequencePoints.Count != 0) {
229-
foreach (CodeCoverageSequencePoint sp in this.SequencePoints) {
230-
if (sp.FileID != this.FileID) continue;
231-
if (this.BodyStartSP == null || (sp.Line < this.BodyStartSP.Line) ||
232-
(sp.Line == this.BodyStartSP.Line && sp.Column < this.BodyStartSP.Column)
233-
) {
234-
this.BodyStartSP = sp;
229+
if (this.FileNameExt == ".cs") {
230+
foreach (CodeCoverageSequencePoint sp in this.SequencePoints) {
231+
if (sp.FileID != this.FileID) continue;
232+
if (this.BodyStartSP == null || (sp.Line < this.BodyStartSP.Line) ||
233+
(sp.Line == this.BodyStartSP.Line && sp.Column < this.BodyStartSP.Column)
234+
) {
235+
this.BodyStartSP = sp;
236+
}
235237
}
236238
}
239+
else {
240+
this.BodyStartSP = this.SequencePoints.First();
241+
}
237242
}
238243
}
239244

@@ -242,29 +247,34 @@ void getBodyStartSP() {
242247
// and lowest Offset (when duplicated bw ccrewrite)
243248
void getBodyFinalSP() {
244249
if (this.SequencePoints.Count != 0) {
245-
for (int i = this.SequencePoints.Count-1; i > 0; i--) {
246-
var sp = this.SequencePoints[i];
247-
if (sp.FileID != this.FileID) continue;
248-
if (sp.Content != "}") continue;
249-
if (this.BodyFinalSP == null || (sp.Line > this.BodyFinalSP.Line) ||
250-
(sp.Line == this.BodyFinalSP.Line && sp.Column >= this.BodyFinalSP.Column)
251-
) {
252-
// ccrewrite ContractClass/ContractClassFor
253-
// adds duplicate method end-sequence-point "}"
254-
//
255-
// Take duplicate BodyFinalSP with lower Offset
256-
// Because IL.Offset of second duplicate
257-
// will extend branch coverage of this method
258-
// by coverage of ContractClassFor inserted SequencePoint!
259-
if (this.BodyFinalSP != null &&
260-
sp.Line == this.BodyFinalSP.Line &&
261-
sp.Column == this.BodyFinalSP.Column &&
262-
sp.Offset < this.BodyFinalSP.Offset) {
263-
this.SequencePoints.Remove(this.BodyFinalSP); // remove duplicate
250+
if (this.FileNameExt == ".cs") {
251+
for (int i = this.SequencePoints.Count-1; i > 0; i--) {
252+
var sp = this.SequencePoints[i];
253+
if (sp.FileID != this.FileID) continue;
254+
if (sp.Content != "}") continue;
255+
if (this.BodyFinalSP == null || (sp.Line > this.BodyFinalSP.Line) ||
256+
(sp.Line == this.BodyFinalSP.Line && sp.Column >= this.BodyFinalSP.Column)
257+
) {
258+
// ccrewrite ContractClass/ContractClassFor
259+
// adds duplicate method end-sequence-point "}"
260+
//
261+
// Take duplicate BodyFinalSP with lower Offset
262+
// Because IL.Offset of second duplicate
263+
// will extend branch coverage of this method
264+
// by coverage of ContractClassFor inserted SequencePoint!
265+
if (this.BodyFinalSP != null &&
266+
sp.Line == this.BodyFinalSP.Line &&
267+
sp.Column == this.BodyFinalSP.Column &&
268+
sp.Offset < this.BodyFinalSP.Offset) {
269+
this.SequencePoints.Remove(this.BodyFinalSP); // remove duplicate
270+
}
271+
this.BodyFinalSP = sp;
264272
}
265-
this.BodyFinalSP = sp;
266273
}
267274
}
275+
else {
276+
this.BodyFinalSP = this.SequencePoints.Last();
277+
}
268278
}
269279
}
270280

@@ -279,6 +289,9 @@ int GetSequencePointsCount() {
279289
return 0;
280290
}
281291

292+
const string @assert = "Assert";
293+
const string @contract = "Contract";
294+
282295
void GetBranchRatio () {
283296

284297
this.BranchCoverageRatio = null;
@@ -301,30 +314,29 @@ void GetBranchRatio () {
301314
// SequencePoint is visited and belongs to this method?
302315
if (sp.VisitCount != 0 && sp.FileID == this.FileID) {
303316

304-
// Don't want branch coverage of ccrewrite(n)
305-
// SequencePoint's with offset before and after method body
306-
if (sp.Offset < BodyStartSP.Offset ||
307-
sp.Offset > BodyFinalSP.Offset) {
308-
sp.BranchCoverage = true;
309-
continue; // skip
310-
}
311-
312317
if (this.FileNameExt == ".cs") {
313-
// 0) Only for C#
318+
// Only for C#
319+
320+
// Don't want branch coverage of ccrewrite(n)
321+
// SequencePoint(s) with offset before and after method body
322+
if (sp.Offset < BodyStartSP.Offset ||
323+
sp.Offset > BodyFinalSP.Offset) {
324+
sp.BranchCoverage = true;
325+
continue; // skip
326+
}
327+
314328
// 1) Generated "in" code for IEnumerables contains hidden "try/catch/finally" branches that
315329
// one do not want or cannot cover by test-case because is handled earlier at same method.
316330
// ie: NullReferenceException in foreach loop is pre-handled at method entry, ie. by Contract.Require(items!=null)
317331
// 2) Branches within sequence points "{" and "}" are not source branches but compiler generated branches
318332
// ie: static methods start sequence point "{" contains compiler generated branches
319333
// 3) Exclude Contract class (EnsuresOnThrow/Assert/Assume is inside method body)
320334
// 4) Exclude NUnit Assert(.Throws) class
321-
const string assert = "Assert";
322-
const string contract = "Contract";
323335
if (sp.Content == "in" || sp.Content == "{" || sp.Content == "}" ||
324-
sp.Content.StartsWith(assert + ".", StringComparison.Ordinal) ||
325-
sp.Content.StartsWith(assert + " ", StringComparison.Ordinal) ||
326-
sp.Content.StartsWith(contract + ".", StringComparison.Ordinal) ||
327-
sp.Content.StartsWith(contract + " ", StringComparison.Ordinal)
336+
sp.Content.StartsWith(@assert + ".", StringComparison.Ordinal) ||
337+
sp.Content.StartsWith(@assert + " ", StringComparison.Ordinal) ||
338+
sp.Content.StartsWith(@contract + ".", StringComparison.Ordinal) ||
339+
sp.Content.StartsWith(@contract + " ", StringComparison.Ordinal)
328340
) {
329341
sp.BranchCoverage = true;
330342
continue; // skip

0 commit comments

Comments
 (0)