1

Firebird's C preprocessor, gpre, creates the following definition in the C code it generates from my .e source files:

static const ISC_STATUS *gds__null = 0;    /* dummy status vector */

I'm not using this value anywhere in my code. Perhaps I should be, but I don't appear to have any need for it (any suggestions as to its appropriate usage are welcomed). In any case, as I'm not using the variable, I get the following compiler warning from gcc:

warning: 'gds__null' defined but not used

I'm wondering if there is some way to tell the compiler that I'm not going to be using the value of this variable, either in the code (keeping in mind that the definition is being inserted by the gpre preprocessor; I have no way to change the definition) or via a command-line argument to the compiler. Or perhaps there is some way to instruct gpre not to inject this definition into the code if we're not using it. I'd like to preserve other unused variable warnings, so -Wno-unused is not an option.

The solution here sort of works, as I can insert the following into an arbitrary function within my .e source file and it gets rid of the warning:

(void) gds__null;

However, it seems inelegant to place this line arbitrarily into a function where it's not serving any clear purpose just to get rid of the compiler warnings. I'm hopeful that there is a better solution. My last resort would be doing some sed replacement on the generated C files post-gpre/pre-gcc, but I'm hoping it doesn't come to that.

Community
  • 1
  • 1
wickstopher
  • 981
  • 6
  • 18

1 Answers1

1

The way to tell gcc to suppress the unused-variable warning on a specific variable is to add __attribute__((unused)) to its declaration.

You could inject this attribute using the preprocessor:

-Dgds__null="gds__null __attribute__((unused))"

Or, avoiding spaces in command line arguments:

-Dgds__null=(gds__null)__attribute__((unused))

This will of course break if you do end up using that variable as it is intended.

It might be better to add a function specifically for the purpose of suppressing the warning; this would itself have to suppress its own unused function warning:

static void suppress_unused_warnings() {
  (void) suppress_unused_warnings;
  (void) gds__null;
}
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • Thanks, accepted your answer. Can you, however, be a little more specific on how exactly using the preprocessor directive breaks the code if the variable comes into use down the line? Isn't it just a note to the preprocessor that it doesn't have to generate warnings about that variable being unused if in fact it isn't? The variable should still be usable if the code decides to use it, no? – wickstopher Aug 04 '15 at 19:44
  • @wickstopher the problem is that it'll replace *every* occurrence of the variable `gds__null` with `gds__null __attribute__((unused))` - the preprocessor doesn't know the difference between a declaration and a usage of the variable. – ecatmur Aug 04 '15 at 19:46
  • Hmm, but in that case I'm sure the code would just fail to compile as opposed to manifesting difficult-to-pinpoint problems. Breakage at compile time is acceptable, because I can go back to my Makefile and remove the directive. Potential breakage at runtime down the line (if this variable ended up being used and somebody forgot to update the Makefile) would be a bigger issue. – wickstopher Aug 04 '15 at 21:50
  • @wickstopher oh, that's OK then - yes, the code will fail to compile (attributes can only appear on a declaration). There won't be any danger of the code compiling and breaking at runtime. – ecatmur Aug 05 '15 at 09:15