]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR c++/85662 ("error: non-constant condition for static assertion" from...
authorJakub Jelinek <jakub@redhat.com>
Fri, 22 Jun 2018 21:23:03 +0000 (23:23 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 22 Jun 2018 21:23:03 +0000 (23:23 +0200)
Backported from mainline
2018-06-22  Jakub Jelinek  <jakub@redhat.com>

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

2018-05-10  Jakub Jelinek  <jakub@redhat.com>

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.

From-SVN: r261962

gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/c-family/c-common.h
gcc/c/ChangeLog
gcc/c/c-fold.c
gcc/c/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/cp-gimplify.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/offsetof2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/offsetof3.C [new file with mode: 0644]

index 962f62844a3b87b7ebbdedd5783dc9a5c48f1646..e2c7ea36b201ca598b2d0d6b51aa12087027a755 100644 (file)
@@ -1,3 +1,18 @@
+2018-06-22  Jakub Jelinek  <jakub@redhat.com>
+
+       Backported from mainline
+       2018-05-10  Jakub Jelinek  <jakub@redhat.com>
+
+       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.
+
 2018-04-26  Richard Biener  <rguenther@suse.de>
 
        Backport from mainline
index e272488b39be6804294c9e99cc78669418891296..ade84866709bffff98f43973842387a87f7eac80 100644 (file)
@@ -6253,10 +6253,11 @@ c_common_to_target_charset (HOST_WIDE_INT c)
 
 /* Fold an offsetof-like expression.  EXPR is a nested sequence of component
    references with an INDIRECT_REF of a constant at the bottom; much like the
-   traditional rendering of offsetof as a macro.  Return the folded result.  */
+   traditional rendering of offsetof as a macro.  TYPE is the desired type of
+   the whole expression.  Return the folded result.  */
 
 tree
-fold_offsetof_1 (tree expr, enum tree_code ctx)
+fold_offsetof (tree expr, tree type, enum tree_code ctx)
 {
   tree base, off, t;
   tree_code code = TREE_CODE (expr);
@@ -6281,10 +6282,10 @@ fold_offsetof_1 (tree expr, enum tree_code ctx)
          error ("cannot apply %<offsetof%> to a non constant address");
          return error_mark_node;
        }
-      return TREE_OPERAND (expr, 0);
+      return convert (type, TREE_OPERAND (expr, 0));
 
     case COMPONENT_REF:
-      base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
+      base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
       if (base == error_mark_node)
        return base;
 
@@ -6301,7 +6302,7 @@ fold_offsetof_1 (tree expr, enum tree_code ctx)
       break;
 
     case ARRAY_REF:
-      base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
+      base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
       if (base == error_mark_node)
        return base;
 
@@ -6358,23 +6359,16 @@ fold_offsetof_1 (tree expr, enum tree_code ctx)
       /* Handle static members of volatile structs.  */
       t = TREE_OPERAND (expr, 1);
       gcc_assert (VAR_P (t));
-      return fold_offsetof_1 (t);
+      return fold_offsetof (t, type);
 
     default:
       gcc_unreachable ();
     }
 
+  if (!POINTER_TYPE_P (type))
+    return size_binop (PLUS_EXPR, base, convert (type, off));
   return fold_build_pointer_plus (base, off);
 }
-
-/* Likewise, but convert it to the return type of offsetof.  */
-
-tree
-fold_offsetof (tree expr)
-{
-  return convert (size_type_node, fold_offsetof_1 (expr));
-}
-
 \f
 /* *PTYPE is an incomplete array.  Complete it with a domain based on
    INITIAL_VALUE.  If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
index b933342043437488b0ea8684b6af9784930a47a1..0cff44142948f4bf0c651985c94cf12f95e2a764 100644 (file)
@@ -1053,8 +1053,8 @@ extern bool c_dump_tree (void *, tree);
 
 extern void verify_sequence_points (tree);
 
-extern tree fold_offsetof_1 (tree, tree_code ctx = ERROR_MARK);
-extern tree fold_offsetof (tree);
+extern tree fold_offsetof (tree, tree = size_type_node,
+                          tree_code ctx = ERROR_MARK);
 
 extern int complete_array_type (tree *, tree, bool);
 
index 96b9effea12167d95bad87c74f8b4f01ebd9c5ea..9370bef39180e7e639c18672b3420d0eab140972 100644 (file)
@@ -1,6 +1,16 @@
 2018-06-22  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2018-05-10  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/85662
+       * 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.
+
        2018-03-21  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/84999
index b060d76da43fba23e686fe3a79cb45ad3a116039..96c041e265d26b9f1482e007363d56c812ff461d 100644 (file)
@@ -403,7 +403,7 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
          && (op1 = get_base_address (op0)) != NULL_TREE
          && INDIRECT_REF_P (op1)
          && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
-       ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
+       ret = fold_offsetof (op0, TREE_TYPE (expr));
       else if (op0 != orig_op0 || in_init)
        ret = in_init
          ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
index 363fa32d468db9f2359d97f4995b4bba81a5b710..2c87ec9875a73a27e241241b800ddb9bdad24a9a 100644 (file)
@@ -4638,7 +4638,7 @@ build_unary_op (location_t location, enum tree_code code, tree xarg,
       if (val && INDIRECT_REF_P (val)
           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
        {
-         ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
+         ret = fold_offsetof (arg, argtype);
          goto return_build_unary_op;
        }
 
index 19f235292727b04cef5ad93315637c8e0867fcb8..effd4a7b27d6739cfb44fd2ae7c0e249a7b9ad8b 100644 (file)
@@ -1,6 +1,13 @@
 2018-06-22  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2018-05-10  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/85662
+       * 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.
+
        2018-04-18  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/84463
index c05547b6ff930d592c1e5ad2cc5a4bab270fdbe8..5f5274a0ce3aed1984e941e4de970aec47079b47 100644 (file)
@@ -2173,7 +2173,7 @@ cp_fold (tree x)
              val = TREE_OPERAND (val, 0);
              STRIP_NOPS (val);
              if (TREE_CODE (val) == INTEGER_CST)
-               return fold_convert (TREE_TYPE (x), fold_offsetof_1 (op0));
+               return fold_offsetof (op0, TREE_TYPE (x));
            }
        }
       goto finish_unary;
index ec4153a635eb59870a8edf15762342884c68cebc..dd4c71e9baf3ef04a86ff544d88091bcd0b01c2a 100644 (file)
@@ -1,6 +1,16 @@
 2018-06-22  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2018-06-22  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/85662
+       * g++.dg/ext/offsetof3.C: New test.
+
+       2018-05-10  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/85662
+       * g++.dg/ext/offsetof2.C: New test.
+
        2018-05-06  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/85659
diff --git a/gcc/testsuite/g++.dg/ext/offsetof2.C b/gcc/testsuite/g++.dg/ext/offsetof2.C
new file mode 100644 (file)
index 0000000..647cf8d
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/85662
+// { dg-do compile { target c++11 } }
+
+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]), "");
diff --git a/gcc/testsuite/g++.dg/ext/offsetof3.C b/gcc/testsuite/g++.dg/ext/offsetof3.C
new file mode 100644 (file)
index 0000000..6bf4c50
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/85662
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2" }
+
+#include "offsetof2.C"