[Ada] Clean up scanner

Message ID 20220712122508.GA3404573@adacore.com
State Committed
Headers
Series [Ada] Clean up scanner |

Commit Message

Pierre-Marie de Rodat July 12, 2022, 12:25 p.m. UTC
  This patch removes some obsolete code in the scanner and related files,
and corrects some comments. Tok_Special is used only by the
preprocessor, and uses only the two characters '#' and '$'.

It might be simpler to have a single flag indicating we're scanning for
preprocessing, instead of the Special_Characters array and the
End_Of_Line_Is_Token flag, but that's for another day.

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

gcc/ada/

	* scans.ads: Fix obsolete comments about Tok_Special, and give
	Special_Character a predicate assuring it is one of the two
	characters used in preprocessing.
	* scng.ads: Clean up comments.
	* scng.adb: Clean up handling of Tok_Special.  Remove comment
	about '@' (target_name), which doesn't seem very helpful.
	Set_Special_Character will now blow up if given anything other
	than '#' and '$', because of the predicate on Special_Character;
	it's not clear why it used to say "when others => null;".
	Remove Comment_Is_Token, which is not used.
	* scn.ads: Remove commented-out use clause.  Remove redundant
	comment.
	* ali-util.adb: Use "is null" for do-nothing procedures.
	* gprep.adb (Post_Scan): Use "is null".
  

Patch

diff --git a/gcc/ada/ali-util.adb b/gcc/ada/ali-util.adb
--- a/gcc/ada/ali-util.adb
+++ b/gcc/ada/ali-util.adb
@@ -42,15 +42,12 @@  package body ALI.Util is
    --  empty, because we don't want to report any errors when computing
    --  a source checksum.
 
-   procedure Post_Scan;
+   procedure Post_Scan is null;
 
-   procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr);
-
-   procedure Error_Msg_S (Msg : String);
-
-   procedure Error_Msg_SC (Msg : String);
-
-   procedure Error_Msg_SP (Msg : String);
+   procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is null;
+   procedure Error_Msg_S (Msg : String) is null;
+   procedure Error_Msg_SC (Msg : String) is null;
+   procedure Error_Msg_SP (Msg : String) is null;
 
    --  Instantiation of Styleg, needed to instantiate Scng
 
@@ -85,47 +82,6 @@  package body ALI.Util is
       return Checksum1 = Checksum2 and then Checksum1 /= Checksum_Error;
    end Checksums_Match;
 
-   ---------------
-   -- Error_Msg --
-   ---------------
-
-   procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is
-      pragma Warnings (Off, Msg);
-      pragma Warnings (Off, Flag_Location);
-   begin
-      null;
-   end Error_Msg;
-
-   -----------------
-   -- Error_Msg_S --
-   -----------------
-
-   procedure Error_Msg_S (Msg : String) is
-      pragma Warnings (Off, Msg);
-   begin
-      null;
-   end Error_Msg_S;
-
-   ------------------
-   -- Error_Msg_SC --
-   ------------------
-
-   procedure Error_Msg_SC (Msg : String) is
-      pragma Warnings (Off, Msg);
-   begin
-      null;
-   end Error_Msg_SC;
-
-   ------------------
-   -- Error_Msg_SP --
-   ------------------
-
-   procedure Error_Msg_SP (Msg : String) is
-      pragma Warnings (Off, Msg);
-   begin
-      null;
-   end Error_Msg_SP;
-
    -----------------------
    -- Get_File_Checksum --
    -----------------------
@@ -192,15 +148,6 @@  package body ALI.Util is
       Interfaces.Reset;
    end Initialize_ALI_Source;
 
-   ---------------
-   -- Post_Scan --
-   ---------------
-
-   procedure Post_Scan is
-   begin
-      null;
-   end Post_Scan;
-
    ----------------------
    -- Read_Withed_ALIs --
    ----------------------


diff --git a/gcc/ada/gprep.adb b/gcc/ada/gprep.adb
--- a/gcc/ada/gprep.adb
+++ b/gcc/ada/gprep.adb
@@ -93,8 +93,8 @@  package body GPrep is
    procedure Display_Copyright;
    --  Display the copyright notice
 
