Skip to content

Commit

Permalink
Fix FunctionComplexity to not throw an exception on Lucene.Net
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeri Hristov authored and valhristov committed Aug 31, 2017
1 parent f197635 commit f389d0b
Show file tree
Hide file tree
Showing 6 changed files with 1,535 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand All @@ -35,6 +36,21 @@ public class CyclomaticComplexityWalker : CSharpSyntaxWalker

public int CyclomaticComplexity => incrementLocations.Count;

public void Walk(SyntaxNode node)
{
try
{
Visit(node);
}
catch (InsufficientExecutionStackException)
{
// TODO: trace this exception

// Roslyn walker overflows the stack when the depth of the call is around 2050.
// See ticket #727.
}
}

public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
{
if (node.ExpressionBody != null || HasBody(node))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected override IEnumerable<SyntaxNode> PublicApiNodes
public override int GetComplexity(SyntaxNode node)
{
var walker = new CyclomaticComplexityWalker();
walker.Visit(node);
walker.Walk(node);
return walker.CyclomaticComplexity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected void CheckComplexity<TSyntax>(SyntaxNodeAnalysisContext context, Func<
return;
}

walker.Visit(nodeToCheck);
walker.Walk(nodeToCheck);

if (walker.CyclomaticComplexity > Maximum)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void CognitiveComplexity()
[TestCategory("Rule")]
public void CognitiveComplexity_StackOverflow()
{
Verifier.VerifyAnalyzer(@"TestCases\CognitiveComplexity_InsufficientExecutionStackException.cs",
Verifier.VerifyAnalyzer(@"TestCases\SyntaxWalker_InsufficientExecutionStackException.cs",
new CognitiveComplexity { Threshold = 0, PropertyThreshold = 0 });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ public class FunctionComplexityTest
[TestCategory("Rule")]
public void FunctionComplexity_CSharp()
{
var diagnostic = new SonarAnalyzer.Rules.CSharp.FunctionComplexity { Maximum = 3};
var diagnostic = new SonarAnalyzer.Rules.CSharp.FunctionComplexity { Maximum = 3 };
Verifier.VerifyAnalyzer(@"TestCases\FunctionComplexity.cs", diagnostic);
}

[TestMethod]
[TestCategory("Rule")]
public void FunctionComplexity_InsufficientExecutionStack_CSharp()
{
var diagnostic = new SonarAnalyzer.Rules.CSharp.FunctionComplexity { Maximum = 3 };
Verifier.VerifyAnalyzer(@"TestCases\SyntaxWalker_InsufficientExecutionStackException.cs", diagnostic);
}

[TestMethod]
[TestCategory("Rule")]
public void FunctionComplexity_VisualBasic()
Expand Down
Loading

0 comments on commit f389d0b

Please sign in to comment.