diff options
Diffstat (limited to 'gcc/cp/decl.c')
-rw-r--r-- | gcc/cp/decl.c | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 65ba812..4038197 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -9652,17 +9652,21 @@ static tree compute_array_index_type_loc (location_t name_loc, tree name, tree size, tsubst_flags_t complain) { - tree itype; - tree osize = size; - if (error_operand_p (size)) return error_mark_node; + /* The type of the index being computed. */ + tree itype; + + /* The original numeric size as seen in the source code before + conversion to size_t. */ + tree origsize = size; + location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location); if (!type_dependent_expression_p (size)) { - osize = size = mark_rvalue_use (size); + origsize = size = mark_rvalue_use (size); if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR && TREE_SIDE_EFFECTS (size)) @@ -9679,7 +9683,7 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size, /*manifestly_const_eval=*/true); if (!TREE_CONSTANT (size)) - size = osize; + size = origsize; } if (error_operand_p (size)) @@ -9740,16 +9744,30 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size, /* Normally, the array-bound will be a constant. */ if (TREE_CODE (size) == INTEGER_CST) { - /* An array must have a positive number of elements. */ - if (!valid_constant_size_p (size)) + /* The size to use in diagnostics that reflects the constant + size used in the source, rather than SIZE massaged above. */ + tree diagsize = size; + + /* If the original size before conversion to size_t was signed + and negative, convert it to ssizetype to restore the sign. */ + if (!TYPE_UNSIGNED (TREE_TYPE (origsize)) + && TREE_CODE (size) == INTEGER_CST + && tree_int_cst_sign_bit (size)) + { + diagsize = fold_convert (ssizetype, size); + + /* Clear the overflow bit that may have been set as a result + of the conversion from the sizetype of the new size to + ssizetype. */ + TREE_OVERFLOW (diagsize) = false; + } + + /* Verify that the array has a positive number of elements + and issue the appropriate diagnostic if it doesn't. */ + if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error))) { if (!(complain & tf_error)) return error_mark_node; - - if (name) - error_at (loc, "size of array %qD is negative", name); - else - error_at (loc, "size of array is negative"); size = integer_one_node; } /* As an extension we allow zero-sized arrays. */ |