AST generated by Libclang’s python binding unable to parse certain tokens in C++ source codes

前端 未结 1 1891
野的像风
野的像风 2021-01-17 03:07

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 f

相关标签:
1条回答
  • 2021-01-17 04:01

    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');
    }
    
    0 讨论(0)
提交回复
热议问题