AST generated by Libclang’s python binding unable

2019-02-20 22:21发布

问题:

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?

回答1:

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');
}