按照此question https://stackoverflow.com/questions/37098725/parsing-with-libclang-unable-to-parse-certain-tokens-python-in-windows and Andrew's https://stackoverflow.com/a/37100397/6167676建议,我正在尝试liblang 添加编译系统包含路径(在 Windows 中)为了我的 Python 代码
import clang.cindex
def parse_decl(node):
reference_node = node.get_definition()
if node.kind.is_declaration():
print(node.kind, node.kind.name,
node.location.line, ',', node.location.column,
reference_node.displayname)
for ch in node.get_children():
parse_decl(ch)
# configure path
clang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll')
index = clang.cindex.Index.create()
trans_unit = index.parse(r'C:\path\to\sourcefile\test.cpp', args=['-std=c++11'])
parse_decl(trans_unit.cursor)
to 完全地 解析C++源文件像这个
/* test.cpp
*/
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
void readfunction(vector<double>& numbers, ifstream& myfile)
{
double number;
while (myfile >> number) {
numbers.push_back(number);}
}
double meanfunction(vector<double>& numbers)
{
double total=0;
vector<double>::const_iterator i;
for (i=numbers.begin(); i!=numbers.end(); ++i) {
total +=*i; }
return total/numbers.size();
}
现在,如果没有正确设置编译器系统包含路径(使用 Windows),我会得到以下输出:
CursorKind.USING_DIRECTIVE USING_DIRECTIVE 8 , 17 std
CursorKind.VAR_DECL VAR_DECL 10 , 6 readfunction
Process finished with exit code 0
<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 3, column 10>, spelling "'iostream' file not found">
不幸的是,我无法理解(Python 和 Clang 中的新功能)approach https://stackoverflow.com/a/11946295/6167676或者如何实现这个solution https://stackoverflow.com/questions/26440911/python-clang-does-not-search-system-include-paths在我的Python代码中。
我也尝试过cc系统路径 https://github.com/AndrewWalker/ccsyspath,但我没有“针对 Windows 进行调整”的技能。
有人知道如何解决这个问题吗?