diff mbox series

[Ada] Fix handling of 'Image acting as a prefix of a slice in CodePeer

Message ID 20211004084748.GA1536652@adacore.com
State New
Headers show
Series [Ada] Fix handling of 'Image acting as a prefix of a slice in CodePeer | expand

Commit Message

Pierre-Marie de Rodat Oct. 4, 2021, 8:47 a.m. UTC
In CodePeer mode attribute Image is not expanded and has its Etype set
as the unconstrained String type. When this attribute appears as a
prefix of an indexed component, we get a check; when it appears as a
prefix of a slice, we don't get a check.

For indexed components, the check effectively comes from
Apply_Scalar_Range_Check, where prefixes of unconstrained array types
are handled specifically. For slices, the seemingly similar routine
Apply_Range_Check doesn't specifically handle such prefixes. Instead, we
need to give this routine a constrained subtype, just like we do when
slice is prefixed with a call to a function that returns an
unconstrained array.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* sem_res.adb (Resolve_Slice): Add custom handling of attribute
	Image and similar in CodePeer mode. This complements the
	existing custom handling of these attributes in
	Expand_N_Attribute_Reference.
diff mbox series

Patch

diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb
--- a/gcc/ada/sem_res.adb
+++ b/gcc/ada/sem_res.adb
@@ -11275,10 +11275,20 @@  package body Sem_Res is
             end;
          end if;
 
+      --  In CodePeer mode the attribute Image is not expanded, so when it
+      --  acts as a prefix of a slice, we handle it like a call to function
+      --  returning an unconstrained string. Same for the Wide variants of
+      --  attribute Image.
+
       elsif Is_Entity_Name (Name)
         or else Nkind (Name) = N_Explicit_Dereference
         or else (Nkind (Name) = N_Function_Call
                   and then not Is_Constrained (Etype (Name)))
+        or else (CodePeer_Mode
+                  and then Nkind (Name) = N_Attribute_Reference
+                  and then Attribute_Name (Name) in Name_Image
+                                                  | Name_Wide_Image
+                                                  | Name_Wide_Wide_Image)
       then
          Array_Type := Get_Actual_Subtype (Name);