Glud
Overview
Glud is an abstraction on the libclang library that make matching fragments of the Clang AST simple and Pythonic, in the same way that libclangastmatchers does for the C++ Clang API.
Examples
On the code snippet
// input.cpp
namespace X {
class Y {};
}
class Y {};
You could match a class named X in the namespace Y with the matcher:
from glud import *
m = cxxRecordDecl(
hasName('Y'),
hasAncestor(namespaceDecl(hasName('X'))))
tu = parse('input.cpp')
matches = walk(m, tu.cursor)
Prerequisites
Install a recent version of Clang and the python libclang bindings. On Ubuntu Trusty, you can install pre-built binaries from the LLVM apt repositories:
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 python-clang-3.8 libclang1-3.8
To run glud, you'll need to make sure that libclang.so is on your loader path.
export LD_LIBRARY_PATH=/usr/lib/llvm-3.8/lib
Installing
You can install the latest stable version from PyPI
$ pip install glud
Acknowledgements
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.
Contributing
If you experience problems with glud, log them on GitHub. If you want to contribute code, please fork the code and submit a pull request.