static void D() { }
static void Y() { D(); }
static void X() { Y(); }
static void C() { D(); X(); }
static void B() { C(); }
static void S() { D(); }
static void P() { S(); }
static void O() { P(); }
static void N() { O(); }
static void M() { N(); }
static void G() { M(); }
static void A() { B(); G(); }
int main() {
A();
}
Then
$ clang++ -S -emit-llvm main1.cpp -o - | opt -analyze -dot-callgraph
$ dot -Tpng -ocallgraph.png callgraph.dot
Yields some shiny picture (there is an "external node", because main
has external linkage and might be called from outside that translation unit too):
You may want to postprocess this with c++filt
, so that you can get the unmangled names of the functions and classes involved. Like in the following
#include <vector>
struct A {
A(int);
void f(); // not defined, prevents inlining it!
};
int main() {
std::vector<A> v;
v.push_back(42);
v[0].f();
}
$ clang++ -S -emit-llvm main1.cpp -o - |
opt -analyze -std-link-opts -dot-callgraph
$ cat callgraph.dot |
c++filt |
sed 's,>,\>,g; s,-\>,->,g; s,<,\<,g' |
gawk '/external node/{id=$1} $1 != id' |
dot -Tpng -ocallgraph.png
Yields this beauty (oh my, the size without optimizations turned on was too big!)
That mystical unnamed function, Node0x884c4e0
, is a placeholder assumed to be called by any function whose definition is not known.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…