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.

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

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.
diff --git a/gcc/ada/csets.adb b/gcc/ada/csets.adb
--- a/gcc/ada/csets.adb
+++ b/gcc/ada/csets.adb
@@ -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.


diff --git a/gcc/ada/libgnat/a-swmwco.ads b/gcc/ada/libgnat/a-swmwco.ads
--- a/gcc/ada/libgnat/a-swmwco.ads
+++ b/gcc/ada/libgnat/a-swmwco.ads
@@ -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


diff --git a/gcc/ada/libgnat/a-szmzco.ads b/gcc/ada/libgnat/a-szmzco.ads
--- a/gcc/ada/libgnat/a-szmzco.ads
+++ b/gcc/ada/libgnat/a-szmzco.ads
@@ -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


diff --git a/gcc/ada/opt.adb b/gcc/ada/opt.adb
--- a/gcc/ada/opt.adb
+++ b/gcc/ada/opt.adb
@@ -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.


diff --git a/gcc/ada/scng.adb b/gcc/ada/scng.adb
--- a/gcc/ada/scng.adb
+++ b/gcc/ada/scng.adb
@@ -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


diff --git a/gcc/ada/style.ads b/gcc/ada/style.ads
--- a/gcc/ada/style.ads
+++ b/gcc/ada/style.ads
@@ -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;


diff --git a/gcc/ada/styleg.adb b/gcc/ada/styleg.adb
--- a/gcc/ada/styleg.adb
+++ b/gcc/ada/styleg.adb
@@ -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 --


diff --git a/gcc/ada/styleg.ads b/gcc/ada/styleg.ads
--- a/gcc/ada/styleg.ads
+++ b/gcc/ada/styleg.ads
@@ -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


Reply via email to