Skip to content

basseches/libclang-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

libclang-tools

Use the libclang module to query the Clang AST for a .c file.

Dependencies

libclang is part of the LLVM project. Unfortunately, there's no good way to install Clang's Python bindings as there's no officiallibclang package. The solution for now is to

pip install libclang

This package is an unofficial mirror.

replace.py

Finds all calls to a certain function and replaces them with calls to another function. Optionally replaces the arguments with arguments passed on the command line.

usage: replace.py [-h] [-a [ARGS ...]] [-i] [-o OUTPUTFILE] <filepath> <oldfn> <newfn>

Find and replace function calls. Writes result to stdout.

positional arguments:
  <filepath>            the path of the .c file
  <oldfn>               the name of the function to replace
  <newfn>               the new function's name

options:
  -h, --help            show this help message and exit
  -a [ARGS ...], --args [ARGS ...]
                        new args to be passed to the function (refer to old function's args
                        with [number])
  -i, --inplace         in place edit, if specified
  -o OUTPUTFILE, --outputfile OUTPUTFILE
                        the name of an output file, if specified

Example usage

Executing the following will replace all calls to foo() in file.c with calls to bar(), but the arguments to the function call will be retained. The result is written to stdout.

./replace.py file.c foo bar

Executing the following will replace all calls to foo(...) in file.c with calls to bar(1, 2, 3) in-place.

./replace.py -i file.c foo bar -a 1 2 3

Executing the following will replace all calls to foo(arg0, arg1) with calls to bar((arg0) * (arg1)) in-place.

./replace.py -i file.c foo bar -a "([0]) * ([1])"

For nice syntax highlighting, pipe into batcat:

./find.py example.c foo | batcat -l=c

printdef.py

Like printf(), but not really like printf() at all.

Finds a function's definition and prints it out.

usage: printdef.py [-h] <filepath> <function>

Find and print the definition of a function.

positional arguments:
  <filepath>  the path of the .c file
  <function>  the name of the function to extract

options:
  -h, --help  show this help message and exit

Example usage

The following will print out the definition of foo() if it exists in file.c.

./printdef.py file.c foo

orthodontist.py

Remove braced zero-initialization of arrays.

usage: orthodontist.py [-h] [-i] [-o OUTPUTFILE] <filepath>

Remove braced zero initialization of arrays. Writes result to stdout by default.

positional arguments:
  <filepath>            the path of the .c file

options:
  -h, --help            show this help message and exit
  -i, --inplace         in place edit, if specified
  -o OUTPUTFILE, --outputfile OUTPUTFILE
                        the name of an output file, if specified

About

A collection of tools which use libclang to query the Clang AST.

Resources

Stars

Watchers

Forks

Contributors

Languages