1

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 Answers1

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');
}
Sam P
  • 681
  • 5
  • 19
  • thanks but id doesn't solve my doubt regarding parsing of . I was able to parse cpp files but I am not able to parse codes mentioned above and moreover it don't tell that file is not parsable! I think this answer is not the what I am looking for. thanks for reply. – user3848619 Aug 20 '14 at 06:42
  • I tried parsing it, and got back a valid cursor. The new operator returns a pointer so your text should have been vector * color = new ... – Sam P Aug 22 '14 at 14:54
  • I am having the exact same issue when parsing `vector vecd;` from a C++ source file. Do you know how to set the `-x c++` and `-std=c++11` in code (using libclang in Python)? –  May 07 '16 at 20:43