[cfe-dev] How do I detect global variables?

Nico Weber nicolasweber at gmx.de
Wed Jul 23 05:00:27 PDT 2008


On 23.07.2008, at 01:56, Eli Friedman wrote:

> On Tue, Jul 22, 2008 at 2:18 PM, Nico Weber <nicolasweber at gmx.de>  
> wrote:
>> Now, what's the best method to find all global variable definitions?
>> (global functions are not interesting, and I don't want to find
>> globals form system headers).
>> Right now, I'm trying to overwrite ActOnDeclarator. In there, I only
>> look at variables with FileContext, and reject everything that is
>> `extern` (I only want the definitions of globals). Furthermore, I try
>> to skip functions by checking for PQ_FunctionSpecifier, but that does
>> not seem to skip all functions (for example, the `main` function of  
>> my
>> test input program).
> Right... it would be easier to do this from an ASTConsumer, where you
> would have access to types.  Fundamentally, in C, it's impossible to
> tell apart a global function declaration and a global variable
> declaration without resolving typedefs and __typeof expressions.


> If you want to try and resolve typedefs, try looking at the
> implementation of MinimalAction in clang/lib/Parser/MinimalAction.cpp.
> Resolving __typeof would require implementing a large chunk of Sema,
> so that's probably not something you want to mess with.

Thanks for the thorough reply. It helped a lot, my program now does  
what I wanted it to do.

>> Can you give me a few hints where I should look to add that
>> functionality?
> If you want to contribute it to clang?  My first instinct is
> clang/Driver/ASTConsumers.cpp, assuming you wrote an implementation as
> an ASTConsumer.  That said, I'm not sure how useful this would be; if
> anyone else would find this useful, please comment.

No, I don't want to contribute this to clang. It's just my little "get- 
to-know-clang" project. I don't think it's very useful myself.

Thanks again,

More information about the cfe-dev mailing list