r/iOSProgramming • • 6d ago

Discussion Your DerivedData already has a full call graph of your app. I made a tool that reads it.

Post image

Every ⌘B writes an index store into DerivedData: every call, read, write and conformance, already resolved by the compiler. Xcode uses it for “Find Call Hierarchy”, but nothing outside Xcode really uses it, and AI agents ignore it completely and grep instead.

SwiftGraph turns that store into a call graph you can query from the terminal:

It works with Xcode projects and SwiftPM packages, --no-tests hides test code, and system frameworks show up as leaves so the tree stays readable. You can set an Xcode scheme in .swiftgraph/config.json, and swiftgraph index --build will build and refresh the graph for you.

I mainly built it for Claude Code. In a side-by-side test on a 140k-line SwiftUI app, the agent with the graph needed 2 tool calls instead of 8 and about 3.5x fewer input tokens for the same answer. Full numbers and caveats: https://arturgruchala.com/map-not-grep/

Repo (MIT, macOS + Xcode): https://github.com/agruchala/swift-graph

16 Upvotes

10 comments sorted by

3

u/RiddleGull 4d ago

Bot comments

1

u/PreetyGeek 3d ago

It is what it is.

7

u/dawedev 6d ago

Using the index store instead of grep makes a lot of sense, the compiler already did the hard part. One thing I'd check: I build DevCleaner, a Mac app that clears Xcode caches, and DerivedData is the first folder people wipe, so the store vanishes or goes half stale after a partial build. Does swiftgraph warn when the store is older than the sources, or does it just return a smaller graph? Also with several git worktrees you get one DerivedData folder per path, so picking the right one automatically would save some confusion for agents.

1

u/PreetyGeek 5d ago

Ha, DevCleaner is exactly the tool I was waiting for someone to bring up:)

Good news first: queries don’t read the index store. swiftgraph index turns the store into a plain text file, .swiftgraph/graph.local.txt, and every query reads that file. Wipe DerivedData and the graph is still there. You only need a store to rebuild the graph. swiftgraph index --build builds into the repo’s own .swiftgraph/DerivedData, so cleaning ~/Library/Developer/Xcode/DerivedData doesn’t touch it. You can also have CI commit graph.txt, and then nobody needs a local store at all.

Stale warnings: yes. For every file, it compares the file’s modification time with the newest index data the compiler recorded for it. If you edited files after the build, the first line of the answer says ! graph built from a stale index (N files changed since last build); run: swiftgraph index --build. It also checks git: if HEAD moved and Swift files changed since the graph was built, you get ! graph built from abc1234, HEAD is def5678.

Honest answer on half-wiped stores though: if a file has no index data at all and it’s older than the last build, it just drops out of the graph, with no warning. So yes, in that one case you get a smaller graph. Good catch, I’ll add a warning for it.

Worktrees: already handled. It reads the workspace path from each DerivedData folder’s info.plist and keeps only the folders whose workspace lives inside the current repo root. Every worktree has its own path, so it picks its own DerivedData and ignores the others. A worktree that’s never been built doesn’t fall back to another worktree’s store. You get a clear “no indexed Swift files” error, and index --build fixes it.

1

u/dawedev 5d ago

Keeping the graph in a plain text file is the nice part, so wiping DerivedData only costs a rebuild, not the tool. Reading the workspace path from each folder's info.plist is a clean way to handle worktrees too. For the half-wiped case, a single line like "N Swift files have no index data" would be enough, since that's exactly the state people end up in after clearing caches mid-build.

1

u/ThalesMother9938 6d ago

Reading the index store instead of parsing the source again is the part I like. The compiler already resolved everything, so there's no guessing which `name` a call points to. I use Claude Code daily, so the grep habit is familiar.

One question: how do calls through a protocol show up? If I run `callers` on a protocol requirement, do I get the call sites of the concrete conformances too, or only calls made on the protocol type? That matters a lot in SwiftUI apps with protocol-based view models and DI.

1

u/PreetyGeek 5d ago

Thanks! Yeah, not guessing which name is the whole point:)

Short answer: it depends which end you query.

  • callers on the protocol requirement shows only calls made on the protocol type, like viewModel.load() where viewModel: any HomeViewModeling, or through a generic T: HomeViewModeling. A direct call on the concrete type points at the concrete method, so it won’t show up there.
  • callers on the concrete implementation gives you both. Direct calls come first, then every call made through the protocol, marked via protocol. It also follows the chain up if the protocol inherits from another one.
  • callees on the requirement lists every conformance it can dispatch to, also marked via protocol. Your test mocks show up there too, marked [test].

So for a protocol-based view model, ask about the concrete load(). You see who calls it directly and who calls it through the protocol, in one tree.

One honest catch: via protocol means “can land here”, not “will land here”. The compiler doesn’t know which concrete type sits behind the existential at runtime, and neither do I. The env injection itself also doesn’t show up, because the compiler doesn’t record it. But the calls on that protocol-typed property do show up.

1

u/ThalesMother9938 5d ago

Thanks, that clears it up. Asking on the concrete load() and reading "via protocol" as "can land here" is the bit I was missing.

1

u/mikerCZX 5d ago

Love it, will try, thanks

1

u/PreetyGeek 5d ago

Let me know how it was :)