diff mbox

[Ada] Handle Soft_Hyphen as non-graphic in Ada 2005 mode

Message ID 20101008123418.GA1612@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Oct. 8, 2010, 12:34 p.m. UTC
This patch implements proper handling of Soft_Hyphen (16#A5#) in
Ada 2005 where it is considered a non-graphic and correspons to
the special name SOFT_HYPHEN.

The following test, compiled with -gnat05, prints SOFT_HYPHEN 6 times.

with Ada.Text_IO; use  Ada.Text_IO;
procedure SoftHyphen is
begin
   Put_Line (Character'Image
              (Character'Val (16#00AD#)));
   Put_Line (Wide_Character'Image
              (Wide_Character'Val (16#00AD#)));
   Put_Line (Wide_Wide_Character'Image
              (Wide_Wide_Character'Val (16#00AD#)));
   Put_Line (Character'Image
              (Character'Value ("SOFT_HYPHEN")));
   Put_Line (Wide_Character'Image
              (Wide_Character'Value ("SOFT_HYPHEN")));
   Put_Line (Wide_Wide_Character'Image
              (Wide_Wide_Character'Value ("SOFT_HYPHEN")));
end;

This modified version of the test compiled with -gnat95 prints
out '?' four times, where ? is the soft hyphen character 16#A5#

with Ada.Text_IO; use  Ada.Text_IO;
procedure SoftHyphen2 is
begin
   Put_Line (Character'Image
              (Character'Val (16#00AD#)));
   Put_Line (Wide_Character'Image
              (Wide_Character'Val (16#00AD#)));
   Put_Line (Character'Image
              (Character'Value ("SOFT_HYPHEN")));
   Put_Line (Wide_Character'Image
              (Wide_Character'Value ("SOFT_HYPHEN")));
end;

Note that we recognize SOFT_HYPHEN for Value input even
in Ada 95 mode, which is permitted and seems reasonable!

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

2010-10-08  Robert Dewar  <dewar@adacore.com>

	* exp_imgv.adb (Expand_Image_Attribute): Handle special calling
	sequence for soft hyphen for Character'Image case.
	* rtsfind.ads (Image_Character_05): New entry
	* s-imgcha.adb (Image_Character_05): New procedurew
	* s-imgcha.ads (Image_Character_05): New procedure
	* s-imgwch.adb (Image_Wide_Character): Deal with Ada 2005 soft hyphen
	case.
	* s-valcha.adb (Value_Character): Recognize SOFT_HYPHEN for 16#AD#
	* sem_attr.adb (Eval_Attribute, case Width): Handle soft_hyphen name
	properly.
diff mbox

Patch

Index: s-valcha.adb
===================================================================
--- s-valcha.adb	(revision 165080)
+++ s-valcha.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -65,6 +65,10 @@  package body System.Val_Char is
             end if;
          end loop;
 
+         if S (F .. L) = "SOFT_HYPHEN" then
+            return Character'Val (16#AD#);
+         end if;
+
          raise Constraint_Error;
       end if;
    end Value_Character;
Index: exp_imgv.adb
===================================================================
--- exp_imgv.adb	(revision 165080)
+++ exp_imgv.adb	(working copy)
@@ -306,8 +306,16 @@  package body Exp_Imgv is
          Imid := RE_Image_Boolean;
          Tent := Rtyp;
 
+      --  For standard character, we have to select the version which handles
+      --  soft hyphen correctly, based on the version of Ada in use (ugly!)
+
       elsif Rtyp = Standard_Character then
-         Imid := RE_Image_Character;
+         if Ada_Version < Ada_05 then
+            Imid := RE_Image_Character;
+         else
+            Imid := RE_Image_Character_05;
+         end if;
+
          Tent := Rtyp;
 
       elsif Rtyp = Standard_Wide_Character then
Index: rtsfind.ads
===================================================================
--- rtsfind.ads	(revision 165092)
+++ rtsfind.ads	(working copy)
@@ -800,6 +800,7 @@  package Rtsfind is
      RE_Image_Boolean,                   -- System.Img_Bool
 
      RE_Image_Character,                 -- System.Img_Char
+     RE_Image_Character_05,              -- System.Img_Char
 
      RE_Image_Decimal,                   -- System.Img_Dec
 
@@ -1972,6 +1973,7 @@  package Rtsfind is
      RE_Image_Boolean                    => System_Img_Bool,
 
      RE_Image_Character                  => System_Img_Char,
+     RE_Image_Character_05               => System_Img_Char,
 
      RE_Image_Decimal                    => System_Img_Dec,
 
Index: s-imgwch.adb
===================================================================
--- s-imgwch.adb	(revision 165080)
+++ s-imgwch.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -61,6 +61,16 @@  package body System.Img_WChar is
 
          P := 4;
 
+      --  Deal with annoying Ada 95 incompatibility with soft hyphen
+
+      elsif V = Wide_Character'Val (16#00AD#)
+        and then not Ada_2005
+      then
+         P := 3;
+         S (1) := ''';
+         S (2) := Character'Val (16#00AD#);
+         S (3) := ''';
+
       --  Normal case, same as Wide_Wide_Character
 
       else
@@ -83,10 +93,14 @@  package body System.Img_WChar is
       Val : Unsigned_32 := Wide_Wide_Character'Pos (V);
 
    begin
-      --  If in range of standard Character, use Character routine
+      --  If in range of standard Character, use Character routine. Use the
+      --  Ada 2005 version, since either we are called directly in Ada 2005
+      --  mode for Wide_Wide_Character, or this is the Wide_Character case
+      --  which already took care of the Soft_Hyphen glitch.
 
       if Val <= 16#FF# then
-         Image_Character (Character'Val (Wide_Wide_Character'Pos (V)), S, P);
+         Image_Character_05
+           (Character'Val (Wide_Wide_Character'Pos (V)), S, P);
 
       --  Otherwise value returned is Hex_hhhhhhhh
 
Index: s-imgcha.adb
===================================================================
--- s-imgcha.adb	(revision 165080)
+++ s-imgcha.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -158,4 +158,23 @@  package body System.Img_Char is
       end if;
    end Image_Character;
 
+   ------------------------
+   -- Image_Character_05 --
+   ------------------------
+
+   procedure Image_Character_05
+     (V : Character;
+      S : in out String;
+      P : out Natural)
+   is
+      pragma Assert (S'First = 1);
+   begin
+      if V = Character'Val (16#00AD#) then
+         P := 11;
+         S (1 .. P) := "SOFT_HYPHEN";
+      else
+         Image_Character (V, S, P);
+      end if;
+   end Image_Character_05;
+
 end System.Img_Char;
Index: s-imgcha.ads
===================================================================
--- s-imgcha.ads	(revision 165080)
+++ s-imgcha.ads	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -42,4 +42,14 @@  package System.Img_Char is
    --  setting the resulting value of P. The caller guarantees that S is
    --  long enough to hold the result, and that S'First is 1.
 
+   procedure Image_Character_05
+     (V : Character;
+      S : in out String;
+      P : out Natural);
+   --  Computes Character'Image (V) and stores the result in S (1 .. P)
+   --  setting the resulting value of P. The caller guarantees that S is
+   --  long enough to hold the result, and that S'First is 1. This version
+   --  is for use in Ada 2005 and beyond, where soft hyphen is a non-graphic
+   --  and results in "SOFT_HYPHEN" as the output.
+
 end System.Img_Char;
Index: sem_attr.adb
===================================================================
--- sem_attr.adb	(revision 165166)
+++ sem_attr.adb	(working copy)
@@ -7413,7 +7413,6 @@  package body Sem_Attr is
 
                            --  No need to compute this more than once!
 
-                           W := Int'Max (W, 12);
                            exit;
 
                         else
@@ -7427,13 +7426,11 @@  package body Sem_Attr is
                            case C is
                               when Reserved_128 | Reserved_129 |
                                    Reserved_132 | Reserved_153
-
                                 => Wt := 12;
 
                               when BS | HT | LF | VT | FF | CR |
                                    SO | SI | EM | FS | GS | RS |
                                    US | RI | MW | ST | PM
-
                                 => Wt := 2;
 
                               when NUL | SOH | STX | ETX | EOT |
@@ -7445,13 +7442,20 @@  package body Sem_Attr is
                                    SS2 | SS3 | DCS | PU1 | PU2 |
                                    STS | CCH | SPA | EPA | SOS |
                                    SCI | CSI | OSC | APC
-
                                 => Wt := 3;
 
                               when Space .. Tilde |
                                    No_Break_Space .. LC_Y_Diaeresis
+                                =>
+                                 --  Special case of soft hyphen in Ada 2005
 
-                                => Wt := 3;
+                                 if C = Character'Val (16#AD#)
+                                   and then Ada_Version >= Ada_05
+                                 then
+                                    Wt := 11;
+                                 else
+                                    Wt := 3;
+                                 end if;
                            end case;
 
                            W := Int'Max (W, Wt);