# Way to plot graph of codebase? (i.e. function circles and dependency arrows)

**URL:** <https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 1, 2017, 2:41am UTC](https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450 "2017-05-01T02:41:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 1, 2017, 2:41am UTC](https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450/1 "2017-05-01T02:41:07Z")

</div>

I’m getting to the point where I have a decent amount of functions in my codebase.

I was wondering if there was a way to automatically construct an image w/ a graph showing:

- the functions, macros, etc
- their interdependencies

* * *

**edit:** i.e., something like this [stack overflow post](http://stackoverflow.com/questions/517589/tools-to-get-a-pictorial-function-call-graph-of-code)

 ![](https://global.discourse-cdn.com/julialang/original/3X/5/3/538b8f760276c101f14154b27ab2e8c10689be37.png)

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 1, 2017, 8:24pm UTC](https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450/2 "2017-05-01T20:24:14Z")

</div>

I guess as a first step,

- How can you tell what functions a function uses?

I.e, foo calls fizz and buzz

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [May 2, 2017, 12:23am UTC](https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450/3 "2017-05-02T00:23:15Z")

</div>

There’s [https://github.com/dpsanders/TreeView.jl](https://github.com/dpsanders/TreeView.jl).

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 6, 2017, 6:54pm UTC](https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450/4 "2017-05-06T18:54:45Z")

</div>

I agree that TreeView is awesome, but I feel like that’s just the front-end part.

What needs to be done to get the codebase tree to send to TreeView?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [May 7, 2017, 2:45am UTC](https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450/5 "2017-05-07T02:45:38Z")

</div>

Yeah, TreeView isn’t quite what you want, but it’s the closest thing I could think of at the time.

Maybe just using the functionality in `Base.Profile` to build the call graph by sampling and visualizing it using [ProfileView](https://github.com/timholy/ProfileView.jl) is closer to what you need.

But I was thinking a bit more about how to construct the call graph without sampling. I guess one thing you would need in order to get an actual call graph for a given function call is the ability to ‘dig into’ functions. So given e.g.

```julia
function f(x, y)
    a = x * x
    a + g(y * y)
end

```

somehow get the expression

```julia
quote 
    a = x * x
    return a + (Main.g)(y * y)
end

```

In general, the expression to return will of course depend on the types of the input arguments, because function `f` might have several methods. Here is my attempt at accomplishing this:

```julia
function replace_slot_numbers_with_names!(expr::Expr, l::LambdaInfo)
    for (i, arg) in enumerate(expr.args)
        if isa(arg, SlotNumber)
            expr.args[i] = l.slotnames[arg.id]
        elseif isa(arg, Expr)
            replace_slot_numbers_with_names!(arg, l)
        end
    end
    expr
end

function function_body_expr(f, types)
    l = first(code_lowered(f, types))
    ast = Base.uncompressed_ast(l)
    body = Expr(:block)
    body.args = ast[2 : end]
    body.typ = l.rettype
    expr = Base.remove_linenums!(replace_slot_numbers_with_names!(body, l))
end

```

Usage example:

```julia
function_body_expr(f, (Int, Int))

```

which returns exactly the desired expression above. Note: unfortunately, TreeView / its dependency TikzGraphs can’t quite handle this expression due to some LaTeX issues. I worked around these issues by changing [this line](https://github.com/dpsanders/TreeView.jl/blob/1f40e0f45956372f9f602378306c4168da8a311b/src/display.jl#L11) to

```julia
return latex_escape(string(sym))

```

after which

```julia
using TreeView
TreeView.walk_tree(function_body_expr(f, (Int, Int)))

```

produces  
 ![](https://global.discourse-cdn.com/julialang/original/3X/3/6/36e2b2377c7f2fe433eb804a6ccea033594bbaa8.png)

Now this is just a piece of the puzzle:

- you’d need to do this recursively, e.g. ‘digging into’ `g` in the same way, while keeping track of the types of the arguments with which each function is called (i.e., which method is called). Maybe `code_typed` is more useful than `code_lowered`. Presumably, you’d do this up to some desired depth, or maybe recursing only into functions from a certain module. You’d also need to handle cycles.
- the graph you get out of TreeView isn’t exactly what you want; you only care about methods (things like `a`, `x`, and `=` should not appear), and the depth of a node in the tree should be the recursion depth (so all of the methods in the example I gave should actually be at the same depth in the tree, and the methods that `g` calls should be nested under `g`).

So there’s still quite a lot of work to be done, but maybe this will get you (or somebody else) started. I think it would be very nice to be able to visualize call graphs without sampling.

---

<div class="post-metadata">

**Author:** ![f.L0x](https://avatars.discourse-cdn.com/v4/letter/f/bcef8e/32.png) [@f.L0x](https://discourse.julialang.org/u/f.L0x)\
**Post date:** [November 23, 2021, 10:13pm UTC](https://discourse.julialang.org/t/way-to-plot-graph-of-codebase-i-e-function-circles-and-dependency-arrows/3450/6 "2021-11-23T22:13:26Z")

</div>

Maybe this new git hub tool is closer what you were looking for?

> **[GitHub Next | Visualizing a Codebase](https://githubnext.com/projects/repo-visualization)**
>
> GitHub Next Project: How can we “fingerprint” a codebase to see its structure at a glance? Let’s explore ways to automatically visualize a GitHub repo, and how that could be useful.

Unfortunely not done with makie 😉
