aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2020-05-06 23:38:13 +0200
committerJakub Jelinek <jakub@redhat.com>2020-05-06 23:38:13 +0200
commit46fcef99f49cc2d9f28d98f8ecdbf8263e9e0a87 (patch)
tree5291205eec644bb2bc340fe83f5e35273901eeb2 /gcc
parent25ee2155ead87a5ea1c152a29341ee1e3275d706 (diff)
downloadgcc-46fcef99f49cc2d9f28d98f8ecdbf8263e9e0a87.zip
gcc-46fcef99f49cc2d9f28d98f8ecdbf8263e9e0a87.tar.gz
gcc-46fcef99f49cc2d9f28d98f8ecdbf8263e9e0a87.tar.bz2
c++: Avoid strict_aliasing_warning on dependent types or expressions [PR94951]
The following testcase gets a bogus warning during build_base_path, when cp_build_indirect_ref* calls strict_aliasing_warning with a dependent expression. IMHO calling get_alias_set etc. on dependent types feels wrong to me, we should just defer the warnings in those cases until instantiation and only handle the cases where neither type nor expr are dependent. 2020-05-06 Jakub Jelinek <jakub@redhat.com> PR c++/94951 * typeck.c (cp_strict_aliasing_warning): New function. (cp_build_indirect_ref_1, build_reinterpret_cast_1): Use it instead of strict_aliasing_warning. * g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/ChangeLog5
-rw-r--r--gcc/cp/typeck.c28
-rw-r--r--gcc/testsuite/ChangeLog3
-rw-r--r--gcc/testsuite/g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C12
4 files changed, 42 insertions, 6 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 32bb9f5..a3bec1c 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,5 +1,10 @@
2020-05-06 Jakub Jelinek <jakub@redhat.com>
+ PR c++/94951
+ * typeck.c (cp_strict_aliasing_warning): New function.
+ (cp_build_indirect_ref_1, build_reinterpret_cast_1): Use
+ it instead of strict_aliasing_warning.
+
PR c++/94907
* method.c (defaulted_late_check): Don't call synthesize_method
on constexpr sfk_comparison if it has been called on it already.
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 8e3188a..13d9602 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3318,6 +3318,22 @@ build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
return rval;
}
+/* Like c-family strict_aliasing_warning, but don't warn for dependent
+ types or expressions. */
+
+static bool
+cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
+{
+ if (processing_template_decl)
+ {
+ tree e = expr;
+ STRIP_NOPS (e);
+ if (dependent_type_p (type) || type_dependent_expression_p (e))
+ return false;
+ }
+ return strict_aliasing_warning (loc, type, expr);
+}
+
/* The implementation of the above, and of indirection implied by other
constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
@@ -3360,10 +3376,10 @@ cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
/* If a warning is issued, mark it to avoid duplicates from
the backend. This only needs to be done at
warn_strict_aliasing > 2. */
- if (warn_strict_aliasing > 2)
- if (strict_aliasing_warning (EXPR_LOCATION (ptr),
- type, TREE_OPERAND (ptr, 0)))
- TREE_NO_WARNING (ptr) = 1;
+ if (warn_strict_aliasing > 2
+ && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
+ type, TREE_OPERAND (ptr, 0)))
+ TREE_NO_WARNING (ptr) = 1;
}
if (VOID_TYPE_P (t))
@@ -7777,7 +7793,7 @@ build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
expr = cp_build_addr_expr (expr, complain);
if (warn_strict_aliasing > 2)
- strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
+ cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
if (expr != error_mark_node)
expr = build_reinterpret_cast_1
@@ -7891,7 +7907,7 @@ build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
if (warn_strict_aliasing <= 2)
/* strict_aliasing_warning STRIP_NOPs its expr. */
- strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
+ cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
return build_nop_reinterpret (type, expr);
}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index a2c0e85..a080b9a 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,5 +1,8 @@
2020-05-06 Jakub Jelinek <jakub@redhat.com>
+ PR c++/94951
+ * g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C: New test.
+
PR c++/94907
* g++.dg/cpp2a/spaceship-synth8.C: New test.
diff --git a/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C b/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C
new file mode 100644
index 0000000..d037504
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wstrict-aliasing-bogus-tmpl.C
@@ -0,0 +1,12 @@
+// PR c++/94951
+// { dg-do compile }
+// { dg-options "-O2 -Wall" }
+
+struct A { int a; };
+template <int N>
+struct B : public A
+{
+ static B<N> foo () { B<N> t; t.a = 4; return t; } // { dg-bogus "dereferencing type-punned pointer will break strict-aliasing rules" }
+};
+
+B<0> b = B<0>::foo ();