Instances of Variable Usage (SSA form)

Hi,
   I want to use clang to find out all the instances of a variable usage.
e.g. I want to find out every place where the variable 'x' was used? I know
that, in theory, this problem can be solved using the SSA form. How can I do
this in clang?

Regards,
Adil

You could search for all DeclRefExprs that refer to the particular variable declaration (by doing an AST walk).

Note that this will find only syntactic references to a particular variable, but will not trace the data flow. For example,
int x = 0;
int y = x;
use(y); // This will not be a use of x.

(SSA form just guarantees that there is only a single assignment for each variable.)

Anna.

Thank you. This is exactly what I was looking for. I have a small issue though. I’ve used a CompilerInvocation and CompilerInstance to set up clang. Then I use parseAST to parse the clang AST. My program can’t find standard C++ headers like and <stdio.h>. How do I make clang automatically find system include directories (e.g. /usr/include etc).

Regards,
Adil

Thank you. This is exactly what I was looking for. I have a small issue
though. I've used a CompilerInvocation and CompilerInstance to set up
clang. Then I use parseAST to parse the clang AST. My program can't find
standard C++ headers like <string> and <stdio.h>. How do I make clang
automatically find system include directories (e.g. /usr/include etc).

The easiest way is to not code your own Clang setup, and use either
- a clang plugin (http://clang.llvm.org/docs/ClangPlugins.html)
- or LibTooling (http://clang.llvm.org/docs/LibTooling.html)

Cheers,
/Manuel