With GCC 16, why can a C++ build pass -Werror at -O0 but fail at -O2 on -Wmaybe-uninitialized, and what does -fanalyzer add?
answer
- warnings that ride on the optimizer
- state tracked only when optimizing
- null paths isolated at -O2
- symbolic execution, neither sound nor complete
- suitable only for C in this release
basics
~20 sSome GCC warnings use data-flow analysis that only runs when optimizing: -Wmaybe-uninitialized is impossible at -O0, so an -O2 build sees new reports. -fanalyzer adds costly path-based checking, but the GCC 16 manual calls it suitable only for C code.
solid answer
~50 sThe manual says `-Wmaybe-uninitialized` warnings "are only possible in optimizing compilation, because otherwise GCC does not keep track of the state of variables". So an `-O0` debug build with `-Werror` can be clean while the `-O2` CI build fails on the same source. Optimization also brings false positives, and inlining changes from one release to the next move them around. `-Wnull-dereference`, which is in neither `-Wall` nor `-Wextra`, is only active with `-fdelete-null-pointer-checks` and its precision depends on the optimization options. `-fanalyzer` performs "coverage-guided symbolic execution", reports problems such as double free, leaks and null dereference along interprocedural paths, and is much more expensive than normal warnings. GCC 16's manual says it is "only suitable for use on C code in this release", so for C++ it is a tool for C parts, not a gate.
code
cpp · 17 lines#include <cstdio>
void apply(int x) { std::printf("level %d\n", x); }
void set_quality(int y) {
int x;
switch (y) {
case 1: x = 1; break;
case 2: x = 4; break;
case 3: x = 5;
}
apply(x); // GCC may report -Wmaybe-uninitialized here at -O2, never at -O0
}
int main() {
set_quality(2); // y outside 1..3 would read an indeterminate value: undefined behaviour
}go deeper
Know that some warnings only appear in optimized builds, so a clean debug build does not mean a clean release build.
Explain that -Wmaybe-uninitialized needs the optimizer's variable tracking and can produce both false positives and false negatives.
Gate warnings at the shipped optimization level, expect upgrades to move optimizer-based warnings, and know -fanalyzer's C-only status in GCC 16.
Decide where expensive analysis runs in the pipeline and which optimizer-dependent warnings are allowed to fail a build.
## Two kinds of warnings Most GCC warnings come from the **front end**: they look at one expression or declaration and need nothing else. A second group needs **data-flow** or path information, which GCC computes in optimization passes. The manual says it directly: "Some other warnings may not be issued at all unless optimization is enabled. While optimization in general improves the efficacy of warnings about control and data-flow problems, in some cases it may also cause false positives." This has a practical consequence. A team that compiles debug builds at `-O0` with `-Werror` and release builds at `-O2` can see a clean debug build and a failing release build of the same commit. ## -Wmaybe-uninitialized `-Wmaybe-uninitialized` is enabled by both `-Wall` and `-Wextra`. It fires when some path initializes a variable and another path might not, and GCC cannot prove the bad path never runs. The manual is explicit about two limits: - **Only with optimization:** "These warnings are only possible in optimizing compilation, because otherwise GCC does not keep track of the state of variables." - **Both kinds of error:** it is "known not to warn in many situations (false negatives)", and it is optional because GCC "may not be able to determine when the code is correct in spite of appearing to have an error". The manual's example is a `switch` that sets `x` for cases 1 to 3 and then uses `x`; if the caller only ever passes 1 to 3 the code is correct, but GCC cannot know that. The repair is to initialize the variable or add a `default:` that handles the other values; that is a code change, not a flag change. ## -Wnull-dereference `-Wnull-dereference` warns about paths that dereference a null pointer. It is in neither umbrella set. The manual ties it to the optimizer: - it "is only active when `-fdelete-null-pointer-checks` is active" - "the precision of the warnings depends on the optimization options used" The related pass, `-fisolate-erroneous-paths-dereference`, which finds those paths and turns them into traps, is enabled by default at `-O2` and higher. Run this warning in an optimized build, or it will find little. ## -fanalyzer in GCC 16 `-fanalyzer` is a separate static analysis inside GCC. The manual describes it as: - "coverage-guided symbolic execution" looking for interesting interprocedural paths - "much more expensive than other GCC warnings" - "neither sound nor complete": a bug-finding tool with false positives and false negatives It enables a family of `-Wanalyzer-*` warnings, including `-Wanalyzer-double-free`, `-Wanalyzer-malloc-leak`, `-Wanalyzer-null-dereference`, `-Wanalyzer-file-leak` and `-Wanalyzer-fd-leak`, and prints the path of events that leads to each problem. The crucial line for a C++ team: "The analyzer is only suitable for use on C code in this release." In a C++ codebase it is reasonable for C sources or an experiment, not as a `-Werror` gate on C++ translation units. ## What to do about it | Situation | Action | |---|---| | `-Werror` fails only in the `-O2` build | Treat the optimized build as the warning gate; fix the flagged code | | A real false positive after inlining | Initialize the variable, or a narrow pragma with a comment | | Want null-dereference warnings | Add `-Wnull-dereference` to an optimized CI job | | Want deep path analysis on C++ | Not `-fanalyzer` in GCC 16; run it on C sources only | 1. Run the warning-gated build at the **optimization level you ship**, so the gate sees what release sees. 2. Expect **compiler upgrades** to change these warnings, because inlining and optimization change between releases. 3. Keep sanitizer builds out of the `-Werror` job; the manual notes sanitizers raise false positives "most notably those around `-Wmaybe-uninitialized`".
- Should the -Werror CI job compile at -O0 for faster builds?Not if it is the warning gate. Several GCC warnings, including `-Wmaybe-uninitialized` and the null-dereference paths, depend on optimization passes, so an `-O0` gate misses what an `-O2` release build reports. Gate at the level you ship, and keep fast `-O0` builds for iteration.
- Why not make -fanalyzer part of the C++ warning gate in GCC 16?The GCC 16 manual says the analyzer is only suitable for C code in this release, and it is much more expensive than ordinary warnings, with false positives and negatives. On C++ translation units it would add cost and noise without the support level a gate needs.
- -Wmaybe-uninitialized works the same at -O0 and -O2
- A warning that appears only at -O2 must be a compiler bug
- -Wnull-dereference is enabled by -Wall
- -fanalyzer proves the absence of memory bugs
- GCC 16's -fanalyzer fully supports C++ code
Assumes GCC 16.2; its manual states the analyzer is only suitable for C code in this release, which may change in later releases.