* cp-tree.h (cp_save_expr): New function.
* init.c (build_new): Correct logic for zero-element array
warning. Use cp_save_expr.
* tree.c (cp_save_expr): New function.
PR c++/27210
* g++.dg/warn/new1.C: New test.
* g++.dg/template/new5.C: Likewise.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@113958
138bc75d-0d04-0410-961f-
82ee72b054a4
+2006-05-21 Mark Mitchell <mark@codesourcery.com>
+
+ PR c++/27210
+ * cp-tree.h (cp_save_expr): New function.
+ * init.c (build_new): Correct logic for zero-element array
+ warning. Use cp_save_expr.
+ * tree.c (cp_save_expr): New function.
+
2006-05-21 Volker Reichelt <reichelt@igpm.rwth-aachen.de>
PR c++/27398
extern tree fold_if_not_in_template (tree);
extern tree rvalue (tree);
extern tree convert_bitfield_to_declared_type (tree);
+extern tree cp_save_expr (tree);
/* in typeck.c */
extern int string_conv_p (tree, tree, int);
{
if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
pedwarn ("size in array new must have integral type");
- nelts = save_expr (cp_convert (sizetype, nelts));
- if (nelts == integer_zero_node)
- warning (0, "zero size array reserves no space");
+ nelts = cp_save_expr (cp_convert (sizetype, nelts));
+ /* It is valid to allocate a zero-element array:
+
+ [expr.new]
+
+ When the value of the expression in a direct-new-declarator
+ is zero, the allocation function is called to allocate an
+ array with no elements. The pointer returned by the
+ new-expression is non-null. [Note: If the library allocation
+ function is called, the pointer returned is distinct from the
+ pointer to any other object.]
+
+ However, that is not generally useful, so we issue a
+ warning. */
+ if (integer_zerop (nelts))
+ warning (0, "allocating zero-element array");
}
/* ``A reference cannot be created by the new operator. A reference
&& nonstatic_local_decl_p (var));
}
+/* Like save_expr, but for C++. */
+
+tree
+cp_save_expr (tree expr)
+{
+ /* There is no reason to create a SAVE_EXPR within a template; if
+ needed, we can create the SAVE_EXPR when instantiating the
+ template. Furthermore, the middle-end cannot handle C++-specific
+ tree codes. */
+ if (processing_template_decl)
+ return expr;
+ return save_expr (expr);
+}
+
/* Initialize tree.c. */
void
+2006-05-21 Mark Mitchell <mark@codesourcery.com>
+
+ PR c++/27210
+ * g++.dg/warn/new1.C: New test.
+ * g++.dg/template/new5.C: Likewise.
+
2006-05-21 Roger Sayle <roger@eyesopen.com>
* gcc.target/i386/20060512-1.c: Add -msse2 command line option.
--- /dev/null
+// PR c++/27210
+
+template <class foo> class junk {
+ void bar(int a)
+ {
+ unsigned char *c = new unsigned char[a*sizeof(foo)];
+ }
+};
+
--- /dev/null
+void f() {
+ new int[0]; // { dg-warning "zero" }
+}