diff mbox

[Ada] Small enhancement to unchecked conversion warning in -gnatf mode

Message ID 20150522124522.GA4926@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet May 22, 2015, 12:45 p.m. UTC
In full warning mode, when an unchecked conversion is applied to types of
different sizes, the compiler issues a warning describing the effects of
the conversion on the additional or missing bits.

When the source is smaller than the target, it was issuing a specific warning
if the target is of a discrete type but, when the source is larger than the
target, it wasn't doing the same if the source is of a discrete type.

The compiler must issue the following warnings with -gnatf:

ucw.ads:12:03: warning: types for unchecked conversion have different sizes
ucw.ads:12:03: warning: size of "REC" is 16, size of "INTEGER" is 32
ucw.ads:12:03: warning: target value will include 16 undefined high order bits
ucw.ads:13:03: warning: types for unchecked conversion have different sizes
ucw.ads:13:03: warning: size of "INTEGER" is 32, size of "REC" is 16
ucw.ads:13:03: warning: 16 high order bits of source will be ignored

on

with Unchecked_Conversion;

package UCW is

  type Int8_t is mod 2**8;

  type Rec is record
    I1 : Int8_t;
    I2 : Int8_t;
  end record;

  function C1 is new Unchecked_Conversion (Source => Rec, Target => Integer);
  function C2 is new Unchecked_Conversion (Source => Integer, Target => Rec);

end UCW;

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

2015-05-22  Eric Botcazou  <ebotcazou@adacore.com>

	* sem_ch13.adb (Validate_Unchecked_Conversions): Also issue
	specific warning for discrete types when the source is larger
	than the target.
diff mbox

Patch

Index: sem_ch13.adb
===================================================================
--- sem_ch13.adb	(revision 223534)
+++ sem_ch13.adb	(working copy)
@@ -13483,9 +13483,22 @@ 
                         end if;
 
                      else pragma Assert (Source_Siz > Target_Siz);
-                        Error_Msg
-                          ("\?z?^ trailing bits of source will be ignored!",
-                           Eloc);
+                        if Is_Discrete_Type (Source) then
+                           if Bytes_Big_Endian then
+                              Error_Msg
+                                ("\?z?^ low order bits of source will be "
+                                 & "ignored!", Eloc);
+                           else
+                              Error_Msg
+                                ("\?z?^ high order bits of source will be "
+                                 & "ignored!", Eloc);
+                           end if;
+
+                        else
+                           Error_Msg
+                             ("\?z?^ trailing bits of source will be "
+                              & "ignored!", Eloc);
+                        end if;
                      end if;
                   end if;
                end if;