[C++ PATCH] DR 1511 - const volatile variables and ODR
Jakub Jelinek
jakub@redhat.com
Fri Oct 14 17:24:00 GMT 2016
Hi!
We weren't implementing this DR, in the past all non-extern const vars
(and non-inline) at namespace scope had internal linkage, but now only
non-volatile const var.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?
2016-10-14 Jakub Jelinek <jakub@redhat.com>
DR 1511 - const volatile variables and ODR
* decl.c (grokvardecl): Change flags argument to type_quals,
add conceptp argument. Set TREE_PUBLIC for non-static volatile vars.
(grokdeclarator): Adjust grokvardecl caller.
* g++.dg/DRs/dr1511-1.C: New test.
* g++.dg/DRs/dr1511-2.C: New test.
--- gcc/cp/decl.c.jj 2016-10-14 12:31:49.000000000 +0200
+++ gcc/cp/decl.c 2016-10-14 12:50:28.697542270 +0200
@@ -68,7 +68,7 @@ static int unary_op_p (enum tree_code);
static void push_local_name (tree);
static tree grok_reference_init (tree, tree, tree, int);
static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
- int, int, int, int, tree);
+ int, int, int, bool, int, tree);
static int check_static_variable_definition (tree, tree);
static void record_unknown_type (tree, const char *);
static tree builtin_function_1 (tree, tree, bool);
@@ -8512,8 +8512,9 @@ grokvardecl (tree type,
tree orig_declarator,
const cp_decl_specifier_seq *declspecs,
int initialized,
- int flags,
+ int type_quals,
int inlinep,
+ bool conceptp,
int template_count,
tree scope)
{
@@ -8522,8 +8523,8 @@ grokvardecl (tree type,
gcc_assert (!name || identifier_p (name));
- bool constp = flags&1;
- bool conceptp = flags&2;
+ bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
+ bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
/* Compute the scope in which to place the variable, but remember
whether or not that scope was explicitly specified by the user. */
@@ -8580,6 +8581,7 @@ grokvardecl (tree type,
TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
&& (DECL_THIS_EXTERN (decl)
|| ! constp
+ || volatilep
|| inlinep));
TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
}
@@ -11626,8 +11628,9 @@ grokdeclarator (const cp_declarator *dec
decl = grokvardecl (type, dname, unqualified_id,
declspecs,
initialized,
- ((type_quals & TYPE_QUAL_CONST) != 0) | (2 * concept_p),
+ type_quals,
inlinep,
+ concept_p,
template_count,
ctype ? ctype : in_namespace);
if (decl == NULL_TREE)
--- gcc/testsuite/g++.dg/DRs/dr1511-1.C.jj 2016-10-14 13:12:06.745016428 +0200
+++ gcc/testsuite/g++.dg/DRs/dr1511-1.C 2016-10-14 13:12:40.715583815 +0200
@@ -0,0 +1,38 @@
+/* DR 1511 - const volatile variables and the one-definition rule */
+/* { dg-do run } */
+/* { dg-additional-sources "dr1511-2.C" } */
+
+typedef const int cint;
+typedef const volatile int cvint;
+typedef volatile int vint;
+const int v1 = 5;
+extern volatile const int v2;
+cint v3 = 7;
+extern cvint v4;
+extern const vint v5;
+extern volatile cint v6;
+const int w1 = 5;
+extern volatile const int w2;
+cint w3 = 7;
+extern cvint w4;
+extern const vint w5;
+extern volatile cint w6;
+extern const int &r1;
+extern volatile const int &r2;
+extern const int &r3;
+extern const volatile int &r4;
+extern const volatile int &r5;
+extern const volatile int &r6;
+
+int
+main ()
+{
+ if (v1 != 5 || v2 != 6 || v3 != 7 || v4 != 8 || v5 != 9 || v6 != 10)
+ __builtin_abort ();
+ if (w1 != 5 || w2 != 6 || w3 != 7 || w4 != 8 || w5 != 9 || w6 != 10)
+ __builtin_abort ();
+ if (r1 != w1 || &r1 == &w1 || r2 != w2 || &r2 != &w2 || r3 != w3 || &r3 == &w3)
+ __builtin_abort ();
+ if (r4 != w4 || &r4 != &w4 || r5 != w5 || &r5 != &w5 || r6 != w6 || &r6 != &w6)
+ __builtin_abort ();
+}
--- gcc/testsuite/g++.dg/DRs/dr1511-2.C.jj 2016-10-14 13:12:09.912976098 +0200
+++ gcc/testsuite/g++.dg/DRs/dr1511-2.C 2016-10-14 13:11:53.000000000 +0200
@@ -0,0 +1,24 @@
+/* DR 1511 - const volatile variables and the one-definition rule */
+/* { dg-do compile } */
+
+typedef const int cint;
+typedef const volatile int cvint;
+typedef volatile int vint;
+const int v1 = 5;
+volatile const int v2 = 6;
+cint v3 = 7;
+cvint v4 = 8;
+const vint v5 = 9;
+volatile cint v6 = 10;
+const int w1 = 5;
+volatile const int w2 = 6;
+cint w3 = 7;
+cvint w4 = 8;
+const vint w5 = 9;
+volatile cint w6 = 10;
+const int &r1 = w1;
+volatile const int &r2 = w2;
+const int &r3 = w3;
+const volatile int &r4 = w4;
+const volatile int &r5 = w5;
+const volatile int &r6 = w6;
Jakub
More information about the Gcc-patches
mailing list