From: Piotr Trojanek <troja...@adacore.com>

Previously the lower bound of string literals indexed by non-static
integer types was artificially set to 1 in the frontend. This was to
avoid an overflow in calculation of a null string size by the GCC
backend, which was causing an excessively large binary object file.

However, setting the lower bound to 1 was problematic for GNATprove,
which could not easily retrieve the lower bound of string literals.

This patch avoids the overflow in GCC by recognizing null string literal
subtypes in Gigi.

gcc/ada/ChangeLog:

        * gcc-interface/decl.cc (gnat_to_gnu_entity): Recognize null
        string literal subtypes and set their bounds to 1 .. 0.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/gcc-interface/decl.cc | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc
index f5188ddc8bc..32e476c6993 100644
--- a/gcc/ada/gcc-interface/decl.cc
+++ b/gcc/ada/gcc-interface/decl.cc
@@ -3110,14 +3110,24 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree 
gnu_expr, bool definition)
        tree gnu_string_index_type
          = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
                                      (TYPE_DOMAIN (gnu_string_array_type))));
+
+        /* For a null string literal we set the bounds to 1 .. 0, to
+           avoid a possible overflow when calculating the upper bound
+           as LOWER_BOUND + LENGTH - 1.  */
+        const bool is_null_string
+          = String_Literal_Length (gnat_entity) == Uint_0;
        tree gnu_lower_bound
-         = convert (gnu_string_index_type,
+         = is_null_string ?
+           build_int_cst (gnu_string_index_type, 1) :
+           convert (gnu_string_index_type,
                     gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
        tree gnu_length
          = UI_To_gnu (String_Literal_Length (gnat_entity),
                       gnu_string_index_type);
        tree gnu_upper_bound
-         = build_binary_op (PLUS_EXPR, gnu_string_index_type,
+         = is_null_string ?
+           build_int_cst (gnu_string_index_type, 0) :
+           build_binary_op (PLUS_EXPR, gnu_string_index_type,
                             gnu_lower_bound,
                             int_const_binop (MINUS_EXPR, gnu_length,
                                              convert (gnu_string_index_type,
-- 
2.43.0

Reply via email to