擅长:python、mysql、java
<p>你自己接受的答案是不正确的。<code>libclang</code><a href="http://clang.llvm.org/doxygen/group__COMPILATIONDB.html" rel="nofollow noreferrer">does support compilation databases</a>和{a2},libclang python绑定。</p>
<p>造成混淆的主要原因可能是libclang知道/使用的编译标志只是可以传递给clang前端的所有参数的子集。支持编译数据库,但不能自动工作:必须手动加载和查询它。这样的方法应该有效:</p>
<pre><code>#!/usr/bin/env python
from argparse import ArgumentParser, FileType
from clang import cindex
compilation_database_path = args.compilation_database.name
source_file_path = args.source_file.name
index = cindex.Index.create()
# Step 1: load the compilation database
compdb = cindex.CompilationDatabase.fromDirectory(compilation_database_path)
# Step 2: query compilation flags
try:
file_args = compdb.getCompileCommands(source_file_path)
translation_unit = index.parse(source_file_path, file_args)
file_nodes = get_nodes_in_file(translation_unit.cursor, source_file_path)
print [p.spelling for p in file_nodes]
except CompilationDatabaseError:
print 'Could not load compilation flags for', source_file_path
</code></pre>