Can clang generate a call graph for an Xcode project (in Objective-C?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 22:38:53

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!