12

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.

2
  • 1
    Why do you want to produce "one" AST for several files? Why can't you simply handle one AST for each file? Commented Mar 12, 2013 at 3:49
  • 1
    I wish someone had an answer for this :\ I'm trying to do the same thing right now. Commented Jul 13, 2016 at 5:47

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How did you map the functions out on your own? Please correct me if I am wrong but the Ids will be different from each AST. Do you map it by some kind of hash which was calculated using filename, function name and its parameters? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.