Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More regex matches
  • Loading branch information
ddur committed Dec 15, 2015
commit 757e9ea1f9a8d4458062d9a0bf6143f2a9e8fc02
46 changes: 34 additions & 12 deletions main/OpenCover.Framework/Persistance/BasePersistance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,32 @@ private void RemoveUnreferencedFiles()
// Dictionary with stored source files per module
private Dictionary<uint, CodeCoverageStringTextSource> sourceRepository = new Dictionary<uint, CodeCoverageStringTextSource>();

// match if (
private static Regex ifMatch = new Regex (@"\Aif\s*\(", RegexOptions.Compiled);
// match else if (
private static Regex elseifMatch = new Regex (@"\Aelse\s+if\s*\(", RegexOptions.Compiled);
// match switch (
private static Regex switchMatch = new Regex (@"\Aswitch\s*\(", RegexOptions.Compiled);
// match while (
private static Regex whileMatch = new Regex (@"\Awhile\s*\(", RegexOptions.Compiled);
// match for (
private static Regex forMatch = new Regex (@"\Afor\s*\(", RegexOptions.Compiled);
// match foreach (
private static Regex foreachMatch = new Regex (@"\Aforeach\s*\(", RegexOptions.Compiled);
// match try {
private static Regex tryMatch = new Regex (@"\Atry\s*\{", RegexOptions.Compiled);

// match anything like "= boolean expression"
private static Regex assignBoolMatch = new Regex (@"(?<![=!<>])=(?!\s*[=><]).+(\|\s*\||&\s*&|=\s*=|!\s*=|>\s*=|<\s*=|\?\s*\?|\?|:|<|>|\|).*;", RegexOptions.Compiled);
// match anything like (boolean expression)
private static Regex evalBoolMatch = new Regex (@"\(.*(\|\s*\||&\s*&|=\s*=|!\s*=|>\s*=|<\s*=|\?\s*\?|\?|:|<|>|\|).*\)\s*;", RegexOptions.Compiled);

private static Regex cRequiresMatch = new Regex (@"Contract\s*\.\s*Requires", RegexOptions.Compiled);
private static Regex cInvariantMatch = new Regex (@"Contract\s*\.\s*Invariant", RegexOptions.Compiled);
private static Regex cEnsuresMatch = new Regex (@"Contract\s*\.\s*Ensures", RegexOptions.Compiled);
// match Contract.Requires<*> (
private static Regex cRequiresMatch = new Regex (@"Contract\s*\.\s*Requires\s*<.*>\s*\(", RegexOptions.Compiled);
// match Contract.Invariant (
private static Regex cInvariantMatch = new Regex (@"Contract\s*\.\s*Invariant\s*\(", RegexOptions.Compiled);
// match Contract.Ensures[OnThrow<*>] (
private static Regex cEnsuresMatch = new Regex (@"Contract\s*\.\s*(Ensures|EnsuresOnThrow\s*<.*>)\s*\(", RegexOptions.Compiled);

private const bool doRemove = true;
private const bool preserve = false;
Expand All @@ -263,6 +276,8 @@ private bool doRemoveBranches (SequencePoint sp) {
return preserve;
if (sp.FileId == 0)
return preserve;
if (sp.StartLine == sp.EndLine && (sp.EndColumn - sp.StartColumn) <= 2)
return doRemove;

CodeCoverageStringTextSource source = null;
sourceRepository.TryGetValue (sp.FileId, out source);
Expand All @@ -280,24 +295,29 @@ private bool doRemoveBranches (SequencePoint sp) {

default:
#if DEBUG
throw new NotImplementedException ("Source.FileType");
throw new NotImplementedException ("Source.FileType");
#else
return preserve;
#endif
}

string spSource = source.GetText (sp);
if (String.IsNullOrWhiteSpace (spSource))
return preserve;
return doRemove;
if (spSource.Length < 5)
return doRemove; //if(x); x=a|b;
return doRemove; // { } in (if(x) x=a|b)

switch (spSource.Substring (0, 2)) {
case "if": // if (
if (ifMatch.IsMatch (spSource))
return preserve;
break;

case "el": // else if (
if (elseifMatch.IsMatch (spSource))
return preserve;
break;

case "sw": // switch (
if (switchMatch.IsMatch (spSource))
return preserve;
Expand All @@ -321,12 +341,14 @@ private bool doRemoveBranches (SequencePoint sp) {
break;

case "Co": // Contract.*
if (cRequiresMatch.IsMatch (spSource))
return preserve;
if (cInvariantMatch.IsMatch (spSource))
return doRemove;
if (cEnsuresMatch.IsMatch (spSource))
return doRemove;
if (spSource.Length > 8 && spSource.Substring(0, 8) == "Contract") {
if (cRequiresMatch.IsMatch (spSource))
return preserve;
if (cInvariantMatch.IsMatch (spSource))
return doRemove;
if (cEnsuresMatch.IsMatch (spSource))
return doRemove;
}
break;

default:
Expand Down