Honor -Wstrict-aliasing explicitly, even with -fno-strict-aliasing.
C-family front-end warns about strict aliasing violations when explicitly requested, regardless of -fno-strict-aliasing.
The C-family front-end now correctly honors an explicit -Wstrict-aliasing warning flag, even when -fno-strict-aliasing is also enabled. Previously, -fno-strict-aliasing would unconditionally silence this warning. This change ensures that users who explicitly request strict aliasing checks receive them, as the front-end’s type-based check does not require optimization to be active. The back-end’s more complex flow-sensitive checks are still subject to optimization flags.
In Details
The strict_aliasing_warning function in c-warn.cc has been modified to not gate its execution on flag_strict_aliasing. Instead, it now uses temp_override and make_temp_override (moved from cp/cp-tree.h to c-family/c-common.h) to temporarily enable strict aliasing checks only when the -Wstrict-aliasing option is explicitly set, irrespective of -fno-strict-aliasing. This preserves the behavior for implied warnings (e.g., from -Wall) while honoring explicit user requests for the front-end's type-based analysis.
- -Wstrict-aliasing
- A GCC warning option that diagnoses violations of the strict aliasing rule, which states that pointers of different types should not be used to access the same memory location. The front-end check is type-based, while the back-end performs flow-sensitive analysis.
- -fno-strict-aliasing
- A GCC optimization flag that disables optimizations that rely on the strict aliasing rule. This can lead to incorrect behavior if the code violates the rule, but may improve performance by allowing more aggressive optimizations.
- strict aliasing rule
- A C/C++ language rule that restricts how memory can be accessed through pointers of different types. It ensures that aliasing does not disrupt compiler optimizations. Violating this rule can lead to undefined behavior.
- type-based diagnostic
- A compiler check that analyzes code based on the declared types of variables and pointers, without considering runtime execution flow or optimization state.