Search code examples
coracle-databasegccpragmaunused-variables

Ignore unused variables in a block of code in c


I have a large codebase of C code which part of it is generated code from the Oracle Pro*C precompiler.

We use the GNU gcc compiler.

The Pro*C precompiler generates code that contains unused variables that emits many warnings related to -Wunused-variable which I'd like to ignore.

I've tried the following which I found in other questions but it doesn't see to work for C code (cut down to a minimal example).

 int main(void)
 {
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-variable"
         int  a=0;
 #pragma GCC diagnostic pop
         int b=0;
         return 0;
 }

I still get the -Wunused-variable error for variable a.

 aa.c: In function 'main':
 aa.c:8:13: warning: unused variable 'b' [-Wunused-variable]
          int b=0;
              ^
 aa.c:6:14: warning: unused variable 'a' [-Wunused-variable]
          int  a=0;
               ^

GCC command:

gcc-8 -Wall -Wextra -pedantic aa.c -o a

Incase you are wondering, if I remove the pop pragma, no warnings are issued.


Solution

  • The solution I found was to add __attribute__((unused)) before the generated variables that were problematic. In this situation there are always only 4 relevant variables so it was possible.

    I wrote a bash command in the make file right after the Pro*C precompiler:

    for var in varA varB varC varD; do sed -i "0,/${var}/{s/\(${var}\)/__attribute__((unused))\1/}" $file_name; done
    

    Hope it can be helpful for someone.