]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Fix style checking rule for square brackets in Ada 2022 and above
authorMarc Poulhiès <poulhies@adacore.com>
Fri, 28 Jan 2022 15:55:43 +0000 (16:55 +0100)
committerPierre-Marie de Rodat <derodat@adacore.com>
Thu, 12 May 2022 12:38:34 +0000 (12:38 +0000)
The square bracket syntax introduced in Ada 2022 was incorrectly handled
by the style checker and incorrect spacing was enforced. The issue was
in part caused by the wide character syntax (support removed starting
from Ada 2022) that treats the square bracket as a valid identifier
character.

gcc/ada/

* csets.adb (Initialize): Only treat square bracket as valid
identifier character for Ada versions prior to Ada 2022.
* style.ads (Check_Left_Paren): Rename...
(Check_Left_Paren_Square_Bracket): ...to this.
* styleg.adb (Check_Left_Bracket): Rename...
(Check_Left_Paren_Square_Bracket): ...to this.
* styleg.ads (Check_Left_Paren): Rename...
(Check_Left_Paren_Square_Bracket): ...to this.
* scng.adb (Scan): Add check for spacing around left square
bracket and use new name for Check_Left_Paren_Square_Bracket.
* libgnat/a-szmzco.ads (Control_Ranges, Graphic_Ranges)
(Letter_Ranges, Decimal_Digit_Ranges, ISO_646_Ranges)
(Character_Ranges): Fix style (remove extra space).
* libgnat/a-swmwco.ads (Control_Ranges, Graphic_Ranges)
(Letter_Ranges, Decimal_Digit_Ranges, ISO_646_Ranges)
(Character_Ranges): Likewise.
* opt.adb (Set_Config_Switches): Remove [ from Identifier_Char
set.

gcc/ada/csets.adb
gcc/ada/libgnat/a-swmwco.ads
gcc/ada/libgnat/a-szmzco.ads
gcc/ada/opt.adb
gcc/ada/scng.adb
gcc/ada/style.ads
gcc/ada/styleg.adb
gcc/ada/styleg.ads

index d36b815e5273b0284bce17b74e9e9ae52b3c4200..bd2ba928661798e5b206759b8c07265300b91f33 100644 (file)
@@ -1145,12 +1145,13 @@ package body Csets is
          Identifier_Char (J) := (Fold_Upper (J) /= ' ');
       end loop;
 
-      --  Always add [ as an identifier character to deal with the brackets
-      --  notation for wide characters used in identifiers. Note that if
-      --  we are not allowing wide characters in identifiers, then any use
-      --  of this notation will be flagged as an error in Scan_Identifier.
+      --  Add [ as an identifier character to deal with the brackets notation
+      --  for wide characters used in identifiers for versions up to Ada 2012.
+      --  Note that if we are not allowing wide characters in identifiers, then
+      --  any use of this notation will be flagged as an error in
+      --  Scan_Identifier.
 
-      Identifier_Char ('[') := True;
+      Identifier_Char ('[') := Ada_Version < Ada_2022;
 
       --  Add entry for ESC if wide characters in use with a wide character
       --  encoding method active that uses the ESC code for encoding.
index af11630226c6a947895aeb3c65d72bb7568d1fe7..ed377187f213acd7b577e00df550d4b370e0c4c3 100644 (file)
@@ -66,27 +66,27 @@ private
    subtype WC is Wide_Character;
 
    Control_Ranges           : aliased constant Wide_Character_Ranges :=
-     [ (W.NUL, W.US),
-       (W.DEL, W.APC)];
+     [(W.NUL, W.US),
+      (W.DEL, W.APC)];
 
    Control_Set              : constant Wide_Character_Set :=
      (AF.Controlled with
       Control_Ranges'Unrestricted_Access);
 
    Graphic_Ranges           : aliased constant Wide_Character_Ranges :=
-     [ (W.Space,      W.Tilde),
-       (WC'Val (256), WC'Last)];
+     [(W.Space,      W.Tilde),
+      (WC'Val (256), WC'Last)];
 
    Graphic_Set              : constant Wide_Character_Set :=
      (AF.Controlled with
       Graphic_Ranges'Unrestricted_Access);
 
    Letter_Ranges            : aliased constant Wide_Character_Ranges :=
-     [ ('A',                   'Z'),
-       (W.LC_A,                W.LC_Z),
-       (W.UC_A_Grave,          W.UC_O_Diaeresis),
-       (W.UC_O_Oblique_Stroke, W.LC_O_Diaeresis),
-       (W.LC_O_Oblique_Stroke, W.LC_Y_Diaeresis)];
+     [('A',                   'Z'),
+      (W.LC_A,                W.LC_Z),
+      (W.UC_A_Grave,          W.UC_O_Diaeresis),
+      (W.UC_O_Oblique_Stroke, W.LC_O_Diaeresis),
+      (W.LC_O_Oblique_Stroke, W.LC_Y_Diaeresis)];
 
    Letter_Set               : constant Wide_Character_Set :=
      (AF.Controlled with
@@ -126,7 +126,7 @@ private
       Basic_Ranges'Unrestricted_Access);
 
    Decimal_Digit_Ranges     : aliased constant Wide_Character_Ranges :=
-     [ ('0', '9')];
+     [('0', '9')];
 
    Decimal_Digit_Set        : constant Wide_Character_Set :=
      (AF.Controlled with
@@ -167,14 +167,14 @@ private
       Special_Graphic_Ranges'Unrestricted_Access);
 
    ISO_646_Ranges           : aliased constant Wide_Character_Ranges :=
-     [ (W.NUL, W.DEL)];
+     [(W.NUL, W.DEL)];
 
    ISO_646_Set              : constant Wide_Character_Set :=
      (AF.Controlled with
       ISO_646_Ranges'Unrestricted_Access);
 
    Character_Ranges         : aliased constant Wide_Character_Ranges :=
-     [ (W.NUL, WC'Val (255))];
+     [(W.NUL, WC'Val (255))];
 
    Character_Set            : constant Wide_Character_Set :=
      (AF.Controlled with
index 96d64b36058dd56727312593a8cbaa2ce3779613..e8de549382fe93d2ad6777e8b15160fba7680111 100644 (file)
@@ -66,27 +66,27 @@ private
    subtype WC is Wide_Wide_Character;
 
    Control_Ranges           : aliased constant Wide_Wide_Character_Ranges :=
-     [ (W.NUL, W.US),
-       (W.DEL, W.APC)];
+     [(W.NUL, W.US),
+      (W.DEL, W.APC)];
 
    Control_Set              : constant Wide_Wide_Character_Set :=
      (AF.Controlled with
       Control_Ranges'Unrestricted_Access);
 
    Graphic_Ranges           : aliased constant Wide_Wide_Character_Ranges :=
-     [ (W.Space,      W.Tilde),
-       (WC'Val (256), WC'Last)];
+     [(W.Space,      W.Tilde),
+      (WC'Val (256), WC'Last)];
 
    Graphic_Set              : constant Wide_Wide_Character_Set :=
      (AF.Controlled with
       Graphic_Ranges'Unrestricted_Access);
 
    Letter_Ranges            : aliased constant Wide_Wide_Character_Ranges :=
-     [ ('A',                  'Z'),
-       (W.LC_A,                W.LC_Z),
-       (W.UC_A_Grave,          W.UC_O_Diaeresis),
-       (W.UC_O_Oblique_Stroke, W.LC_O_Diaeresis),
-       (W.LC_O_Oblique_Stroke, W.LC_Y_Diaeresis)];
+     [('A',                  'Z'),
+      (W.LC_A,                W.LC_Z),
+      (W.UC_A_Grave,          W.UC_O_Diaeresis),
+      (W.UC_O_Oblique_Stroke, W.LC_O_Diaeresis),
+      (W.LC_O_Oblique_Stroke, W.LC_Y_Diaeresis)];
 
    Letter_Set               : constant Wide_Wide_Character_Set :=
      (AF.Controlled with
@@ -126,7 +126,7 @@ private
       Basic_Ranges'Unrestricted_Access);
 
    Decimal_Digit_Ranges     : aliased constant Wide_Wide_Character_Ranges :=
-     [ ('0', '9')];
+     [('0', '9')];
 
    Decimal_Digit_Set        : constant Wide_Wide_Character_Set :=
      (AF.Controlled with
@@ -167,14 +167,14 @@ private
       Special_Graphic_Ranges'Unrestricted_Access);
 
    ISO_646_Ranges           : aliased constant Wide_Wide_Character_Ranges :=
-     [ (W.NUL, W.DEL)];
+     [(W.NUL, W.DEL)];
 
    ISO_646_Set              : constant Wide_Wide_Character_Set :=
      (AF.Controlled with
       ISO_646_Ranges'Unrestricted_Access);
 
    Character_Ranges         : aliased constant Wide_Wide_Character_Ranges :=
-     [ (W.NUL, WC'Val (255))];
+     [(W.NUL, WC'Val (255))];
 
    Character_Set            : constant Wide_Wide_Character_Set :=
      (AF.Controlled with
index 49e03b01826f230166f51cc97bb11693d3e207be..ae6b844052efff77db1a223dcde1ee48c5fc30ae 100644 (file)
@@ -23,6 +23,8 @@
 --                                                                          --
 ------------------------------------------------------------------------------
 
+with Csets;          use Csets;
+
 package body Opt is
 
    -------------------------
@@ -210,6 +212,7 @@ package body Opt is
          Prefix_Exception_Messages   := True;
          Uneval_Old                  := 'E';
          Use_VADS_Size               := False;
+         Identifier_Char ('[')       := False;
 
          --  Note: we do not need to worry about Warnings_As_Errors_Count since
          --  we do not expect to get any warnings from compiling such a unit.
index c50a41e94dc3237451f5a777399ab39ae31330e5..cd10d1d4497aa17123f858ae7c29bd4a202cd442 100644 (file)
@@ -1421,7 +1421,7 @@ package body Scng is
             Token := Tok_Left_Paren;
 
             if Style_Check then
-               Style.Check_Left_Paren;
+               Style.Check_Left_Paren_Square_Bracket;
             end if;
 
             return;
@@ -1437,6 +1437,11 @@ package body Scng is
             if Ada_Version >= Ada_2022 then
                Scan_Ptr := Scan_Ptr + 1;
                Token := Tok_Left_Bracket;
+
+               if Style_Check then
+                  Style.Check_Left_Paren_Square_Bracket;
+               end if;
+
                return;
 
             elsif Source (Scan_Ptr + 1) = '"' then
index 814bb2bcb19d67bb1265ab349a5d5d552d7edc25..209dff851ee7487b11a93bb6db0c2bef8d767250 100644 (file)
@@ -147,9 +147,10 @@ package Style is
    --  is that the starting column is appropriate to the indentation rules if
    --  Token_Ptr is the first token on the line.
 
-   procedure Check_Left_Paren
-     renames Style_Inst.Check_Left_Paren;
-   --  Called after scanning out a left parenthesis to check spacing
+   procedure Check_Left_Paren_Square_Bracket
+     renames Style_Inst.Check_Left_Paren_Square_Bracket;
+   --  Called after scanning out a left parenthesis to check spacing. If
+   --  Ada_Version >= Ada_2022 then called similarly for a left square bracket.
 
    procedure Check_Line_Terminator (Len : Int)
      renames Style_Inst.Check_Line_Terminator;
index 205bad03d7cbf87996f69f610efac443c04ce9cd..6a785b5835aadbd566ac985d5560ff9be25c35f2 100644 (file)
@@ -721,15 +721,16 @@ package body Styleg is
       end if;
    end Check_Indentation;
 
-   ----------------------
-   -- Check_Left_Paren --
-   ----------------------
+   -------------------------------------
+   -- Check_Left_Paren_Square_Bracket --
+   -------------------------------------
 
    --  In check token mode (-gnatyt), left paren must not be preceded by an
    --  identifier character or digit (a separating space is required) and may
    --  never be followed by a space.
+   --  Same applies for the left square bracket starting from Ada version 2022.
 
-   procedure Check_Left_Paren is
+   procedure Check_Left_Paren_Square_Bracket is
    begin
       if Style_Check_Tokens then
          if Token_Ptr > Source_First (Current_Source_File)
@@ -740,7 +741,7 @@ package body Styleg is
 
          Check_No_Space_After;
       end if;
-   end Check_Left_Paren;
+   end Check_Left_Paren_Square_Bracket;
 
    ---------------------------
    -- Check_Line_Max_Length --
index 116d6ef654ebfa7a0e33299f50cfce428216496d..23a1ee4b86ff3f1a5e8150d766a45ab9bac64f77 100644 (file)
@@ -111,8 +111,9 @@ package Styleg is
    --  is that the starting column is appropriate to the indentation rules if
    --  Token_Ptr is the first token on the line.
 
-   procedure Check_Left_Paren;
-   --  Called after scanning out a left parenthesis to check spacing
+   procedure Check_Left_Paren_Square_Bracket;
+   --  Called after scanning out a left parenthesis to check spacing. If
+   --  Ada_Version >= Ada_2022 then called similarly for a left square bracket.
 
    procedure Check_Line_Max_Length (Len : Nat);
    --  Called with Scan_Ptr pointing to the first line terminator character