-3

I am using GCC version 8.2 On several pieces of code, I use small functions. On each one of the functions, I have tests (i.e. Unity framework tests). The tests are defined as #define macros, testing very specific things. For instance, if a number if positive.

Now, when compiling the code using -Wextra flag, I am getting warning about unused variables, although I am using them on the defined macros.

The question is, GCC does not recognize a macro as using a variable, or am I missing something?

Example:

    #define compare(a,b) ( ((a) == (b)) ? 1 : 0 )
    ...
    void f() {
        int a;
        a = f1();
        if(compare(a,123))
            printf("It works");
    }

In this case, GCC would warning about unused variable a, although it is being used by the macro (besides being attributed a value by function f1()).

Felipe GM
  • 129
  • 1
  • 8
  • 2
    Hm. Somehow it is hard to believe you here. Please post [MCVE] and the exact compiler output. – Eugene Sh. Jan 17 '19 at 20:21
  • [Couldn't replicate](https://godbolt.org/z/IrAwZZ). – KamilCuk Jan 17 '19 at 20:22
  • What is `f1`? What does it return? Could it be that the compiler deduces that `a` will *never* or *always* be equal to `123` and therefore optimize the "usage" away? You could try looking at the generated assembly code to find out. – Some programmer dude Jan 17 '19 at 20:22
  • 1
    @Someprogrammerdude In that case there will be a different warning, such as "expression is always true" or something. – Eugene Sh. Jan 17 '19 at 20:25
  • Thanks for the fast replies guys. I can post the entire example cause it is a HUGE complex system. i've tried to reduce as much as possible on the example code to explain the problem I am having. i will try to create a small working example that gives me the result and will return ASAP – Felipe GM Jan 17 '19 at 20:26
  • Hi guys, apparently it was another issue indeed. It was hard to detect because I am using Unity framework for verification of my system. The way it works is by using testing macros. The macros are then redefined some place else in the framework, where the testing really takes place. Digging deeper, I've found out that portions of the code were not being implemented. This way, the compiler was not really using the macro for anything, thus the variable was indeed unused! Thanks again for the comments. everyone was fast against the idea, which lead me to think the problem could be other place. – Felipe GM Jan 17 '19 at 21:07

1 Answers1

2

This is not the case, at least with the example you supplied. Here is a Minimal, Complete, and Verifiable demonstration:

#include <stdio.h>

#define compare(a,b) ( ((a) == (b)) ? 1 : 0 )

int f1() {
    return 42;
}

void f() { // your code
    int a;
    a = f1();
    if (compare(a, 123))
        printf("It works");
}

int main(int argc, char *argv[]) {
    f();
    return 0;
}

When compiled with gcc -Wall -Wunused (yes, this is redundant) using gcc 8.2 or 7.3 there are no warnings or errors.

TypeIA
  • 16,916
  • 1
  • 38
  • 52