On Fri, Oct 09, 2015 at 11:14:33AM +0200, Jakub Jelinek wrote:
> The 4.0 spec required that low-bound in array section must not be negative.
> The 4.5 spec removes that restriction, for arrays it is obvious that it is
> invalid anyway (pointer arithmetics disallows that), and for pointers it is
> meaningful.
> 
> After looking what I've implemented, it seems I've applied common sense
> rather than strictly reading the standard.  And that is the behavior we want
> for OpenMP 4.5, so I've just added new testcases to cover the various cases.

The requirement that length is non-negative stays, for a reason, and
unfortunately it seems it has been only tested for arrays, not for pointers.

Thus I've applied this fix and added further tests.

2015-10-09  Jakub Jelinek  <ja...@redhat.com>

c/
        * c-typeck.c (handle_omp_array_sections_1): Diagnose negative
        length even for pointer based array sections.
cp/
        * semantics.c (handle_omp_array_sections_1): Diagnose negative
        length even for pointer based array sections.
testsuite/
        * c-c++-common/gomp/depend-4.c (main): Add tests for negative
        lengths.
        * c-c++-common/gomp/map-2.c (main): Likewise.

--- gcc/c/c-typeck.c.jj 2015-10-09 10:59:18.000000000 +0200
+++ gcc/c/c-typeck.c    2015-10-09 11:17:17.577429234 +0200
@@ -11889,6 +11889,15 @@ handle_omp_array_sections_1 (tree c, tre
                    "for pointer type length expression must be specified");
          return error_mark_node;
        }
+      if (length != NULL_TREE
+         && TREE_CODE (length) == INTEGER_CST
+         && tree_int_cst_sgn (length) == -1)
+       {
+         error_at (OMP_CLAUSE_LOCATION (c),
+                   "negative length in array section in %qs clause",
+                   omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
+         return error_mark_node;
+       }
       /* If there is a pointer type anywhere but in the very first
         array-section-subscript, the array section can't be contiguous.  */
       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
--- gcc/cp/semantics.c.jj       2015-10-09 10:59:18.000000000 +0200
+++ gcc/cp/semantics.c  2015-10-09 11:19:37.057473471 +0200
@@ -4668,6 +4668,15 @@ handle_omp_array_sections_1 (tree c, tre
                    "for pointer type length expression must be specified");
          return error_mark_node;
        }
+      if (length != NULL_TREE
+         && TREE_CODE (length) == INTEGER_CST
+         && tree_int_cst_sgn (length) == -1)
+       {
+         error_at (OMP_CLAUSE_LOCATION (c),
+                   "negative length in array section in %qs clause",
+                   omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
+         return error_mark_node;
+       }
       /* If there is a pointer type anywhere but in the very first
         array-section-subscript, the array section can't be contiguous.  */
       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
--- gcc/testsuite/c-c++-common/gomp/depend-4.c.jj       2015-10-09 
11:04:46.000000000 +0200
+++ gcc/testsuite/c-c++-common/gomp/depend-4.c  2015-10-09 11:24:34.316305364 
+0200
@@ -23,4 +23,22 @@ foo (int *p, int (*q)[10], int r[10], in
   ;
   #pragma omp task depend (inout: b[1:2][-2:4])         /* { dg-error 
"negative low bound in array section in" } */
   ;
+  #pragma omp task depend (inout: p[2:-3])      /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp task depend (inout: q[2:-3][:])   /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp task depend (inout: q[2:3][0:-1])         /* { dg-error 
"negative length in array section in" } */
+  ;
+  #pragma omp task depend (inout: r[2:-5])      /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp task depend (inout: s[2:-5][:])   /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp task depend (inout: s[2:5][0:-4])         /* { dg-error 
"negative length in array section in" } */
+  ;
+  #pragma omp task depend (inout: a[2:-5])      /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp task depend (inout: b[2:-5][0:10]) /* { dg-error "negative 
length in array section in" } */
+  ;
+  #pragma omp task depend (inout: b[2:5][0:-4]) /* { dg-error "negative length 
in array section in" } */
+  ;
 }
--- gcc/testsuite/c-c++-common/gomp/map-2.c.jj  2015-10-09 11:07:19.000000000 
+0200
+++ gcc/testsuite/c-c++-common/gomp/map-2.c     2015-10-09 11:25:03.097901794 
+0200
@@ -23,4 +23,22 @@ foo (int *p, int (*q)[10], int r[10], in
   ;
   #pragma omp target map (tofrom: b[1:2][-2:10]) /* { dg-error "negative low 
bound in array section in" } */
   ;
+  #pragma omp target map (tofrom: p[2:-3])      /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp target map (tofrom: q[2:-3][:])   /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp target map (tofrom: q[2:3][0:-1])         /* { dg-error 
"negative length in array section in" } */
+  ;
+  #pragma omp target map (tofrom: r[2:-5])      /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp target map (tofrom: s[2:-5][:])   /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp target map (tofrom: s[2:5][0:-4])         /* { dg-error 
"negative length in array section in" } */
+  ;
+  #pragma omp target map (tofrom: a[2:-5])      /* { dg-error "negative length 
in array section in" } */
+  ;
+  #pragma omp target map (tofrom: b[2:-5][0:10]) /* { dg-error "negative 
length in array section in" } */
+  ;
+  #pragma omp target map (tofrom: b[2:5][0:-4]) /* { dg-error "negative length 
in array section in" } */
+  ;
 }


        Jakub

Reply via email to