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...)

Community
  • 1
  • 1
zekel
  • 9,227
  • 10
  • 65
  • 96

2 Answers2

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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 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? – zekel Jan 31 '12 at 19:03
  • 1
    dot is part of graphviz. From Macports: 'sudo port install graphviz' – Rob Napier Jan 31 '12 at 19:06
  • Will I need to translate the method names, as [described here](http://stackoverflow.com/a/5373814/168594)? – zekel Jan 31 '12 at 19:28
  • 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. – Rob Napier Jan 31 '12 at 19:47
  • 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?) – zekel Jan 31 '12 at 20:11
  • My real goal here is to be able to run this on my files so I can (objectively) see which ones are a mess so I can be allocated time to fix them. I'm hoping to show something that's easy to understand so my boss doesn't have to just take my word for it, and to show that a "messy file" is not equated with the "messiest file in the project." – zekel Jan 31 '12 at 20:14
  • You can try opening the .dot file in OmniGraffle, which lets you perform re-layout dynamically. You can also render using `fdp` or `sfdp` rather than `dot`, which will probably give you something a bit more readable. `man dot` for much more info. – Rob Napier Jan 31 '12 at 20:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7210/discussion-between-rob-napier-and-zekel) – Rob Napier Jan 31 '12 at 20:42
  • WRT "...cut and paste out the giant build line", I'm coming across this -- exactly my question! -- in 2019, and it appears that Xcode now generates a lot of little build lines, rather than one giant one. So **my question is:** can I get a hint about what the build-command I'm looking for starts with, so I can delete the last -o parameter and add the suggested ones? Thanks! – Olie Dec 16 '19 at 19:28
  • Also curious what the modern `opt-mp` is; I don't appear to have one of those and brew doesn't appear to know about it. Thanks~ – Olie Dec 16 '19 at 20:05
  • Did you find answer to your query? - @Olie – Bibek Sep 24 '21 at 10:24
0

I hit error with xcodebuild command line. anything missing?

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