7

この質問アンドリューの提案に従って、私は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)

このような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 の新機能) や、このソリューションを Python コードに実装する方法を理解できません。

ccsyspathも試しましたが、「Windows 用に調整する」スキルがありません。

誰でもこの問題を解決する方法を知っていますか?

4

1 に答える 1