AST生成的Libclang的python绑定无法解析C ++源代码中的某些标记 [英] AST generated by Libclang’s python binding unable to parse certain tokens in C++ source codes

查看:338
本文介绍了AST生成的Libclang的python绑定无法解析C ++源代码中的某些标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Libclang的python绑定。我基本上有两个查询:


  1. 我想知道我们如何解析库函数,一个库已经包括..
    例如当我有以下源代码 -

      char * a =(char *)malloc 




    • Libclang无法解析malloc(),因为既不包括stdlib在此代码中,也没有为malloc提供用户定义的定义。


  2. 未使用构造函数定义的对象未被识别由Libclang的AST。例如,在源代码中 -

      vector< int>颜色; 
    color.push_back(1);
    color.push_back(2);


不会解析push_back但是写成这样时:

  vector< int> color = new vector< int>(); 
color.push_back(1);
color.push_back(2);

正确解析。




  • 这种行为的另一个令人惊讶的表现是当这些对象作为函数参数传递到用户定义的函数时。例如

      bool check(int ** grid,vector< char> color){
    color.push_back一个');
    }




push_back仍然无法识别,但是在写入时,会正确解析。

  bool check(int ** grid,vector< char& ,int anc,int cur){
vector< char> color = new vector< int>()
color.push_back('a');

如果有人能够建议解决方法,这将是巨大的。

解决方案

您需要添加以下参数

$当调用解析时,b
$ b

-x c ++ -std = c ++ 11



,否则默认为解析C代码.h文件。
您可以将头文件重命名为.hpp



这是我的帮助脚本。

 来自cindex import * 
def get_cursor_from_file(filename,my_args = []):
index = Index.create()
options = TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD
file_obj = index.parse(filename,args = my_args,options = options)
对于file_obj.diagnostics中的i:
print i
return file_obj.cursor


x = get_cursor_from_file('test.cpp')

对于x.get_children()中的c:
print c.spelling



我测试的源文件看起来像这样

  #include< vector> 
using namespace std;
int main(){
char * a =(char *)malloc(4);
矢量< int>颜色;

vector< int> * color2 = new vector< int>();
color.push_back(1);
color.push_back(2);
}

bool check(int ** grid,vector< char> color){
color.push_back('a');
}


I am using Libclang’s python binding. I have basically two queries:

  1. I want to know how can we parse library function which are neither defined by user nor for which a library has been included..   For e.g. when I have the following source code –

     char* a=(char *)malloc(4);
    

    • Libclang is unable to parse malloc( ) because neither stdlib has been included in this code nor has a user-defined definition provided for malloc.
  2. An object not defined using a constructor is not recognized by Libclang’s AST. For e.g., in the source code -

    vector<int> color;
    color.push_back(1);
    color.push_back(2);
    

the push_back( )statements will not be parsed but when written like this:

        vector<int> color=new vector<int>();
        color.push_back(1);
        color.push_back(2);

it parses correctly.

  • Another surprising manifestation of this behavior is when such objects are passed as function parameters to a user defined function. For e.g.

    bool check(int **grid, vector<char> color){
    color.push_back('a');
    }
    

push_back( ) is still not identified but when this is written, things are parsed correctly

    bool check(int **grid, vector<char> color, int anc, int cur){
    vector<char> color = new vector<int>()
    color.push_back('a');

Would be great if someone is able to suggest a workaround. Perhaps there’s a flag which when set is able to avoid this?

解决方案

You need to add the following argument

-x c++ -std=c++11

when calling parse, otherwise it defaults to parsing C code for .h files. You might rename the header file to .hpp

Here is what my helper script looks like.

from cindex import *
def get_cursor_from_file(filename,my_args=[]):
    index = Index.create()
    options = TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD
    file_obj = index.parse(filename,args=my_args,options=options)
    for i in file_obj.diagnostics:
        print i
    return file_obj.cursor


x = get_cursor_from_file('test.cpp')

for c in x.get_children():
    print c.spelling

The source file I tested on looks like this

#include <vector>
using namespace std;
int main(){
 char* a=(char *)malloc(4);
 vector<int> color;

 vector<int> *color2=new vector<int>();
 color.push_back(1);
 color.push_back(2);
}

bool check(int **grid, vector<char> color){
    color.push_back('a');
}

这篇关于AST生成的Libclang的python绑定无法解析C ++源代码中的某些标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