2

In the spirit of What are the consequences of ignoring: warning: unused parameter, but I have static functions that are unused,

#include <stdlib.h> /* EXIT_SUCCESS */
#include <stdio.h>  /* fprintf */

#define ANIMAL Sloth
#include "Animal.h"

#define ANIMAL Llama
#include "Animal.h"

int main(void) {
    printf("%s\n%s\n%s\n%s\n%s\n", HelloSloth(), SleepySloth(), HelloLlama(),
        GoodbyeSloth(), GoodbyeLlama());
    return EXIT_SUCCESS;
}

static void foo(void) {
}

Animal.h

#ifndef ANIMAL
#error ANIMAL is undefined.
#endif

#ifdef CAT
#undef CAT
#endif
#ifdef CAT_
#undef CAT_
#endif
#ifdef A_
#undef A_
#endif
#ifdef QUOTE
#undef QUOTE
#endif
#ifdef QUOTE_
#undef QUOTE_
#endif
#define CAT_(x, y) x ## y
#define CAT(x, y) CAT_(x, y)
#define A_(thing) CAT(thing, ANIMAL)
#define QUOTE_(name) #name
#define QUOTE(name) QUOTE_(name)

static const char *A_(Hello)(void) { return "Hello " QUOTE(ANIMAL) "!"; }
static const char *A_(Goodbye)(void) { return "Goodbye " QUOTE(ANIMAL) "."; }
static const char *A_(Sleepy)(void) { return QUOTE(ANIMAL) " is sleeping."; }

#undef ANIMAL

I absolutely want SleepyLlama to be detected as unused and optimised out of the code by a clever compiler. I don't want to hear about it; potentially, as I expand into more ANIMALs and more actions, it becomes distracting. However, I don't want to interfere with the possible warning about foo being unused.

MSVC (14) has #pragma warning(push), but apparently doesn't check anyway; gcc (4.2) and clang have -Wunused-function. I've tried https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html but they don't seem to work for functions. Is there a way to get all warnings about foo and not about SleepyLlama across different compilers?

Community
  • 1
  • 1
Neil
  • 1,767
  • 2
  • 16
  • 22
  • Why bother with those "templates". You can easily use a simple function to do the job.... – LPs May 08 '17 at 07:24
  • Do `Wno-unused-function` in gcc (`-Wunused-function` means to turn it on). Or you could use a different technique to static functions in headers – M.M May 08 '17 at 07:24
  • 4
    Why simple if one can make it complicated? Is this some obfuscation class? Seriously: don't get too fancy with macros! Not only the code is less readable, but it is also much harder to debug. Concentrate on writing readble code! And remove the unrelated tag. C and C++ are different languages! – too honest for this site May 08 '17 at 07:26
  • 1
    What is the actual problem you are trying to solve? – Lundin May 08 '17 at 08:08
  • Why don't you just use them in a dummy wrapper? Create a dummy function that just calls all these functions generated and also itself. – Ajay Brahmakshatriya May 08 '17 at 08:34
  • Removed C++ tag; that is true, you would be no reason to do this in C++. In C, the header behaves kind of like a template. I am wondering what's the standard way to silence warnings about unused functions, but only in the header, while still optimising out the unused functions. – Neil May 08 '17 at 23:23

2 Answers2

4

Not a way of disabling warnings but you can suppress the not used warning by actually using them.

In your animals.h add the lines -

static const char *A_(DummyWrapper)(void) {
    (void)A_(Hello)(); 
    (void)A_(Goodbye)(); 
    (void)A_(Sleepy)(); 
    (void)A_(DummyWrapper)(); 
}

This should make the compiler not complain about unused functions.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
  • Elegant. Turning on all errors, this worked in GCC4.2.1 -O3, clang1.7 (I had to have two `DummyWrappers` calling each other to get rid of the `-Wunsed-function`,) MSVC14 /O2 (/Ox didn't get rid of the functions from the binary,) BCC32C10.1(7.20?,) and MinGW GCC4.9.3 -O3. – Neil May 09 '17 at 04:31
  • I had suspected some compilers would go on to realize that with a single function it is not actually called and that two functions would be required. In principle even with two functions, a compiler can figure out that it is cycle with no entry points. In that case you can declare a function pointer and assign the dummy wrapper to it. Since the function pointer can be called (potentially from other translation units), the problem would be solved. – Ajay Brahmakshatriya May 09 '17 at 04:36
1

Several years late, but in gcc, you can also use:

__attribute__((unused))

in front of your functions. More details in Inhibit error messages for unused static functions?

fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • I was personally looking for it to be compiler-agnostic, but it has the advantage of being compact and self-documenting. Good option for gcc-style compilers. – Neil Jun 19 '23 at 22:24