Linking files while using Clang AST matchers to implement a call graph

Dear all,

I am trying to implement an ability to keep track of reads and writes to a variables and objects. The object can be passed by reference or by pointer to other functions.
I am using CLang AST matcher for this task.
My solution seems to work, but only when all the functions are located in the same source file.
When functionA calls functionB, whose definition is located in another cpp file, I am unable to get the location of the definition of functionB, I only able to get the declaration which located in its header file.

My questions are:
How I can get to the definition to a callee function from a caller location which located in a different source file (different AST)?
Do I need to somehow pass the linkage stage to make it work?

I have seen in other posts that CLang libTooling does not support Cross Translation Unit feature.
I also seen that LibClang allowed to impenitent this by using Unified Symbol Resolution. Does libTooling offer something like that?

Is there any other option to getting to function definition when more than one source file is used?

I have searched the web for a few days to find any solution with no luck.
Any help will be much appreciated.

Thank you

Here is more details about how and what I am trying to accomplish. It is a bit LONG so please don’t read it if you don’t have time:

How I can get to the definition to a callee function from a caller location which located in a different source file (different AST)?

AFAIK you have two options, under the hoods both of them use the ASTImporter class in Clang.

  1. Use the “-ast-merge” option of cc1, which will merge all top level declarations of the given AST(PCH) file into the actual ASTContext and then the original compiler action will be executed. You may merge as many files as you want this way.
  2. Use CrossTranslationUnitContext::getCrossTUDefinition() from the CTU library to load a function definition from an external AST file and merge it into the the original AST. Clang static analyzer is a client of the CTU library, so you may find in the static analyzer code how we use it (CallEvent.cpp).

Hope this helps,
Gabor

Thank you so much for your help. I will check these options.

I was able to make a connection between nodes located in different ASTs using Name Mangling.
Every function, and parameter has its own unique mangled name. I am getting these names from MangleContext and pragmatically connect the functions later in my application. It seems to work fine.

Thank you so much for your help. I will check these options.

I was able to make a connection between nodes located in different ASTs using Name Mangling.
Every function, and parameter has its own unique mangled name. I am getting these names from MangleContext and pragmatically connect the functions later in my application. It seems to work fine.

This might be good enough. If you want anything more precise you might try asking your linker about this (for example ld with —cref option).

Hope this helps!

Jan