Python bindings for the unstable interface of the Clang AST Matchers library
Clast is a Python wrapper of the unstable interface of the Clang AST Matchers library.
Existing tools in this space make good use of libclang to build rapid prototypes for exploring the Clang AST, however, several important tools (for example code rewriting) are not available from libclang.
Example of creating a dynamically typed AST node matcher using the dynamic parser and dumping the node.
import sys from clast import * class MyMatchCallback(MatchCallback): def __init__(self, *args, **kwargs): super(MyMatchCallback, self).__init__() def run(self, result): cls = result.GetNode('cls').get(CXXRecordDecl) cls.dump() if __name__ == "__main__": callback = MyMatchCallback() m = parseMatcherExpression('cxxRecordDecl().bind("cls")') finder = MatchFinder() finder.addDynamicMatcher(m, callback) matchString('class X;', finder, '-std=c++11', 'input.cpp')
To install Clast, you’ll need to:
To compile under Ubuntu 14.04 (Trusty), you’ll need to get an updated version of LLVM:
wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add - echo "deb http://llvm.org/apt/trusty/ llvm-toolchain-trusty-3.8 main" | sudo tee -a /etc/apt/sources.list sudo apt-get update -qq sudo apt-get install -y clang-3.8 libclang-common-3.8-dev libclang-3.8-dev llvm-3.8-dev liblldb-3.8-dev python-clang-3.8
Then, set your environment variables:
export LLVM_HOME=/usr/lib/llvm-3.8 export LD_LIBRARY_PATH=$LLVM_HOME/lib
Finally, install pybind11 and Clast. It is strongly recommended that clast be installed with the verbose flag on to show compilation progress
pip install pybind11 pip install -v clast
Given the breadth of the Clang AST matchers API, it isn’t feasible to attempt to maintain hand rolled bindings, instead Clast bootstraps itself using the libclang library, and generates the pybind11 wrappers as required.
In cases where the bindings are stale, or do not compile correctly, you can try to rebuild the them using the included clastgen.py script.
It is hoped that future revisions of Clast will rapidly become self-hosting (one version of Clast will be able to build successive versions).
This project builds on the excellent work of the LLVM team and the University of Illinois at Urbana-Champaign, but is in no way affiliated with either.
Some parts of clasts setup.py were derived from the BSD licensed pybind python_example, Copyright (c) 2016 the Pybind Development Team, All rights reserved.
The need for a tool to make the Clang AST matchers library available from Python was inspired by Christian Schafmeister’s work on clasp