-   procedure Post_Scan;
-   --  Null procedure, needed by instantiation of Scng below
+   procedure Post_Scan is null;
+   --  Needed by instantiation of Scng below
 
    package Scanner is new Scng
      (Post_Scan,
@@ -327,15 +327,6 @@  package body GPrep is
       New_Line (Outfile.all);
    end New_EOL_To_Outfile;
 
-   ---------------
-   -- Post_Scan --
-   ---------------
-
-   procedure Post_Scan is
-   begin
-      null;
-   end Post_Scan;
-
    ----------------------------
    -- Preprocess_Infile_Name --
    ----------------------------


diff --git a/gcc/ada/scans.ads b/gcc/ada/scans.ads
--- a/gcc/ada/scans.ads
+++ b/gcc/ada/scans.ads
@@ -210,15 +210,11 @@  package Scans is
 
       Tok_End_Of_Line,
       --  Represents an end of line. Not used during normal compilation scans
-      --  where end of line is ignored. Active for preprocessor scanning and
-      --  also when scanning project files (where it is needed because of ???)
+      --  where end of line is ignored. Active for preprocessor scanning.
 
       Tok_Special,
-      --  AI12-0125-03 : target name as abbreviation for LHS
-
-      --  Otherwise used only in preprocessor scanning (to represent one of
-      --  the characters '#', '$', '?', '@', '`', '\', '^', '~', or '_'. The
-      --  character value itself is stored in Scans.Special_Character.
+      --  Special character used by the preprocessor. The character itself is
+      --  stored in Special_Character below.
 
       No_Token);
       --  No_Token is used for initializing Token values to indicate that
@@ -466,12 +462,9 @@  package Scans is
    --  character found (i.e. a character that does not fit in Character or
    --  Wide_Character).
 
-   Special_Character : Character;
-   --  AI12-0125-03 : '@' as target name is handled elsewhere.
-   --  Valid only when Token = Tok_Special. Returns one of the characters
-   --  '#', '$', '?', '`', '\', '^', '~', or '_'.
-   --
-   --  Why only this set? What about wide characters???
+   subtype Special_Preprocessor_Character is Character with
+     Predicate => Special_Preprocessor_Character in '#' | '$';
+   Special_Character : Special_Preprocessor_Character;
 
    Comment_Id : Name_Id := No_Name;
    --  Valid only when Token = Tok_Comment. Store the string that follows


diff --git a/gcc/ada/scn.ads b/gcc/ada/scn.ads
--- a/gcc/ada/scn.ads
+++ b/gcc/ada/scn.ads
@@ -29,7 +29,7 @@ 
 with Casing; use Casing;
 with Errout; use Errout;
 with Scng;
-with Style;  --  use Style;
+with Style;
 with Types;  use Types;
 
 package Scn is
@@ -75,9 +75,5 @@  package Scn is
       Style        => Style.Style_Inst);
 
    procedure Scan renames Scanner.Scan;
-   --  Scan scans out the next token, and advances the scan state accordingly
-   --  (see package Scans for details). If the scan encounters an illegal
-   --  token, then an error message is issued pointing to the bad character,
-   --  and Scan returns a reasonable substitute token of some kind.
 
 end Scn;


diff --git a/gcc/ada/scng.adb b/gcc/ada/scng.adb
--- a/gcc/ada/scng.adb
+++ b/gcc/ada/scng.adb
@@ -29,7 +29,6 @@  with Errout;   use Errout;
 with Hostparm; use Hostparm;
 with Namet;    use Namet;
 with Opt;      use Opt;
-with Scans;    use Scans;
 with Sinput;   use Sinput;
 with Snames;   use Snames;
 with Stringt;  use Stringt;
@@ -53,9 +52,6 @@  package body Scng is
    Special_Characters : array (Character) of Boolean := (others => False);
    --  For characters that are Special token, the value is True
 
-   Comment_Is_Token : Boolean := False;
-   --  True if comments are tokens
-
    End_Of_Line_Is_Token : Boolean := False;
    --  True if End_Of_Line is a token
 
@@ -259,9 +255,6 @@  package body Scng is
 
    procedure Scan is
 
-      Start_Of_Comment : Source_Ptr;
-      --  Record start of comment position
-
       Underline_Found : Boolean;
       --  During scanning of an identifier, set to True if last character
       --  scanned was an underline or other punctuation character. This
@@ -1609,10 +1602,6 @@  package body Scng is
                   return;
                end if;
 
-               --  Otherwise scan out the comment
-
-               Start_Of_Comment := Scan_Ptr;
-
                --  Loop to scan comment (this loop runs more than once only if
                --  a horizontal tab or other non-graphic character is scanned)
 
@@ -1711,18 +1700,8 @@  package body Scng is
                   end if;
                end loop;
 
-               --  Note that, except when comments are tokens, we do NOT
-               --  execute a return here, instead we fall through to reexecute
-               --  the scan loop to look for a token.
-
-               if Comment_Is_Token then
-                  Name_Len := Integer (Scan_Ptr - Start_Of_Comment);
-                  Name_Buffer (1 .. Name_Len) :=
-                    String (Source (Start_Of_Comment .. Scan_Ptr - 1));
-                  Comment_Id := Name_Find;
-                  Token := Tok_Comment;
-                  return;
-               end if;
+               --  Note that we do not return here; instead we fall through to
+               --  reexecute the scan loop to look for a token.
             end if;
          end Minus_Case;
 
@@ -2072,14 +2051,6 @@  package body Scng is
          --  Underline character
 
          when '_' =>
-            if Special_Characters ('_') then
-               Token_Ptr := Scan_Ptr;
-               Scan_Ptr := Scan_Ptr + 1;
-               Token := Tok_Special;
-               Special_Character := '_';
-               return;
-            end if;
-
             Error_Msg_S ("identifier cannot start with underline");
             Name_Len := 1;
             Name_Buffer (1) := '_';
@@ -2132,42 +2103,19 @@  package body Scng is
                Error_Illegal_Character;
             end if;
 
