AST, created by the python Libclangs bundle, unable to parse specific tokens in C ++ source code

I am using python binding for Libclangs. I have basically two queries:

  • I want to know how we can analyze a library function that is not user defined and for which the library was not included. E.g. when I have the following source code -

    char* a=(char *)malloc(4); 
    • Libclang cannot parse malloc () since no stdlib has been included in this code and does not have a user-defined definition for malloc.
  • An object that is not defined using the constructor is not recognized by the Libclangs AST. For example, in the source code -

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

push_back () statements will not be parsed, but when writing:

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

he analyzes correctly.

  • Another unexpected manifestation of this behavior is that such objects are passed as functional parameters to a user-defined function. For example,

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

push_back () is still not identified, but when it is written, everything is parsed correctly

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

It would be great if someone could suggest a workaround. Perhaps this is a flag that, when set, can avoid this?

0
c ++ python parsing clang libclang
source share
1 answer

You need to add the following argument

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

when calling parse, otherwise, the default is to parse C. code for .h files. You can 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 is as follows

 #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
source share

All Articles