• Stars
    star
    127
  • Rank 282,790 (Top 6 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool based on clang which generates a call graph from a given C++ codebase

clang-callgraph

A Python 3 script based on clang which generates a call graph from a given C++ codebase.

Usage

This is the general script usage:
./clang-callgraph.py file.cpp|compile_commands.json [options] [extra clang args...]

Understood options are:

  • -x name1,name2: a comma separated list of excluded prefixes, like std::,boost::. All symbols starting with one of those will be hidden in the callgraph.
  • -p path1,path2: a comma separated list of excluded prefixes, like /usr. All symbols defined or used in files whose name starts with one of those will be hidden in the callgraph.
  • --cfg config_file: Retrieve additional configuration from a config file.
  • --lookup function_name: Directly lookup the given function and print its callgraph instead of asking the user to type a function name.

For more details look at the example provided.

Configuration File

A configuration file can be used for options that don't depend on the source project to be analysed. This way it makes command lines shorter. The format used is YAML and may contain the following entries :

  • excluded_prefixes: same as the -x option
  • excluded_paths: same as the -p option
  • clang_args: any additional clang args

Dependencies

This script is based on Python 3, therefore a compatible interpreter is needed.
Clang is also needed, below here an example of the required packaged supposing Clang 14 is available on an Ubuntu installation, adapt the following depending on your distro and packages availability:

$ sudo apt install clang-14 libclang-14-dev
$ pip install clang==14.0.0

Generating compile_commands.json

The compile_commands.json is a compilation database formatted in JSON, and contains details and flags for each compilation unit (.c/.cpp file) of your project.
For more details look here.

CMake based projects

Simply enable the CMAKE_EXPORT_COMPILE_COMMANDS setting when configuring your project and CMake will automatically generate a compile_commands.json file inside your chosen build folder.

Make based projects

The easiest way to generate the file compile_commands.json for any make based compilation chain is to use Bear and recompile with bear make.

Example

When running the python script, after parsing all the codebase, you are prompted to type in the function's name for which you want to obtain the callgraph:

$ clang-callgraph.py compile_commands.json -p /usr/lib/llvm-14/lib/clang/14.0.0/include/
reading source files...
/home/vermeille/CPAsim/src/module.cpp
/home/vermeille/CPAsim/src/module/modulevalues.cpp
/home/vermeille/CPAsim/src/main.cpp
/home/vermeille/CPAsim/src/parser.cpp
> main
matching:
main(int, char **)
> main(int, char **)
main(int, char **)
  Parser::ParseModuleDef(std::istream &)
    Parser::EatWord(std::istream &, const std::string &)
      Parser::FuckSpaces(std::istream &)
      Parser::EatChar(std::istream &, char)
    Parser::ParseWord(std::istream &)
      Parser::FuckSpaces(std::istream &)
    Module::Module(const std::string &)
    Parser::FuckSpaces(std::istream &)
    Parser::EatChar(std::istream &, char)
    Parser::FuckSpaces(std::istream &)
    Parser::EatChar(std::istream &, char)
    Module::AddInput(std::unique_ptr<WireDecl>)
      WireDecl::name()
      WireDecl::name()
      WireDecl::name()
    Parser::ParseWireDecl(std::istream &)
      Parser::ParseWord(std::istream &)
      Parser::FuckSpaces(std::istream &)
      Parser::EatChar(std::istream &, char)
      Parser::FuckSpaces(std::istream &)
      Parser::ParseDecimalInt(std::istream &)
        Parser::FuckSpaces(std::istream &)
      Parser::FuckSpaces(std::istream &)
      Parser::EatChar(std::istream &, char)
      Parser::FuckSpaces(std::istream &)
      WireDecl::WireDecl(const std::string &, int)
$ clang-callgraph.py compile_commands.json -x Parser:: -p /usr/lib/llvm-14/lib/clang/14.0.0/include/
reading source files...
/home/vermeille/CPAsim/src/module.cpp
/home/vermeille/CPAsim/src/module/modulevalues.cpp
/home/vermeille/CPAsim/src/main.cpp
/home/vermeille/CPAsim/src/parser.cpp
> main(int, char **)
main(int, char **)
  Module::BindUsagesToDef()
    Module::BindUsagesToDef_Rec(Expr *)
      Module::BindUsagesToDef_Rec(Expr *)
        Module::BindUsagesToDef_Rec(Expr *)
        Binop::lhs()
        Module::BindUsagesToDef_Rec(Expr *)
        Binop::rhs()
        Module::BindUsagesToDef_Rec(Expr *)
        Not::rhs()
        WireUsage::name()
        WireUsage::name()
        WireUsage::SetDeclRef(WireDecl *)
        WireUsage::IsUseValid()
          WireDecl::size()
          WireDecl::size()
          WireDecl::size()
        WireUsage::name()
        WireUsage::index()
        WireUsage::index()
        WireDecl::size()

Configuration file example : callgraph.yml:

excluded_prefixes:
  - 'std::'
excluded_paths:
  - /usr/include
  - /usr/lib/llvm-14/lib/clang/14.0.0/include/
clang_args:
  - '-I/usr/lib/gcc/x86_64-redhat-linux/12/include'

More Repositories

1

Torchelie

Torchélie is a set of utility functions, layers, losses, models, trainers and other things for PyTorch.
Python
111
star
2

motion-capture

Animate a SVG avatar through facial Motion Capture
Python
11
star
3

http-interface

An embeddable http server for C++ applications. Outputs charts for monitoring purposes, running jobs, offer an interface to call functions, etc
C++
6
star
4

neural_style

A reimplementation of neural style
Python
6
star
5

vim-eqshow

A vim plugin to nicely display maths
Vim Script
5
star
6

VQVAE2

Yet another implementation of the VQVAE2
Python
3
star
7

BoW

A bag of words implementation
C++
3
star
8

Synth

A scriptable synthesizer
C++
3
star
9

puyo-platform

A Puyo Puyo HTTP API to propose Puyo Puyo as a coding contest
C++
3
star
10

PatchMatch

A python implementation of the PatchMatch nearest neighbor matching algorithm
Python
2
star
11

DeepCluster

An implementation of DeepCluster based on Pytorch and sklearn.
Python
1
star
12

genesis

a genesis emulator
Rust
1
star
13

rust-HOG

An Histogram of Oriented Gradient in Rust. Linear SVM to come. Mostly for educational purposes, not production ready.
Rust
1
star
14

batriz

C++
1
star
15

dqn

C++
1
star
16

french-pos-tagger

a french POS tagger
C++
1
star
17

Gamulator

A gameboy emulator I'm working on
C++
1
star
18

parcxx

A parser combinators library shamelessly inspired by what's done in Haskell
C++
1
star
19

Thesis

TeX
1
star
20

depth-recovery

Jupyter Notebook
1
star
21

slip

This is a header only library that allows you to add a Lisp-ish script engine to your C++ programs or S-expression serialization.
C++
1
star
22

JMTFS

A very simple RAMfs
C
1
star