GCC diagnostic pragma: targeted warning suppression in embedded C builds
0 reputation · 11 Apr 2020, 15:37 UTC
Goal
In a large embedded C codebase compiled with -Wall -Werror, developers often need to silence harmless -Wunused-variable warnings that arise in legacy or third‑party headers, while preserving the strict error‑on‑warning behavior for all other diagnostics.
Constraints
The suppression must be local—affecting only the code following a #pragma GCC diagnostic push and preceding the matching #pragma GCC diagnostic pop. It must not lower the global warning level, must interact correctly with -Werror, and ideally should be portable or at least conditionally compiled across other embedded compilers.
Unresolved Questions
1. Does placing #pragma GCC diagnostic ignored "-Wunused-variable" inside a push/pop pair prevent the warning from being treated as an error when -Werror is active, while leaving other warnings in the same file still subject to -Werror?
2. If a warning is emitted before the push but after the pop, does it still trigger a build error, confirming the localized scope of the suppression?
3. What is the most reliable way to wrap these pragmas in a header so that they are ignored by non‑GCC compilers without breaking compilation on IAR, Keil, or Clang?