-         --  Invalid control characters
-
-         when ACK
-            | ASCII.SO
-            | BEL
-            | BS
-            | CAN
-            | DC1
-            | DC2
-            | DC3
-            | DC4
-            | DEL
-            | DLE
-            | EM
-            | ENQ
-            | EOT
-            | ETB
-            | ETX
-            | FS
-            | GS
-            | NAK
-            | NUL
-            | RS
-            | SI
-            | SOH
-            | STX
-            | SYN
-            | US
+         --  Illegal characters
+
+         when ACK | ASCII.SO | BEL | BS | CAN | DC1 | DC2 | DC3 | DC4 | DEL
+           | DLE | EM | ENQ | EOT | ETB | ETX | FS | GS | NAK | NUL | RS | SI
+           | SOH | STX | SYN | US
+           | '?' | '`' | '\' | '^' | '~'
          =>
             Error_Illegal_Character;
 
-         --  Invalid graphic characters
-         --  Note that '@' is handled elsewhere, because following AI12-125
-         --  it denotes the target_name of an assignment.
+         --  Special preprocessor characters. If Set_Special_Character has been
+         --  called, return a Special token. Otherwise give an error.
 
-         when '#' | '$' | '?' | '`' | '\' | '^' | '~' =>
+         when Special_Preprocessor_Character =>
 
             --  If Set_Special_Character has been called for this character,
             --  set Scans.Special_Character and return a Special token.
@@ -2710,15 +2658,6 @@  package body Scng is
          end if;
    end Scan;
 
-   --------------------------
-   -- Set_Comment_As_Token --
-   --------------------------
-
-   procedure Set_Comment_As_Token (Value : Boolean) is
-   begin
-      Comment_Is_Token := Value;
-   end Set_Comment_As_Token;
-
    ------------------------------
    -- Set_End_Of_Line_As_Token --
    ------------------------------
@@ -2732,15 +2671,9 @@  package body Scng is
    -- Set_Special_Character --
    ---------------------------
 
-   procedure Set_Special_Character (C : Character) is
+   procedure Set_Special_Character (C : Special_Preprocessor_Character) is
    begin
-      case C is
-         when '#' | '$' | '_' | '?' | '@' | '`' | '\' | '^' | '~' =>
-            Special_Characters (C) := True;
-
-         when others =>
-            null;
-      end case;
+      Special_Characters (C) := True;
    end Set_Special_Character;
 
    ----------------------


diff --git a/gcc/ada/scng.ads b/gcc/ada/scng.ads
--- a/gcc/ada/scng.ads
+++ b/gcc/ada/scng.ads
@@ -23,11 +23,11 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
---  This package contains a generic lexical analyzer. This is used for scanning
---  Ada source files or text files with an Ada-like syntax, such as project
---  files. It is instantiated in Scn and Prj.Err.
+--  This is a generic lexical analyzer, used for scanning Ada source files, and
+--  also for preprocessor files.
 
 with Casing; use Casing;
+with Scans;  use Scans;
 with Styleg;
 with Types;  use Types;
 
@@ -68,33 +68,27 @@  package Scng is
    --  Scan scans out the next token, and advances the scan state accordingly
    --  (see package Scan_State for details). If the scan encounters an illegal
    --  token, then an error message is issued pointing to the bad character,
-   --  and Scan returns a reasonable substitute token of some kind.
-   --  For tokens Char_Literal, Identifier, Real_Literal, Integer_Literal,
-   --  String_Literal and Operator_Symbol, Post_Scan is called after scanning.
+   --  and Scan returns a reasonable substitute token. For tokens Char_Literal,
+   --  Identifier, Real_Literal, Integer_Literal, String_Literal and
+   --  Operator_Symbol, Post_Scan is called after scanning.
 
    function Determine_Token_Casing return Casing_Type;
    pragma Inline (Determine_Token_Casing);
    --  Determines the casing style of the current token, which is
    --  either a keyword or an identifier. See also package Casing.
 
-   procedure Set_Special_Character (C : Character);
-   --  Indicate that one of the following character '#', '$', '?',  '`',
-   --  '\', '^', '_' or '~', when found is a Special token.
-   --  AI12-0125-03 : target name (ES) is not in this list because '@' is
-   --  handled as a special token as abbreviation of LHS of assignment.
+   procedure Set_Special_Character (C : Special_Preprocessor_Character);
+   --  Called when the preprocessor is active to indicate that Scan should
+   --  return a Special token for C.
 
    procedure Reset_Special_Characters;
-   --  Indicate that there is no characters that are Special tokens., which
+   --  Indicate that there are no characters that are Special tokens, which
    --  is the default.
 
    procedure Set_End_Of_Line_As_Token (Value : Boolean);
    --  Indicate if End_Of_Line is a token or not.
    --  By default, End_Of_Line is not a token.
 
-   procedure Set_Comment_As_Token (Value : Boolean);
-   --  Indicate if a comment is a token or not.
-   --  By default, a comment is not a token.
-
    function Set_Start_Column return Column_Number;
    --  This routine is called with Scan_Ptr pointing to the first character
    --  of a line. On exit, Scan_Ptr is advanced to the first non-blank