klitsche / clang-ffi
libclang binding via ffi
Requires
- php: ^7.4 || ^8.0
- ext-ffi: *
- symfony/polyfill-php80: ^1.20
- symfony/process: ^4.4 || ^5.0
Requires (Dev)
- composer/composer: ^2.0
- phpunit/phpunit: ^9.5
- symplify/easy-coding-standard: ^9.0
README
A library for PHP exposing libclang via FFI.
It allows easy traversing and analyzing of an abstract syntax tree of a c / c++ file.
Unfortunately PHP FFI does not currently support Struct By Value as parameter or return value in function calls. That is why a c based wrapper is provided for libclang itself, which can then be bound via FFI. This wrapper must be compiled before use.
The wrapper is taken from dart ffigen and has been slightly extended.
Expect breaking changes along all 0.* pre-releases.
Requirements
- PHP >= 7.4
- FFI extension enabled
- LLVM >= 9 with clang and libclang-dev
Quick Start
Install llvm with clang ...
... on macOS (see https://formulae.brew.sh/formula/llvm)
brew install llvm@11
... on Debian / Ubuntu (see https://apt.llvm.org/)
apt-get update; \
apt-get install wget lsb-release software-properties-common ca-certificates gnupg; \
wget -q https://apt.llvm.org/llvm.sh; \
chmod +x llvm.sh; \
sudo ./llvm.sh 11; \
apt-get install libclang-11-dev;
Install in your project
composer require klitsche/clang-ffi
Add to composer.json (this will trigger compilation of wrapped libclang library)
"scripts": { "post-autoload-dump": "Klitsche\\Clang\\FFI\\libclang\\Compiler::run", },
Dump autoload to trigger compilation
composer dump-autoload
Run examples
Checkout & prepare
git clone https://github.com/klitsche/clang-ffi.git; \
cd clang-ffi; \
docker-compose build;
Print parsed file
docker-compose run --rm php74 php examples/print.php
Collect and dump simplified AST nodes as json
docker-compose run --rm php74 php examples/json.php
Dump found issues during parsing
docker-compose run --rm php74 php examples/issues.php
Todos
- Add ffigen
- Add multiversion support for llvm >9 (some const values are different)
- Add documentation
- Add support for Windows
License
See LICENSE.md for details.
LICENSE.wrapper.c.md covers file src/FFI/libclang/wrapper.c.