[Ada] Warn for failure to parenthesize unary minus

Message ID 20121029095838.GA16096@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Oct. 29, 2012, 9:58 a.m.
This patch gives a warning when unary minus is used in a context that
requires parentheses, and no parentheses are present, as shown by the
following test:

     1. function Test_Mod_B (N: in Integer) return Integer is
     2. begin
     3.    return N mod -10;
        >>> parentheses required for unary minus

     4. end Test_Mod_B;

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

2012-10-29  Robert Dewar  <dewar@adacore.com>

	* par-ch4.adb (P_Primary): Warn on bad use of unary minus.


Index: par-ch4.adb
--- par-ch4.adb	(revision 192908)
+++ par-ch4.adb	(working copy)
@@ -2364,6 +2364,7 @@ 
       --  The loop runs more than once only if misplaced pragmas are found
+      --  or if a misplaced unary minus is skipped.
          case Token is
@@ -2537,8 +2538,15 @@ 
                   return P_Identifier;
                end if;
+            --  Minus may well be an improper attempt at a unary minus. Give
+            --  a message, skip the minus and keep going!
+            when Tok_Minus =>
+               Error_Msg_SC ("parentheses required for unary minus");
+               Scan; -- past minus
             --  Anything else is illegal as the first token of a primary, but
-            --  we test for a reserved identifier so that it is treated nicely
+            --  we test for some common errors, to improve error messages.
             when others =>
                if Is_Reserved_Identifier then