22

I want to find all recursive calls in my code.

If I open file in Visual Studio, I get "Recursive call" icon on left side of Editor. enter image description here

I want to inspect whole solution for such calls.

I used Resharper Command Line tools and VS's add-in Resharper - Code Inspection with no luck, this rule is not applied in their ruleset.

Is there any way that i could inspect whole solution - i really don't want to open each file and check for that blue-ish "Recursive call" icon :)

Edit: I am interested in single-level recursion

2
  • 2
    You're aware that this icon don't appear for multi-level recursion? ie. method A calls B which in turn calls A, etc.? Aug 6, 2013 at 12:24
  • 1
    Yes i am aware, i just want single-level recursion. Aug 6, 2013 at 12:25

1 Answer 1

27

You could do it with Mono.Cecil.

Here is a simple LINQPad program that demonstrates:

const string AssemblyFilePath = @"path\to\assembly.dll";

void Main()
{
    var assembly = ModuleDefinition.ReadModule(AssemblyFilePath);
    var calls =
        (from type in assembly.Types
         from caller in type.Methods
         where caller != null && caller.Body != null
         from instruction in caller.Body.Instructions
         where instruction.OpCode == OpCodes.Call
         let callee = instruction.Operand as MethodReference
         select new { type, caller, callee }).Distinct();

    var directRecursiveCalls =
        from call in calls
        where call.callee == call.caller
        select call.caller;

    foreach (var method in directRecursiveCalls)
        Debug.WriteLine(method.DeclaringType.Namespace + "." + method.DeclaringType.Name + "." + method.Name + " calls itself");
}

This will output the methods that calls themselves directly. Note that I only processed the Call instruction, I'm not certain how to handle the other call instructions correctly here, or even if that is even possible.

Caveat: Note that this will, because I only handled that single instruction, only work with statically compiled calls.

Virtual calls, calls through interfaces, that just happen to go back to the same method, will not be detected by this, a lot more advanced code is necessary for that.

2
  • 2
    Just to clarify, in case anyone isn't sure; you don't need Mono to do this. In LinqPad Pro you can add the NuGet package 'Install-Package Mono.Cecil' in the Query Properties window. If you are a cheap skate and have the free version then you'll need to download the nuget package into another temporary project then add additional assemblies to your query using Query Properties. Otherwise ModuleDefinition.ReadModule will not have the correct references to work.
    – Rebecca
    Jul 3, 2014 at 19:44
  • I've downloaded github.com/jbevain/cecil/releases build and run with the code above and list me all recursive call. Thanks!
    – Zyo
    Dec 29, 2016 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.