14

I found this example that looks like it outputs what I want for C++. How can it be done for the Objective-C code in an Xcode project?

I see mentions of Doxygen being able to create a call graph, but I can't find an example.

(I want to get to know clang better, but it's hard to get started...)

2 Answers 2

12

Absolutely. There are a couple of tricks that you need to understand, but it's not too bad.

First, you need a compatible version of opt, since it doesn't come with the LLVM Apple ships. I got mine from macports:

port install llvm-3.0

Then you need to compile your file. Working out the parameters can sometimes be a bit of a pain. The easiest way is to let Xcode build it, then go to the logs and cut and paste out the giant build line. I used to be able to hand-hack these, but I've gotten too lazy....

Take out the last -o parameter (conveniently at the end of the compile line), and substitute:

-S -emit-llvm -o - | opt-mp-3.0 -analyze -dot-callgraph

Then, as in the other example:

$ dot -Tpng -ocallgraph.png callgraph.dot

Keep in mind that there are a few functions that get called a lot in ObjC that you almost never care about. In particular, almost anything that starts with objc_. Luckily the DOT format is a very simple text file, and it's pretty easy to write post-processing scripts to strip out what you don't want.

There's also a -print-callgraph parameter that will out put this information in a slightly different format if you want to do further processing.

Sign up to request clarification or add additional context in comments.

11 Comments

I've got it creating the dot graph file. What is the dot command? Part of clang or do I have to install it separately?
dot is part of graphviz. From Macports: 'sudo port install graphviz'
Will I need to translate the method names, as described here?
No. Objective-C doesn't mangle its names. More importantly, clang is ObjC-aware (no shock there given who wrote much of it), so it presents ObjC method signatures, not the underlying implementation functions. Keep in mind that ObjC is dynamic, so there are many ways to call things that wouldn't show up in any kind of static analysis, but you'll get most of what you're probably expecting.
It scaled my image down because it was too big for cairo. (I'm running this on a "problem" file, so that's not a huge surprise.) Are there any practical ways to trim it down into something smaller? For example, I didn't realize that the entire graph was horizontal, I was hoping for something a little easier to read. Or do I just have to trim down the .dot file (or filter the ObjC file before running clang?)
|
0

I hit error with xcodebuild command line. anything missing?

xcodebuild: error: invalid option '-emit-llvm'

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.