Skip to content
Advertisement

How to use compile_commands.json with clang python bindings?

I have the following script that attempts to print out all the AST nodes in a given C++ file. This works fine when using it on a simple file with trivial includes (header file in the same directory, etc).

JavaScript

However, I get a clang.cindex.TranslationUnitLoadError: Error parsing translation unit. when I run the script and provide a valid C++ file that has a compile_commands.json file in its parent directory. This code runs and builds fine using CMake with clang, but I can’t seem to figure out how to pass the argument for pointing to the compile_commands.json correctly.

I also had difficulty finding this option in the clang documentation and could not get -ast-dump to work. However, clang-check works fine by just passing the file path!

Advertisement

Answer

Your own accepted answer is incorrect. libclang does support compilation databases and so does cindex.py, the libclang python binding.

The main source of confusion might be that the compilation flags that libclang knows/uses are only a subset of all arguments that can be passed to the clang frontend. The compilation database is supported but does not work automatically: it must be loaded and queried manually. Something like this should work:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement