Identifying declared but unreferenced global variables from a plugin

Bertram, Alexander alex@bedatadriven.com
Fri Jun 17 09:39:00 GMT 2016


Thanks for the pointer.

But I'm not sure if this is the case here: the global variable is
unreferenced, but it has external visibility and is ultimately written
out to the object file as a symbol.

I've kept digging around in the sources, and it looks like varasm.c
might be something in the right direction:

  /* This file handles generation of all the assembler code
      *except* the instructions of a function.
      This includes declarations of variables and their initial values.

      We also output the assembler code for constants stored in memory
      and are responsible for combining constants with the same value.  */

So somehow the code in varasm gets a list of these global declarations
and writes them as data(?) sections in the object file. Where does
varasm get this list?
Will continue to dig, but any pointers would be very helpful!

Alex



On Fri, Jun 17, 2016 at 11:18 AM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> 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
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70757 to ask for extra
> plugin hooks.



More information about the Gcc-help mailing list