0

I'm wondering how to activate GCC compiler flags in code, as opposed to including them in the compilation command on the command line.

Consider the following minimal example, which produces the warning -Wswitch-bool (I think by default but at least with -Wall enabled):

switch (true) {
default:
    break;
}

This warning can be suppressed by providing the flag -Wno-switch-bool when compiling the program via the command line.

Is it possible to activate this flag in code? The following naive attempt doesn't work, since Wno-switch-bool is not a legal identifier (dashes are interpreted as minuses):

#define Wno-switch-bool

What is the appropriate command/syntax to use?

Note: This question is not specifically about warnings. I.e. I'm not asking about how to deactivate a specific class of warnings, which I know is best done via #pragma GCC diagnostic ignore, etc. This is a general question about how to enable GCC compiler flags in code. For instance, another example could be how I could activate -Werror, or -fno-implicit-templates via preprocessor directives (whether or not that would be ill-advised).

andreasdr
  • 3,804
  • 4
  • 28
  • 32
  • 1
    Had you already a look at this page?: [GCC 6.62.13 Diagnostic Pragmas](https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html) – Scheff's Cat Aug 26 '20 at 07:26
  • If you're not asking slot warnings than having your primary example being about disabling a warning it's pretty confusing. Why do you want to change `-fno-implicit-templates` mid code? If there isn't a good use case it's unlikely gcc have implemented support for it – Alan Birtles Aug 26 '20 at 07:33
  • I'm a bit confused by your question (a conflict between the start of the question and the note in the end of it). You are aware of `#pragma GCC diagnostic ignored`, but do you mean you are aware of its use in combination with `#pragma GCC diagnostic push` and `#pragma GCC diagnostic pop`? It seems like the latter is exactly what your first half of your question is asking for. – dfrib Aug 26 '20 at 07:47
  • I would suppose that many flags which affect code generation can only be sensibly applied to complete translation units. If they change the ABI it's even likely that they need to be applied to whole modules/programs, although the prevalent paradigm of compiling separate translation units cannot enforce that (struct packing is an example and a counter-example at the same time; it can be applied to a specific struct but not to others in the same TU, but should be done consistently across TUs.) – Peter - Reinstate Monica Aug 26 '20 at 07:49
  • You cannot. Why do you want to? – n. m. could be an AI Aug 26 '20 at 08:40
  • If this can't be done, that's a perfectly valid answer. The background is that I want to deactivate the warning `-Wstringop-overflow=`, but haven't been able to do so via `pragma GCC diagnostics ignore` for some reason (perhaps related to the fact that it takes an argument) . I then found the option `-Wno-stringop-overflow`, which is supposed to do the same thing. But I want to activate this option in only a single file. – andreasdr Aug 26 '20 at 09:29
  • 1
    Well, most build environments allow you to change compiler options for single files. – Peter - Reinstate Monica Aug 26 '20 at 09:53

1 Answers1

1

Is it possible to activate this flag in code?

Yes, you can use GCC's diagnostic pragmas to apply a specific pragma temporarily around a specific snippet of code, by remembering and subsequently restoring the state of the diagnostics prior to a source-local tweaking of it. E.g.:

#pragma GCC diagnostic push
//          ^^^^^^^^^^^^^^^ remember the state of the diagnostics
#pragma GCC diagnostic ignored "-Wswitch-bool"
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tweak diagnostics
switch (true) {
default:
    break;
}
#pragma GCC diagnostic pop
//          ^^^^^^^^^^^^^^^ restore the state of the diagnostics
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • 2
    Unfortunately, although this was the OP's example the question was meant more broadly. – Peter - Reinstate Monica Aug 26 '20 at 07:54
  • This doesn't work. To deactivate the warning, one has to write `#pragma GCC diagnostic ignored "-Wswitch-bool"` OR compile with the flag `-Wno-switch-bool` (notice that they differ by the string `no-`) My question is whether I can activate `-Wno-switch-bool` via preprocessor directives in the code. (Note that this is just an example, and for this particular example, I know that the `pragma` solution would be the best option.) – andreasdr Aug 26 '20 at 09:23