Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AST for multiple source files with clang

I'm doing inter-procedural data-flow analysis with clang. Currently I'm using libtooling to parse source files and call AST visitor. The question is how do I create a single AST for several .c files?

I've tried to use ASTImport class, but it does not support import of some AST nodes. Moreover, I'm doing something wrong when I create and manipulate CompilerIstance and it crashes in destructor.

A very similar option was ASTImportAction but it's not quite clear to me which command-line parameters should be passed to the ClangTool in this case.

The third option was to create ASTUnits for every .c file and to look for definitions in each of them it's not clear how to find correspondence between user-define types, e.g. records. In ASTImport they use IsStructurallyEquivalent() function but it's declared in anonymous namespace so I can only copy all this code into my program. And again it supports not all AST nodes.

From the internet this link http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-August/023865.html seems to be most appropriate but for me the technical details of the solution are not clear.

Any suggestions are welcome. Many thanks.

like image 825
Julia Demyanova Avatar asked Feb 15 '13 11:02

Julia Demyanova


1 Answers

I was trying to do something similar. I did not try to create a single AST though. I was parsing multiple AST's and trying to map the functions out on my own. I am using the AST matchers to get the function calls and then checking them in the other AST's.

I am using the compile_commands.json file to provide for the source file list. OptionsParser.getCompilations().getAllFiles() can get all the source files specified in the compile_commands.json.

When we create a clangTool and run a frontendAction over the tool with our matcher, it searches for the match in all the source files.

There may be more optimal way of doing this. In case some knows of it please point it out.

like image 152
user8210314 Avatar answered Sep 19 '22 04:09

user8210314