[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-06-25 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

Jakub Jelinek  changed:

   What|Removed |Added

 Status|REOPENED|RESOLVED
 Resolution|--- |FIXED

--- Comment #13 from Jakub Jelinek  ---
Fixed for 7.4+.

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-06-22 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

--- Comment #12 from Jakub Jelinek  ---
Author: jakub
Date: Fri Jun 22 21:23:03 2018
New Revision: 261962

URL: https://gcc.gnu.org/viewcvs?rev=261962&root=gcc&view=rev
Log:
Backported from mainline
2018-06-22  Jakub Jelinek  

PR c++/85662
* g++.dg/ext/offsetof3.C: New test.

2018-05-10  Jakub Jelinek  

PR c++/85662
* c-common.h (fold_offsetof_1): Removed.
(fold_offsetof): Add TYPE argument defaulted to size_type_node and
CTX argument defaulted to ERROR_MARK.
* c-common.c (fold_offsetof_1): Renamed to ...
(fold_offsetof): ... this.  Remove wrapper function.  Add TYPE
argument, convert the pointer constant to TYPE and use size_binop
with PLUS_EXPR instead of fold_build_pointer_plus if type is not
a pointer type.  Adjust recursive calls.

* c-fold.c (c_fully_fold_internal): Use fold_offsetof rather than
fold_offsetof_1, pass TREE_TYPE (expr) as TYPE to it and drop the
fold_convert_loc.
* c-typeck.c (build_unary_op): Use fold_offsetof rather than
fold_offsetof_1, pass argtype as TYPE to it and drop the
fold_convert_loc.

* cp-gimplify.c (cp_fold): Use fold_offsetof rather than
fold_offsetof_1, pass TREE_TYPE (x) as TYPE to it and drop the
fold_convert.

* g++.dg/ext/offsetof2.C: New test.

Added:
branches/gcc-7-branch/gcc/testsuite/g++.dg/ext/offsetof2.C
branches/gcc-7-branch/gcc/testsuite/g++.dg/ext/offsetof3.C
Modified:
branches/gcc-7-branch/gcc/c-family/ChangeLog
branches/gcc-7-branch/gcc/c-family/c-common.c
branches/gcc-7-branch/gcc/c-family/c-common.h
branches/gcc-7-branch/gcc/c/ChangeLog
branches/gcc-7-branch/gcc/c/c-fold.c
branches/gcc-7-branch/gcc/c/c-typeck.c
branches/gcc-7-branch/gcc/cp/ChangeLog
branches/gcc-7-branch/gcc/cp/cp-gimplify.c
branches/gcc-7-branch/gcc/testsuite/ChangeLog

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-06-22 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

--- Comment #11 from Jakub Jelinek  ---
Author: jakub
Date: Fri Jun 22 17:26:44 2018
New Revision: 261910

URL: https://gcc.gnu.org/viewcvs?rev=261910&root=gcc&view=rev
Log:
PR c++/85662
* g++.dg/ext/offsetof3.C: New test.

Added:
branches/gcc-8-branch/gcc/testsuite/g++.dg/ext/offsetof3.C
Modified:
branches/gcc-8-branch/gcc/testsuite/ChangeLog

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-06-22 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

--- Comment #10 from Jakub Jelinek  ---
Author: jakub
Date: Fri Jun 22 17:22:43 2018
New Revision: 261909

URL: https://gcc.gnu.org/viewcvs?rev=261909&root=gcc&view=rev
Log:
PR c++/85662
* g++.dg/ext/offsetof3.C: New test.

Added:
trunk/gcc/testsuite/g++.dg/ext/offsetof3.C
Modified:
trunk/gcc/testsuite/ChangeLog

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-30 Thread roland at gnu dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

roland at gnu dot org changed:

   What|Removed |Added

 Status|RESOLVED|REOPENED
 Resolution|FIXED   |---

--- Comment #9 from roland at gnu dot org ---
Both my original test case and Jakub's smaller case do hit the bug in gcc 7.
I just tested the current gcc-7-branch: commit
c66c7f7b6f41118cef03ece0c367554eb38c3d65

On x86_64-linux-gnu:

$ ../../gcc/configure --enable-languages=c,c++,lto
$ make all-gcc
$ ./gcc/xgcc -Bgcc/ -O2 -S bar.cc
bar.cc:3:1: error: non-constant condition for static assertion
 static_assert (__builtin_offsetof (T, f.x[31 - 1]) == __builtin_offsetof (T,
f.x[30]), "");
 ^
bar.cc:3:1: error: value ‘8’ of type ‘T*’ is not a constant expression
[Exit 1]
$ cat bar.cc
struct S { unsigned long x[31]; };
struct T { bool b; S f; };
static_assert (__builtin_offsetof (T, f.x[31 - 1]) == __builtin_offsetof (T,
f.x[30]), "");
$

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-30 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

Jakub Jelinek  changed:

   What|Removed |Added

 Status|ASSIGNED|RESOLVED
 Resolution|--- |FIXED

--- Comment #8 from Jakub Jelinek  ---
Fixed for 8.2+.  As I said, can't reproduce with GCC 7.

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-30 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

--- Comment #7 from Jakub Jelinek  ---
Author: jakub
Date: Wed May 30 07:20:58 2018
New Revision: 260916

URL: https://gcc.gnu.org/viewcvs?rev=260916&root=gcc&view=rev
Log:
Backported from mainline
2018-05-10  Jakub Jelinek  

PR c++/85662
* c-common.h (fold_offsetof_1): Removed.
(fold_offsetof): Add TYPE argument defaulted to size_type_node and
CTX argument defaulted to ERROR_MARK.
* c-common.c (fold_offsetof_1): Renamed to ...
(fold_offsetof): ... this.  Remove wrapper function.  Add TYPE
argument, convert the pointer constant to TYPE and use size_binop
with PLUS_EXPR instead of fold_build_pointer_plus if type is not
a pointer type.  Adjust recursive calls.

* c-fold.c (c_fully_fold_internal): Use fold_offsetof rather than
fold_offsetof_1, pass TREE_TYPE (expr) as TYPE to it and drop the
fold_convert_loc.
* c-typeck.c (build_unary_op): Use fold_offsetof rather than
fold_offsetof_1, pass argtype as TYPE to it and drop the
fold_convert_loc.

* cp-gimplify.c (cp_fold): Use fold_offsetof rather than
fold_offsetof_1, pass TREE_TYPE (x) as TYPE to it and drop the
fold_convert.

* g++.dg/ext/offsetof2.C: New test.

Added:
branches/gcc-8-branch/gcc/testsuite/g++.dg/ext/offsetof2.C
Modified:
branches/gcc-8-branch/gcc/c-family/ChangeLog
branches/gcc-8-branch/gcc/c-family/c-common.c
branches/gcc-8-branch/gcc/c-family/c-common.h
branches/gcc-8-branch/gcc/c/ChangeLog
branches/gcc-8-branch/gcc/c/c-fold.c
branches/gcc-8-branch/gcc/c/c-typeck.c
branches/gcc-8-branch/gcc/cp/ChangeLog
branches/gcc-8-branch/gcc/cp/cp-gimplify.c
branches/gcc-8-branch/gcc/testsuite/ChangeLog

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-16 Thread roland at gnu dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

--- Comment #6 from roland at gnu dot org ---
Thanks for the fix.  What's the status on backporting this to 8 and/or 7?

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-10 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

--- Comment #5 from Jakub Jelinek  ---
Author: jakub
Date: Thu May 10 17:40:28 2018
New Revision: 260119

URL: https://gcc.gnu.org/viewcvs?rev=260119&root=gcc&view=rev
Log:
PR c++/85662
* c-common.h (fold_offsetof_1): Removed.
(fold_offsetof): Add TYPE argument defaulted to size_type_node and
CTX argument defaulted to ERROR_MARK.
* c-common.c (fold_offsetof_1): Renamed to ...
(fold_offsetof): ... this.  Remove wrapper function.  Add TYPE
argument, convert the pointer constant to TYPE and use size_binop
with PLUS_EXPR instead of fold_build_pointer_plus if type is not
a pointer type.  Adjust recursive calls.

* c-fold.c (c_fully_fold_internal): Use fold_offsetof rather than
fold_offsetof_1, pass TREE_TYPE (expr) as TYPE to it and drop the
fold_convert_loc.
* c-typeck.c (build_unary_op): Use fold_offsetof rather than
fold_offsetof_1, pass argtype as TYPE to it and drop the
fold_convert_loc.

* cp-gimplify.c (cp_fold): Use fold_offsetof rather than
fold_offsetof_1, pass TREE_TYPE (x) as TYPE to it and drop the
fold_convert.

* g++.dg/ext/offsetof2.C: New test.

Added:
trunk/gcc/testsuite/g++.dg/ext/offsetof2.C
Modified:
trunk/gcc/c-family/ChangeLog
trunk/gcc/c-family/c-common.c
trunk/gcc/c-family/c-common.h
trunk/gcc/c/ChangeLog
trunk/gcc/c/c-fold.c
trunk/gcc/c/c-typeck.c
trunk/gcc/cp/ChangeLog
trunk/gcc/cp/cp-gimplify.c
trunk/gcc/testsuite/ChangeLog

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-08 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

Richard Biener  changed:

   What|Removed |Added

   Keywords||rejects-valid
   Priority|P3  |P2

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-05 Thread roland at gnu dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

--- Comment #4 from roland at gnu dot org ---
That fix (applied to trunk) works for my test case and for the original
real-world case I reduced it from.

Will it be backported to 7 and 8?


Thanks for the quick work as usual, Jakub!

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-05 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

Jakub Jelinek  changed:

   What|Removed |Added

 Status|NEW |ASSIGNED
   Assignee|unassigned at gcc dot gnu.org  |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek  ---
Created attachment 44076
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44076&action=edit
gcc8-pr85662.patch

Untested fix.

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-05 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jason at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek  ---
In any case, I think the problem is related to the delayed folding, C++
constexpr handling not liking pointer constants and fold_offsetof_1 done as
pointer addition rather than integer addition.  With delayed folding and
fold_build_pointer_plus called by fold_offsetof_1 the nested expressions aren't
really folded.  We could easily fix it up by doing a cp_fold (together with
recursive cp_fold_r though, because cp_fold isn't fully recursive) on the
result of fold_offsetof in the C++ FE, that way we'd get a constant whenever
possible.
On the other side, we wouldn't then reject __builtin_offsetof used with not
valid constexpr expressions in the second argument (say out of bound array
access etc. subtracted from itself).
Say:
constexpr int a[5];
struct S { int b, c[5]; };
constexpr int d = __builtin_offsetof (S, c[(&a[6] - &a[6]) + 2]);

Another possibility is in fold_offsetof_1, if we detect the TREE_CONSTANT base,
if it is a pointer typed INTEGER_CST, build corresponding sizetype INTEGER_CST
instead and use normal PLUS_EXPR folding (or do we actually want any folding at
all?) instead of fold_build_pointer_plus.

[Bug c++/85662] [8/9 Regression] "error: non-constant condition for static assertion" from __builtin_offsetof in C++

2018-05-05 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85662

Jakub Jelinek  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2018-05-05
 CC||jakub at gcc dot gnu.org
Version|unknown |8.0
   Target Milestone|--- |8.2
Summary|regression since 6: "error: |[8/9 Regression] "error:
   |non-constant condition for  |non-constant condition for
   |static assertion" from  |static assertion" from
   |__builtin_offsetof in C++   |__builtin_offsetof in C++
 Ever confirmed|0   |1

--- Comment #1 from Jakub Jelinek  ---
Weird, I can only reproduce it starting with r247495 and can't reproduce with
gcc 7.
Better testcase that doesn't really depend on the actual structure layout and
sizes:
struct S { unsigned long x[31]; };
struct T { bool b; S f; };
static_assert (__builtin_offsetof (T, f.x[31 - 1]) == __builtin_offsetof (T,
f.x[30]), "");
Using 30 instead of 31-1 makes it go away, and the problem is that something
creates or keeps around 8 as pointer constant.