public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-8140] c++: Treat alignas align_expr and aligned attribute's operand as manifestly constant evaluation [PR1
@ 2022-04-13 13:44 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-04-13 13:44 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:13c32c1984f5857ccce2aeb00ce34343e5a26954

commit r12-8140-g13c32c1984f5857ccce2aeb00ce34343e5a26954
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Apr 13 15:43:34 2022 +0200

    c++: Treat alignas align_expr and aligned attribute's operand as manifestly constant evaluation [PR105233]
    
    The following testcase fails, because we only constant evaluate the
    alignas argument as non-manifestly constant-evaluated and as
    __builtin_is_constant_evaluated appears, we make it non-constant
    (the reason is that we often try to evaluate some expression without
    manifestly_const_eval perhaps even multiple times before actually
    evaluating it with manifestly_const_eval (e.g. when folding for warnings
    and in many other places), and we don't want __builtin_is_constant_evaluated
    to evaluate to false in those cases, because we could get a different
    result from when we actually evaluate it with manifestly_const_eval
    set).
    Now, for alignas the standard seems to be clear, it says the
    argument is constant-expression, which means we should
    manifestly-constant-eval it.
    Attributes are a fuzzy area, they are extensions and various attributes
    take e.g. identifiers, or string literals etc. as arguments.
    
    Either we can just treat alignas as manifestly-const-eval, for that
    we'd need some way how to differentiate between alignas and gnu::aligned
    or aligned attribute.
    
    Another possibility is what the patch below implements, treat
    both alignas and gnu::aligned and aligned attribute's argument as
    manifestly-const-eval and not do that for other attributes.
    
    Another is to go through all attributes and figure out for which
    such treatment is useful (e.g. those that expect INTEGER_CST as argument),
    and either add a new column in the attribute table or have another table
    in the C++ FE to find out which attribute needs that.
    
    Another is do that for all the attribute arguments that are EXPR_P
    and see what breaks (bet that it could be quite risky this late in
    GCC 12 cycle and especially for backporting).
    
    2022-04-13  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/105233
            * decl2.cc (cp_check_const_attributes): For aligned attribute
            pass manifestly_const_eval=true to fold_non_dependent_expr.
    
            * g++.dg/cpp2a/is-constant-evaluated13.C: New test.

Diff:
---
 gcc/cp/decl2.cc                                      |  9 ++++++++-
 gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated13.C | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index e10331f8568..d2b29208ed5 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -1534,12 +1534,19 @@ cp_check_const_attributes (tree attributes)
   for (attr = attributes; attr; attr = TREE_CHAIN (attr))
     {
       tree arg;
+      /* As we implement alignas using gnu::aligned attribute and
+	 alignas argument is a constant expression, force manifestly
+	 constant evaluation of aligned attribute argument.  */
+      bool manifestly_const_eval
+	= is_attribute_p ("aligned", get_attribute_name (attr));
       for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
 	   arg = TREE_CHAIN (arg))
 	{
 	  tree expr = TREE_VALUE (arg);
 	  if (EXPR_P (expr))
-	    TREE_VALUE (arg) = fold_non_dependent_expr (expr);
+	    TREE_VALUE (arg)
+	      = fold_non_dependent_expr (expr, tf_warning_or_error,
+					 manifestly_const_eval);
 	}
     }
 }
diff --git a/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated13.C b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated13.C
new file mode 100644
index 00000000000..4a8af29b086
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated13.C
@@ -0,0 +1,19 @@
+// PR c++/105233
+// { dg-do compile { target c++14 } }
+
+template <typename T>
+constexpr T
+foo (T x) noexcept
+{
+  bool a = __builtin_is_constant_evaluated ();
+  T b = 4 * alignof (int);
+  return x < b ? b : x;
+}
+
+template <typename T>
+struct A { T a, b, c; };
+
+template <typename T>
+struct alignas (foo (sizeof (A<T>))) B { A<T> d; };
+
+B<int> e;


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

only message in thread, other threads:[~2022-04-13 13:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-13 13:44 [gcc r12-8140] c++: Treat alignas align_expr and aligned attribute's operand as manifestly constant evaluation [PR1 Jakub Jelinek

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).