2

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.

SimonW
  • 6,175
  • 4
  • 33
  • 39
  • 1
    It's because the warning can't be generated until the block they were defined in ends. So a possible solution might be to enclose the code containing the unused variables you don't want warnings about in their own block using `{}`. – Some programmer dude Jun 12 '18 at 21:11
  • What happens if you move the pop after the return or after the `}` ? – Yunnosch Jun 12 '18 at 21:11
  • 3
    Usually people silence such a warnings by putting something like `(void)b;` But better clean up your code. – Eugene Sh. Jun 12 '18 at 21:11
  • @Someprogrammerdude I can't this generated code is used later in other parts of the code. – SimonW Jun 12 '18 at 21:13
  • That works perfectly for `Apple LLVM version 9.1.0 (clang-902.0.39.2)`. Did you tried another gcc version? – Jean-Baptiste Yunès Jun 12 '18 at 21:13
  • Is it not an option to just pass `-Wno-unused-variable` on the command line? Since the code is auto-generated, do you really care about gcc's warnings - do you plan to use the warning output as some kind of a sanity check on the precompiler's output? – TypeIA Jun 12 '18 at 21:14
  • @Yunnosch Then it won't warn about my code that I do want to get warnings about, so it's not an option – SimonW Jun 12 '18 at 21:14
  • @EugeneSh. I would if I had control of the code, it's generated... – SimonW Jun 12 '18 at 21:15
  • 1
    Then perhaps put all the generated code in its own source file, where you can disable any warnings you feel like? – Some programmer dude Jun 12 '18 at 21:15
  • 3
    @SimonW You are trying to add pragmas into the code, so I assumed that you have the control... – Eugene Sh. Jun 12 '18 at 21:15
  • @Jean-BaptisteYunès It's a redhat machine I have no control of... I wish I could... – SimonW Jun 12 '18 at 21:15
  • @TypeIA no, part of the code is generated and part not, I do want to get warnings about my code – SimonW Jun 12 '18 at 21:16
  • @Someprogrammerdude The generated code is required for running the rest of the code. Part of the code is written using special syntax that is precompiled to regular C so I can't separate them :( – SimonW Jun 12 '18 at 21:18
  • @SimonW How does those codes are mixed up? Seems to be an X-Y problem... Compile the generated code with unused-variable disabled that's all. – Jean-Baptiste Yunès Jun 12 '18 at 21:18
  • @Jean-BaptisteYunès see previous answer, Part of the code is written using special syntax that is precompiled to regular C so I can't separate them – SimonW Jun 12 '18 at 21:20
  • @SimonW I seriously doubt you can't separate code in some way... Generated code presumably produces result you use after, then isolate generated code in its own function/module, add appropriate return value and parameters and call that from another module. – Jean-Baptiste Yunès Jun 12 '18 at 21:26
  • @EugeneSh. I add the pragmas from outside - in the make file – SimonW Jun 12 '18 at 21:50
  • @Jean-BaptisteYunès The generated code I want to ignore warnings for, creates glabal variables in the top of the file, that some are later used in the generated code at the rest of the file where the generated code and my code are mixed. I think I can find some way to add an attribute that will ignore errors per variable somehow but it's a really ugly solution :/ – SimonW Jun 12 '18 at 21:54
  • 5
    Although it's unlikely to yield a short-term solution, I would urge you to complain to the Pro*C people. It's pretty irresponsible to generate code that yields blatant warnings like these. Wanting your code to compile without warnings (as you do) isn't some kind of fairy tale, it's common and recommended industry practice. – Steve Summit Jun 12 '18 at 22:00
  • You could use `awk` or another text processing tool to strip out warnings pertaining to the Pro*C variables, assuming you can identify them by name – M.M Jun 12 '18 at 22:27
  • 2
    Maybe you could refactor your code so that all the Pro*C is in dedicated functions and then disable that warning for the entire section – M.M Jun 12 '18 at 22:32
  • 1
    Note: I added the Oracle-tag (apparently pro*-c is too sparse to exists) and had to remove the "C" tag. – wildplasser Jun 17 '18 at 10:51

1 Answers1

3

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.

SimonW
  • 6,175
  • 4
  • 33
  • 39