Python で libclang を介して C++ ソース ファイルを解析するときに、特定の関数宣言のすべての参照 (行と列の位置) を見つけようとしています。
例えば:
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z, q;
z = addition (5,3);
q = addition (5,5);
cout << "The first result is " << z;
cout << "The second result is " << q;
}
addition
したがって、上記のソース ファイルの場合、 5 行目の関数宣言については、15 行目と 16 行目find_all_function_decl_references
の参照を (下記参照) に返したいと思います。addition
私はこれを試しました(ここから適応)
import clang.cindex
import ccsyspath
index = clang.cindex.Index.create()
translation_unit = index.parse(filename, args=args)
for node in translation_unit.cursor.walk_preorder():
node_definition = node.get_definition()
if node.location.file is None:
continue
if node.location.file.name != sourcefile:
continue
if node_def is None:
pass
if node.kind.name == 'FUNCTION_DECL':
if node.kind.is_reference():
find_all_function_decl_references(node_definition.displayname) # TODO
もう 1 つの方法は、リストにあるすべての関数宣言を保存し、find_all_function_decl_references
それぞれに対してメソッドを実行することです。
これにアプローチする方法を知っている人はいますか?このfind_all_function_decl_references
方法はどうでしょう。(私はlibclang
Pythonと非常に新しいです。)
が何らかのタイプへのすべての参照を見つけているところを見てきましdef find_typerefs
たが、私のニーズに合わせてそれを実装する方法がわかりません。
理想的には、あらゆる宣言のすべての参照を取得できるようにしたいと考えています。関数だけでなく、変数の宣言、パラメーターの宣言 (例: 上記の例の 7 行目のa
and b
)、クラスの宣言など。
編集アンドリューのコメントに 続いて、私のセットアップ仕様に関する詳細を以下に示します。
- LLVM 3.8.0-win64
- libclang-py3 3.8.1
- Python3.5.1 (WindowsではCPythonを想定)
- については、こちらの回答で提案されているものと別の
args
回答のものの両方を試しました。
*私の小さなプログラミング経験を考えると、それがどのように機能するかについての簡単な説明とともに回答をいただければ幸いです.