@@ -868,19 +868,6 @@ c_get_substring_location (const substring_loc &substr_loc,
}
-/* Fold X for consideration by one of the warning functions when checking
- whether an expression has a constant value. */
-
-tree
-fold_for_warn (tree x)
-{
- if (c_dialect_cxx ())
- return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
- else
- /* The C front-end has already folded X appropriately. */
- return x;
-}
-
/* Return true iff T is a boolean promoted to int. */
bool
@@ -668,3 +668,13 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
}
return ret;
}
+
+/* Fold X for consideration by one of the warning functions when checking
+ whether an expression has a constant value. */
+
+tree
+fold_for_warn (tree x)
+{
+ /* The C front-end has already folded X appropriately. */
+ return x;
+}
@@ -315,3 +315,25 @@ mark_exp_read (tree exp)
}
}
+/* Fold X for consideration by one of the warning functions when checking
+ whether an expression has a constant value. */
+
+tree
+fold_for_warn (tree x)
+{
+ /* C++ implementation. */
+
+ /* It's not generally safe to fold inside of a template, so
+ merely strip any location wrapper and read through enum values. */
+ if (processing_template_decl)
+ {
+ STRIP_ANY_LOCATION_WRAPPER (x);
+
+ if (TREE_CODE (x) == CONST_DECL)
+ x = DECL_INITIAL (x);
+
+ return x;
+ }
+
+ return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
+}
new file mode 100644
@@ -0,0 +1,63 @@
+/* Verify that our memset warnings don't crash when folding
+ arguments within a template (PR c++/83814). */
+
+// { dg-options "-Wno-int-to-pointer-cast -Wmemset-transposed-args -Wmemset-elt-size" }
+
+template <class>
+void test_1()
+{
+ __builtin_memset (int() - char(), 0, 0);
+}
+
+template <class>
+void test_2()
+{
+ __builtin_memset (0, 0, int() - char());
+}
+
+template <class>
+void test_3 (unsigned a, int c)
+{
+ __builtin_memset((char *)c + a, 0, a);
+}
+
+template <class>
+void test_4 (unsigned a, int c)
+{
+ __builtin_memset(0, 0, (char *)c + a);
+}
+
+/* Verify that we warn for -Wmemset-transposed-args inside
+ a template. */
+
+char buf[1024];
+
+template <class>
+void test_5 ()
+{
+ __builtin_memset (buf, sizeof buf, 0); // { dg-warning "transposed parameters" }
+}
+
+/* Adapted from c-c++-common/memset-array.c; verify that
+ -Wmemset-elt-size works within a template. */
+
+enum a {
+ a_1,
+ a_2,
+ a_n
+};
+int t1[20];
+int t2[a_n];
+
+struct s
+{
+ int t[20];
+};
+
+template<class>
+void foo (struct s *s)
+{
+ __builtin_memset (t1, 0, 20); /* { dg-warning "element size" } */
+ __builtin_memset (t2, 0, a_n); /* { dg-warning "element size" } */
+ __builtin_memset (s->t, 0, 20); /* { dg-warning "element size" } */
+}