[cfe-dev] Read/Write Accesses of global variables in a Function body...

Eli Friedman eli.friedman at gmail.com
Mon Dec 19 10:01:35 PST 2011


On Mon, Dec 19, 2011 at 6:56 AM, Peter <clang_llvm_123-4 at yahoo.de> wrote:
> Hi everybody,
>
> I am trying to write a recursive AST visitor that detects all read/write
> accesses of global
> variables within the body of a given function declaration.
>
> My Questions are:
>
> 1) How to detect read/write accesses (for a given function) in a simple way?

If you're just looking for simple assignments, your example code is in
the right direction, although I would suggest having the AST visitor
look for assignment operators rather than doing that part yourself.
Things get a lot more complicated if there's a possibility something
takes the address of a global you're interested in (using the "&"
operator, etc.).

> 2) How to detect if a variable is global?

hasLinkage(), or maybe hasGlobalStorage()?  Depends on what exactly
you mean by global.

-Eli



More information about the cfe-dev mailing list