during the compilation of a program using Petsc I stumbled upon a construct similar to the following. In Petsc they use it, as far as I understand, to wrap calls to MPI in order to monitor calls to functions using collective communication.
#include <stdio.h>
int f() {return 0;}
int g() {printf("g()\n");return 0;}
#define STMT ( 0 || g() )
int main()
{
STMT;
printf("main()\n");
return 0;
}
compiling with gcc:
gcc -Wall -Werror ./test.c
raises the following error/warning:
./test.c:7:20: error: value computed is not used [-Werror=unused-value]
#define STMT ( 0 || g() )
~~~~~~^~~~~~~~
./test.c:11:3: note: in expansion of macro ‘STMT’
STMT;
^~~~
cc1: all warnings being treated as errors
Why does the compiler issue this warning ? IMHO g() is always executed and the value computed is always used.
Note: tested with: gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1), gcc version 8.3 and gcc version 9