On Mon, Jun 8, 2026 at 4:20 PM Sammy Al Hashemi <[email protected]> wrote:
>
> The -Wstrict-aliasing warning was gated on flag_strict_aliasing, so
> passing -fno-strict-aliasing silenced the warning even when explicitly
> requested with -Wstrict-aliasing. The warning analysis is purely
> type-based and does not depend on the optimization being active, so
> remove the guard to allow the warning to fire independently.
That was not true when the warning was added and is still not true.
When the warning was added, the aliasing sets for the types were
always 0 for -fno-strict-aliasing.
This was changed with r6-5494-gbd04cddf10ff45 . But this is still not
the best because alias_sets_must_conflict_p always returns true for
-fno-strict-aliasing.
I suspect you need to set flag_strict_aliasing around the calls to
alias_sets_conflict_p/alias_sets_must_conflict_p to get better
answers.
Thanks,
Andrea
>
> gcc/c-family/ChangeLog:
>
> * c-warn.cc (strict_aliasing_warning):
>
> gcc/ChangeLog:
>
> * doc/invoke.texi:
>
> gcc/testsuite/ChangeLog:
>
> * c-c++-common/Wstrict-aliasing-with-fno.c: New test.
> ---
> gcc/c-family/c-warn.cc | 3 +--
> gcc/doc/invoke.texi | 16 ++++++++++------
> .../c-c++-common/Wstrict-aliasing-with-fno.c | 11 +++++++++++
> 3 files changed, 22 insertions(+), 8 deletions(-)
> create mode 100644 gcc/testsuite/c-c++-common/Wstrict-aliasing-with-fno.c
>
> diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc
> index 1767d2dc090..2fbc83a6f43 100644
> --- a/gcc/c-family/c-warn.cc
> +++ b/gcc/c-family/c-warn.cc
> @@ -701,8 +701,7 @@ strict_aliasing_warning (location_t loc, tree type, tree
> expr)
> STRIP_NOPS (expr);
> tree otype = TREE_TYPE (expr);
>
> - if (!(flag_strict_aliasing
> - && POINTER_TYPE_P (type)
> + if (!(POINTER_TYPE_P (type)
> && POINTER_TYPE_P (otype)
> && !VOID_TYPE_P (TREE_TYPE (type)))
> /* If the type we are casting to is a ref-all pointer
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 339d1d2c97a..b361290cee4 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -8674,17 +8674,21 @@ the implementation.
> @opindex Wstrict-aliasing
> @opindex Wno-strict-aliasing
> @item -Wstrict-aliasing
> -This option is only active when @option{-fstrict-aliasing} is active.
> -It warns about code that might break the strict aliasing rules that the
> -compiler is using for optimization. The warning does not catch all
> +This option warns about code that might break the strict aliasing rules
> +that the compiler uses for optimization when @option{-fstrict-aliasing}
> +is active. This option is independent of @option{-fstrict-aliasing};
> +it diagnoses code that would violate strict aliasing rules regardless of
> +whether the optimization is enabled. The warning does not catch all
> cases, but does attempt to catch the more common pitfalls. It is
> included in @option{-Wall}.
> It is equivalent to @option{-Wstrict-aliasing=3}.
>
> @item -Wstrict-aliasing=@var{n}
> -This option is only active when @option{-fstrict-aliasing} is active.
> -It warns about code that might break the strict aliasing rules that the
> -compiler is using for optimization.
> +This option warns about code that might break the strict aliasing rules
> +that the compiler uses for optimization when @option{-fstrict-aliasing}
> +is active. This option is independent of @option{-fstrict-aliasing};
> +it diagnoses code that would violate strict aliasing rules regardless of
> +whether the optimization is enabled.
> Higher levels correspond to higher accuracy (fewer false positives).
> Higher levels also correspond to more effort, similar to the way @option{-O}
> works.
> diff --git a/gcc/testsuite/c-c++-common/Wstrict-aliasing-with-fno.c
> b/gcc/testsuite/c-c++-common/Wstrict-aliasing-with-fno.c
> new file mode 100644
> index 00000000000..320ad937e4e
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/Wstrict-aliasing-with-fno.c
> @@ -0,0 +1,11 @@
> +/* Test the usage of option -Wstrict-aliasing. */
> +/* Make sure it's enabled even when -fno-strict-aliasing. */
> +/* { dg-do compile } */
> +/* { dg-options "-Wstrict-aliasing -fno-strict-aliasing" } */
> +
> +int main(int argc, char *argv[])
> +{
> + int x;
> + float *q = (float*) &x; /* {dg-warning "strict-aliasing"} */
> + return x;
> +}
> --
> 2.54.0
>