diff options
author | Marek Polacek <polacek@redhat.com> | 2019-08-19 13:59:13 +0000 |
---|---|---|
committer | Marek Polacek <mpolacek@gcc.gnu.org> | 2019-08-19 13:59:13 +0000 |
commit | 04e1749c557a5df14f8528efa451bb0e93afea80 (patch) | |
tree | d3254c67f6ed344600c62297817d2c44b595a9e3 /gcc/cp/constexpr.c | |
parent | 5a9ea4fff4554d9dc0966e8f2598a01602df03cf (diff) | |
download | gcc-04e1749c557a5df14f8528efa451bb0e93afea80.zip gcc-04e1749c557a5df14f8528efa451bb0e93afea80.tar.gz gcc-04e1749c557a5df14f8528efa451bb0e93afea80.tar.bz2 |
PR c++/91264 - detect modifying const objects in constexpr.
* constexpr.c (modifying_const_object_error): New function.
(cxx_eval_call_expression): Set TREE_READONLY on a CONSTRUCTOR of
a const-qualified object after it's been fully constructed.
(modifying_const_object_p): New function.
(cxx_eval_store_expression): Detect modifying a const object
during constant expression evaluation.
(cxx_eval_increment_expression): Use a better location when building
up the store.
(cxx_eval_constant_expression) <case DECL_EXPR>: Mark a constant
object's constructor TREE_READONLY.
* g++.dg/cpp1y/constexpr-tracking-const1.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const2.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const3.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const4.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const5.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const6.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const7.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const8.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const9.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const10.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const11.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const12.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const13.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const14.C: New test.
From-SVN: r274671
Diffstat (limited to 'gcc/cp/constexpr.c')
-rw-r--r-- | gcc/cp/constexpr.c | 140 |
1 files changed, 139 insertions, 1 deletions
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 23f2a02..dbd0dc3 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1575,6 +1575,19 @@ clear_no_implicit_zero (tree ctor) } } +/* Complain about a const object OBJ being modified in a constant expression. + EXPR is the MODIFY_EXPR expression performing the modification. */ + +static void +modifying_const_object_error (tree expr, tree obj) +{ + location_t loc = cp_expr_loc_or_input_loc (expr); + auto_diagnostic_group d; + error_at (loc, "modifying a const object %qE is not allowed in " + "a constant expression", TREE_OPERAND (expr, 0)); + inform (location_of (obj), "originally declared %<const%> here"); +} + /* Subroutine of cxx_eval_constant_expression. Evaluate the call expression tree T in the context of OLD_CALL expression evaluation. */ @@ -1775,6 +1788,19 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, depth_ok = push_cx_call_context (t); + /* Remember the object we are constructing. */ + tree new_obj = NULL_TREE; + if (DECL_CONSTRUCTOR_P (fun)) + { + /* In a constructor, it should be the first `this' argument. + At this point it has already been evaluated in the call + to cxx_bind_parameters_in_call. */ + new_obj = TREE_VEC_ELT (new_call.bindings, 0); + STRIP_NOPS (new_obj); + if (TREE_CODE (new_obj) == ADDR_EXPR) + new_obj = TREE_OPERAND (new_obj, 0); + } + tree result = NULL_TREE; constexpr_call *entry = NULL; @@ -1910,6 +1936,23 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, } } + /* At this point, the object's constructor will have run, so + the object is no longer under construction, and its possible + 'const' semantics now apply. Make a note of this fact by + marking the CONSTRUCTOR TREE_READONLY. */ + if (new_obj + && CLASS_TYPE_P (TREE_TYPE (new_obj)) + && CP_TYPE_CONST_P (TREE_TYPE (new_obj))) + { + /* Subobjects might not be stored in ctx->values but we can + get its CONSTRUCTOR by evaluating *this. */ + tree e = cxx_eval_constant_expression (ctx, new_obj, + /*lval*/false, + non_constant_p, + overflow_p); + TREE_READONLY (e) = true; + } + /* Forget the saved values of the callee's SAVE_EXPRs. */ unsigned int i; tree save_expr; @@ -3724,6 +3767,26 @@ maybe_simplify_trivial_copy (tree &target, tree &init) } } +/* Return true if we are modifying something that is const during constant + expression evaluation. CODE is the code of the statement, OBJ is the + object in question, MUTABLE_P is true if one of the subobjects were + declared mutable. */ + +static bool +modifying_const_object_p (tree_code code, tree obj, bool mutable_p) +{ + /* If this is initialization, there's no problem. */ + if (code != MODIFY_EXPR) + return false; + + /* [basic.type.qualifier] "A const object is an object of type + const T or a non-mutable subobject of a const object." */ + if (mutable_p) + return false; + + return (TREE_READONLY (obj) || CP_TYPE_CONST_P (TREE_TYPE (obj))); +} + /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ static tree @@ -3773,6 +3836,9 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, /* Find the underlying variable. */ releasing_vec refs; tree object = NULL_TREE; + /* If we're modifying a const object, save it. */ + tree const_object_being_modified = NULL_TREE; + bool mutable_p = false; for (tree probe = target; object == NULL_TREE; ) { switch (TREE_CODE (probe)) @@ -3783,6 +3849,12 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, { tree ob = TREE_OPERAND (probe, 0); tree elt = TREE_OPERAND (probe, 1); + if (DECL_P (elt) && DECL_MUTABLE_P (elt)) + mutable_p = true; + if (evaluated + && modifying_const_object_p (TREE_CODE (t), probe, mutable_p) + && const_object_being_modified == NULL_TREE) + const_object_being_modified = probe; if (TREE_CODE (probe) == ARRAY_REF) { elt = eval_and_check_array_index (ctx, probe, false, @@ -3811,6 +3883,10 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, } } + if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) + && const_object_being_modified == NULL_TREE) + const_object_being_modified = object; + /* And then find/build up our initializer for the path to the subobject we're initializing. */ tree *valp; @@ -3950,6 +4026,62 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, valp = &cep->value; } + /* Detect modifying a constant object in constexpr evaluation. + We have found a const object that is being modified. Figure out + if we need to issue an error. Consider + + struct A { + int n; + constexpr A() : n(1) { n = 2; } // #1 + }; + struct B { + const A a; + constexpr B() { a.n = 3; } // #2 + }; + constexpr B b{}; + + #1 is OK, since we're modifying an object under construction, but + #2 is wrong, since "a" is const and has been fully constructed. + To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR + which means that the object is read-only. For the example above, the + *ctors stack at the point of #2 will look like: + + ctors[0] = {.a={.n=2}} TREE_READONLY = 0 + ctors[1] = {.n=2} TREE_READONLY = 1 + + and we're modifying "b.a", so we search the stack and see if the + constructor for "b.a" has already run. */ + if (const_object_being_modified) + { + bool fail = false; + if (!CLASS_TYPE_P (TREE_TYPE (const_object_being_modified))) + fail = true; + else + { + /* [class.ctor]p5 "A constructor can be invoked for a const, + volatile, or const volatile object. const and volatile + semantics are not applied on an object under construction. + They come into effect when the constructor for the most + derived object ends." */ + tree elt; + unsigned int i; + FOR_EACH_VEC_ELT (*ctors, i, elt) + if (same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) + { + fail = TREE_READONLY (elt); + break; + } + } + if (fail) + { + if (!ctx->quiet) + modifying_const_object_error (t, const_object_being_modified); + *non_constant_p = true; + return t; + } + } + if (!preeval) { /* Create a new CONSTRUCTOR in case evaluation of the initializer @@ -4063,7 +4195,8 @@ cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, VERIFY_CONSTANT (mod); /* Storing the modified value. */ - tree store = build2 (MODIFY_EXPR, type, op, mod); + tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), + MODIFY_EXPR, type, op, mod); cxx_eval_constant_expression (ctx, store, true, non_constant_p, overflow_p); ggc_free (store); @@ -4650,6 +4783,11 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, non_constant_p, overflow_p); /* Don't share a CONSTRUCTOR that might be changed. */ init = unshare_constructor (init); + /* Remember that a constant object's constructor has already + run. */ + if (CLASS_TYPE_P (TREE_TYPE (r)) + && CP_TYPE_CONST_P (TREE_TYPE (r))) + TREE_READONLY (init) = true; ctx->values->put (r, init); } else if (ctx == &new_ctx) |