Identifying declared but unreferenced global variables from a plugin

Jonathan Wakely
Fri Jun 17 09:18:00 GMT 2016

On 17 June 2016 at 08:38, Bertram, Alexander wrote:
> Hi there,
> I'm trying to get the gcc-bridge plugin to dump out a list of _all_
> global variables declared within a translation unit, even if they're
> not used, and I've really hit a brick wall. Can anyone point me in the
> right direction?
> In more detail, say that you have a C source file consisting only of:
>     int magic_number = 42;
> With no functions declared. The global variable and its data is
> written to the object file:
>     $ gcc -c link1.c
>     $ nm link1.o
>     0000000000000000 D magic_number
> But the global variable is not referenced by any functions so the
> plugin never encounters it and doesn't know it exists.

If I understand correctly, all the current plugin hooks are in the
code-generation stages. If a variable is unused and so never gets
emitted by the front-end, then the code-gen passes won't see it, and
your plugin won't see it.

I want to write a plugin that inspects the names of all declarations,
including unused variables/functions and uninstantiated templates, but
it can't be done today. I opened to ask for extra
plugin hooks.

More information about the Gcc-help mailing list