[Ada] Improve error recovery

2020-11-30 Thread Pierre-Marie de Rodat
Function P_Formal_Part was unnecessarily calling Scan after calling
T_Semicolon: T_Semicolon is already calling Scan to go past the
comma-used-insted-of-semicolon.

This avoids spurious cascaded errors in case of e.g:

  procedure P (A : Integer, B : Integer, C : Integer) is

where ',' is used instead of ';'.

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

gcc/ada/

* par-ch6.adb (P_Formal_Part): Remove extra call to Scan.
* par-tchk.adb: Minor reformatting.diff --git a/gcc/ada/par-ch6.adb b/gcc/ada/par-ch6.adb
--- a/gcc/ada/par-ch6.adb
+++ b/gcc/ada/par-ch6.adb
@@ -1650,7 +1650,6 @@ package body Ch6 is
 
  elsif Token = Tok_Comma then
 T_Semicolon;
-Scan; -- past comma
 
  --  Special check for omitted separator
 


diff --git a/gcc/ada/par-tchk.adb b/gcc/ada/par-tchk.adb
--- a/gcc/ada/par-tchk.adb
+++ b/gcc/ada/par-tchk.adb
@@ -436,7 +436,6 @@ package body Tchk is
 
procedure T_Semicolon is
begin
-
   if Token = Tok_Semicolon then
  Scan;
 




[Ada] Improve error recovery for bad comma/semicolon in expression

2014-10-20 Thread Arnaud Charlet
This patch improves the error recovery for an errant comma or semicolon
after one condition in an expression when more conditions follow, as
shown in this example:

 1. procedure BadANDTHEN (X : Integer) is
 2. begin
 3.if X > 10
 4.  and then X mod 4 = 2;
 |
>>> extra ";" ignored

 5.  and then X mod 12 = 8
 6.then
 7.   null;
 8.end if;
 9. end;

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

2014-10-20  Robert Dewar  

* par-ch4.adb (P_Expression): Handle extraneous comma/semicolon
in middle of expression with logical operators.

Index: par-ch4.adb
===
--- par-ch4.adb (revision 216469)
+++ par-ch4.adb (working copy)
@@ -1708,6 +1708,48 @@
 Node1 := New_Op_Node (Logical_Op, Op_Location);
 Set_Left_Opnd (Node1, Node2);
 Set_Right_Opnd (Node1, P_Relation);
+
+--  Check for case of errant comma or semicolon
+
+if Token = Tok_Comma or else Token = Tok_Semicolon then
+   declare
+  Com: constant Boolean := Token = Tok_Comma;
+  Scan_State : Saved_Scan_State;
+  Logop  : Node_Kind;
+
+   begin
+  Save_Scan_State (Scan_State); -- at comma/semicolon
+  Scan; -- past comma/semicolon
+
+  --  Check for AND THEN or OR ELSE after comma/semicolon. We
+  --  do not deal with AND/OR because those cases get mixed up
+  --  with the select alternatives case.
+
+  if Token = Tok_And or else Token = Tok_Or then
+ Logop := P_Logical_Operator;
+ Restore_Scan_State (Scan_State); -- to comma/semicolon
+
+ if Nkind_In (Logop, N_And_Then, N_Or_Else) then
+Scan; -- past comma/semicolon
+
+if Com then
+   Error_Msg_SP -- CODEFIX
+ ("|extra "","" ignored");
+else
+   Error_Msg_SP -- CODEFIX
+ ("|extra "";"" ignored");
+end if;
+
+ else
+Restore_Scan_State (Scan_State); -- to comma/semicolon
+ end if;
+
+  else
+ Restore_Scan_State (Scan_State); -- to comma/semicolon
+  end if;
+   end;
+end if;
+
 exit when Token not in Token_Class_Logop;
  end loop;