I am looking for a recursion analysis tool [closed]
Asked Answered
D

2

11

I ran into a problem this weekend where method 1 called method 2 which called method 3 which caught exception, and within the exception handling method 1 was called again...

M1 -> M2 -> M3 -> M1 ....

The problem became obvious and easy to fix, once the problem happened.

Does anyone know of a tool to detect problems like this in a .NET application?

Dyane answered 21/3, 2011 at 18:0 Comment(3)
Compile time (a.ka. static) analysis for detecting unbound recursion is pretty hard to do. I don't think there are any tools in the .NET space that can help you out with that.Gottwald
@Gottwald - yes, I was not able to find anything regarding a tool... the closest I came was an ACM article about it... I crossed my fingers when I posted the question.Dyane
You want to detect arbitrary recursion? Or specifically, recursion that occurs via an exception handler?Stoppage
S
6

To do this right, you need a global call graph over the C# application, computed using C# semantics and what amounts to a points-to analysis, arguably including the libraries it calls. With such a call graph, you could enumerate the cycles in it, and those would be the candidates to check.

I don't know where you would get a tool that compute such a global call graph for C#, off the shelf.

You could approximate this using simple code scanning techniques. For each method M, extract the apparant set of calls it contains as identifiers I. Mostly they will appear as syntax that looks like identifier( After this step you have M_i -> I. You can build this as an (extermely conservative) basic call graph, and then compute the transitive closure. With that, you have an approximate call graph with cycles, and you can carry out your cycle analysis. This would mass methods passed by names, and other cases, but it might be good enough.

Stoppage answered 25/9, 2011 at 20:30 Comment(1)
Not quite the answer I was looking for (a tool) but a good resume of the complexity of the task (which explains, a bit, the lack of such tools). ThanksOsteomalacia
O
2

Gendarme will catch some (the most common, but basic) cases of recursions. MS FxCop has some too (IIRC). However neither have rules (presently) to cover more complex cases like: M1->M2->M3->M1...

Sadly I'm not aware of any other tool that can do such detection for .NET. Please share anything you find :-)

Osteomalacia answered 7/8, 2011 at 19:6 Comment(1)
NDepend. See #1683591Boucicault

© 2022 - 2024 — McMap. All rights reserved.