Python clang not looking for system enable

When using libclang from Python, it does not seem to automatically look for ways to enable the system.

Is there any reliable way to get these paths? I don't like hard coding paths as I write code that will work on various UNIX systems.

For example, given test.cpp

#include <stdio.h> int main() { puts("Hello, world!"); } 

and test.py

 from clang.cindex import Index tu = Index.create().parse(None, ["test.cpp"]) print(list(tu.diagnostics)) 

running python test.py will print:

 [<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 1, column 10>, spelling "'stdio.h' file not found">] 

Of course, I can find a system that includes paths by doing

 $ clang -v -E test.cpp 

and adding "-Isome/path" to the parse argument list, i.e.

 args = ["-I/Applications/[...]", "test.cpp"] 

It really works and does not cause errors.

However, this is not portable, and it would be very nice if I could programmatically get clang to use them automatically.

+4
c ++ python clang libclang
source share
1 answer

This question has been around for a long time, so I will try to answer it myself.

It seems that even Klang himself uses mostly hard-coded paths.

It lists candidate paths and adds those that match the current context. This can be seen in clang / lib / Frontend / InitHeaderSearch.cpp . For example.

 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", "i686-apple-darwin10", "", "x86_64", triple); AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", "i686-apple-darwin8", "", "", triple); // ... 

For Linux, this code has a notification:

 llvm_unreachable("Include management is handled in the driver."); 

In clang/lib/Driver/ we can find more of these paths in files such as ToolChains.cpp , CrossWindowsToolChain.cpp and MinGWToolChain.cpp .

I was hoping the code in InitHeaderSearch.cpp would be open to Python via libclang.

+2
source share

All Articles