public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Reject junk syntax for Contract_Cases/Test_Case/Subprogram_Variant
@ 2020-12-16 13:15 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2020-12-16 13:15 UTC (permalink / raw)
  To: gcc-patches; +Cc: Piotr Trojanek

[-- Attachment #1: Type: text/plain, Size: 577 bytes --]

Reject contracts Contract_Cases, Test_Case and Subprogram_Variant whose
expression is either "null", "(null record)" or has extra parentheses.

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

gcc/ada/

	* sem_ch13.adb (Analyze_Aspect_Specifications): Add a codefix
	for extra parentheses around aspect Annotate expression; reject
	"(null record)" aggregate and extra parentheses around aspect
	Test_Case expression.
	* sem_prag.adb (Analyze_Pragma): Reject "null", "(null record)"
	and extra parentheses around pragma Contract_Cases; likewise for
	pragma Subprogram_Variant.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 4572 bytes --]

diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb
--- a/gcc/ada/sem_ch13.adb
+++ b/gcc/ada/sem_ch13.adb
@@ -4145,7 +4145,8 @@ package body Sem_Ch13 is
                         --  Must not be parenthesized
 
                         if Paren_Count (Expr) /= 0 then
-                           Error_Msg_F ("extra parentheses ignored", Expr);
+                           Error_Msg -- CODEFIX
+                             ("redundant parentheses", First_Sloc (Expr));
                         end if;
 
                         --  List of arguments is list of aggregate expressions
@@ -4426,13 +4427,24 @@ package body Sem_Ch13 is
                      goto Continue;
                   end if;
 
-                  if Nkind (Expr) /= N_Aggregate then
+                  if Nkind (Expr) /= N_Aggregate
+                    or else Null_Record_Present (Expr)
+                  then
                      Error_Msg_Name_1 := Nam;
                      Error_Msg_NE
                        ("wrong syntax for aspect `%` for &", Id, E);
                      goto Continue;
                   end if;
 
+                  --  Check that the expression is a proper aggregate (no
+                  --  parentheses).
+
+                  if Paren_Count (Expr) /= 0 then
+                     Error_Msg -- CODEFIX
+                       ("redundant parentheses", First_Sloc (Expr));
+                     goto Continue;
+                  end if;
+
                   --  Create the list of arguments for building the Test_Case
                   --  pragma.
 


diff --git a/gcc/ada/sem_prag.adb b/gcc/ada/sem_prag.adb
--- a/gcc/ada/sem_prag.adb
+++ b/gcc/ada/sem_prag.adb
@@ -545,16 +545,31 @@ package body Sem_Prag is
 
       --  Single and multiple contract cases must appear in aggregate form. If
       --  this is not the case, then either the parser or the analysis of the
-      --  pragma failed to produce an aggregate.
+      --  pragma failed to produce an aggregate, e.g. when the contract is
+      --  "null" or a "(null record)".
 
-      pragma Assert (Nkind (CCases) = N_Aggregate);
+      pragma Assert
+        (if Nkind (CCases) = N_Aggregate
+         then Null_Record_Present (CCases)
+           xor (Present (Component_Associations (CCases))
+                  or
+                Present (Expressions (CCases)))
+         else Nkind (CCases) = N_Null);
 
       --  Only CASE_GUARD => CONSEQUENCE clauses are allowed
 
-      if Present (Component_Associations (CCases))
+      if Nkind (CCases) = N_Aggregate
+        and then Present (Component_Associations (CCases))
         and then No (Expressions (CCases))
       then
 
+         --  Check that the expression is a proper aggregate (no parentheses)
+
+         if Paren_Count (CCases) /= 0 then
+            Error_Msg -- CODEFIX
+              ("redundant parentheses", First_Sloc (CCases));
+         end if;
+
          --  Ensure that the formal parameters are visible when analyzing all
          --  clauses. This falls out of the general rule of aspects pertaining
          --  to subprogram declarations.
@@ -29170,16 +29185,31 @@ package body Sem_Prag is
 
       --  Single and multiple contract cases must appear in aggregate form. If
       --  this is not the case, then either the parser of the analysis of the
-      --  pragma failed to produce an aggregate.
+      --  pragma failed to produce an aggregate, e.g. when the contract is
+      --  "null" or a "(null record)".
 
-      pragma Assert (Nkind (Variants) = N_Aggregate);
+      pragma Assert
+        (if Nkind (Variants) = N_Aggregate
+         then Null_Record_Present (Variants)
+           xor (Present (Component_Associations (Variants))
+                  or
+                Present (Expressions (Variants)))
+         else Nkind (Variants) = N_Null);
 
       --  Only "change_direction => discrete_expression" clauses are allowed
 
-      if Present (Component_Associations (Variants))
+      if Nkind (Variants) = N_Aggregate
+        and then Present (Component_Associations (Variants))
         and then No (Expressions (Variants))
       then
 
+         --  Check that the expression is a proper aggregate (no parentheses)
+
+         if Paren_Count (Variants) /= 0 then
+            Error_Msg -- CODEFIX
+              ("redundant parentheses", First_Sloc (Variants));
+         end if;
+
          --  Ensure that the formal parameters are visible when analyzing all
          --  clauses. This falls out of the general rule of aspects pertaining
          --  to subprogram declarations.



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-16 13:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-16 13:15 [Ada] Reject junk syntax for Contract_Cases/Test_Case/Subprogram_Variant Pierre-Marie de Rodat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).