re PR c++/85515 (Bogus suggestions from "GCC's leaky abstractions")
authorJakub Jelinek <jakub@redhat.com>
Wed, 25 Jul 2018 16:46:34 +0000 (18:46 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 25 Jul 2018 16:46:34 +0000 (18:46 +0200)
PR c++/85515
* cp-tree.h (enum cp_tree_index): Add
CPTI_FOR_{RANGE,BEGIN,END}{,_}_IDENTIFIER.
(for_range__identifier, for_begin__identifier, for_end__identifier,
for_range_identifier, for_begin_identifier, for_end_identifier):
Define.
* decl.c (initialize_predefined_identifiers): Initialize
for_{range,begin,end}{,_}_identifier.
* parser.c (build_range_temp): Use for_range__identifier instead of
get_identifier ("__for_range").
(cp_convert_range_for): Use for_begin__identifier and
for_end__identifier instead of get_identifier ("__for_begin") and
get_identifier ("__for_end").
* semantics.c (finish_for_stmt): Rename "__for_{range,begin,end} "
local symbols to "__for_{range,begin,end}".

* g++.dg/pr85515-2.C: Add expected dg-error.
* g++.dg/cpp0x/range-for36.C: New test.

From-SVN: r262975

gcc/cp/ChangeLog
gcc/cp/cp-tree.h
gcc/cp/decl.c
gcc/cp/parser.c
gcc/cp/semantics.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/range-for36.C [new file with mode: 0644]
gcc/testsuite/g++.dg/pr85515-2.C

index f1185c4f03d08a9283072c15262a275f11c0400d..51de7d15b2f48e88e888ec6dc9ca186bad9a0a28 100644 (file)
@@ -1,3 +1,21 @@
+2018-07-25  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/85515
+       * cp-tree.h (enum cp_tree_index): Add
+       CPTI_FOR_{RANGE,BEGIN,END}{,_}_IDENTIFIER.
+       (for_range__identifier, for_begin__identifier, for_end__identifier,
+       for_range_identifier, for_begin_identifier, for_end_identifier):
+       Define.
+       * decl.c (initialize_predefined_identifiers): Initialize
+       for_{range,begin,end}{,_}_identifier.
+       * parser.c (build_range_temp): Use for_range__identifier instead of
+       get_identifier ("__for_range").
+       (cp_convert_range_for): Use for_begin__identifier and
+       for_end__identifier instead of get_identifier ("__for_begin") and
+       get_identifier ("__for_end").
+       * semantics.c (finish_for_stmt): Rename "__for_{range,begin,end} "
+       local symbols to "__for_{range,begin,end}".
+
 2018-07-23  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/86569
index 0fac7e9892f198742b664e7eb62149a306a6c509..d4b1fa1f0e1e2432c9db0f8e6956854f11926c22 100644 (file)
@@ -154,6 +154,12 @@ enum cp_tree_index
     CPTI_AUTO_IDENTIFIER,
     CPTI_DECLTYPE_AUTO_IDENTIFIER,
     CPTI_INIT_LIST_IDENTIFIER,
+    CPTI_FOR_RANGE__IDENTIFIER,
+    CPTI_FOR_BEGIN__IDENTIFIER,
+    CPTI_FOR_END__IDENTIFIER,
+    CPTI_FOR_RANGE_IDENTIFIER,
+    CPTI_FOR_BEGIN_IDENTIFIER,
+    CPTI_FOR_END_IDENTIFIER,
 
     CPTI_LANG_NAME_C,
     CPTI_LANG_NAME_CPLUSPLUS,
@@ -274,6 +280,12 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
 #define auto_identifier                        cp_global_trees[CPTI_AUTO_IDENTIFIER]
 #define decltype_auto_identifier       cp_global_trees[CPTI_DECLTYPE_AUTO_IDENTIFIER]
 #define init_list_identifier           cp_global_trees[CPTI_INIT_LIST_IDENTIFIER]
+#define for_range__identifier          cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER]
+#define for_begin__identifier          cp_global_trees[CPTI_FOR_BEGIN__IDENTIFIER]
+#define for_end__identifier            cp_global_trees[CPTI_FOR_END__IDENTIFIER]
+#define for_range_identifier           cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER]
+#define for_begin_identifier           cp_global_trees[CPTI_FOR_BEGIN_IDENTIFIER]
+#define for_end_identifier             cp_global_trees[CPTI_FOR_END_IDENTIFIER]
 #define lang_name_c                    cp_global_trees[CPTI_LANG_NAME_C]
 #define lang_name_cplusplus            cp_global_trees[CPTI_LANG_NAME_CPLUSPLUS]
 
index 3c1e2ef36984129e2fa2d561908bbe3113f7d6e5..29d9ba78a218bebf3c9b06d1180546367493dc16 100644 (file)
@@ -4044,6 +4044,12 @@ initialize_predefined_identifiers (void)
     {"auto", &auto_identifier, cik_normal},
     {"decltype(auto)", &decltype_auto_identifier, cik_normal},
     {"initializer_list", &init_list_identifier, cik_normal},
+    {"__for_range ", &for_range__identifier, cik_normal},
+    {"__for_begin ", &for_begin__identifier, cik_normal},
+    {"__for_end ", &for_end__identifier, cik_normal},
+    {"__for_range", &for_range_identifier, cik_normal},
+    {"__for_begin", &for_begin_identifier, cik_normal},
+    {"__for_end", &for_end_identifier, cik_normal},
     {NULL, NULL, cik_normal}
   };
 
index 90d5d007317c679768fb6969831065b57f1a3ef9..3344ef37decfa011cd3bc8478a98500b2069b70e 100644 (file)
@@ -11952,8 +11952,8 @@ build_range_temp (tree range_expr)
                                  type_uses_auto (range_type));
 
   /* Create the __range variable.  */
-  range_temp = build_decl (input_location, VAR_DECL,
-                          get_identifier ("__for_range"), range_type);
+  range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
+                          range_type);
   TREE_USED (range_temp) = 1;
   DECL_ARTIFICIAL (range_temp) = 1;
 
@@ -12060,8 +12060,8 @@ cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
     }
 
   /* The new for initialization statement.  */
-  begin = build_decl (input_location, VAR_DECL,
-                     get_identifier ("__for_begin"), iter_type);
+  begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
+                     iter_type);
   TREE_USED (begin) = 1;
   DECL_ARTIFICIAL (begin) = 1;
   pushdecl (begin);
@@ -12071,8 +12071,7 @@ cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
 
   if (cxx_dialect >= cxx17)
     iter_type = cv_unqualified (TREE_TYPE (end_expr));
-  end = build_decl (input_location, VAR_DECL,
-                   get_identifier ("__for_end"), iter_type);
+  end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
   TREE_USED (end) = 1;
   DECL_ARTIFICIAL (end) = 1;
   pushdecl (end);
index ef73d07161fb3ea5e69e91aefb2f25d24f14442f..bfdca5024d3128b66a6b9b6a4408871991c03335 100644 (file)
@@ -1060,7 +1060,35 @@ finish_for_stmt (tree for_stmt)
                     : &FOR_SCOPE (for_stmt));
   tree scope = *scope_ptr;
   *scope_ptr = NULL;
+
+  /* During parsing of the body, range for uses "__for_{range,begin,end} "
+     decl names to make those unaccessible by code in the body.
+     Change it to ones with underscore instead of space, so that it can
+     be inspected in the debugger.  */
+  tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
+  gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
+             && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
+             && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
+             && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
+             && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
+  for (int i = 0; i < 3; i++)
+    {
+      tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
+      if (IDENTIFIER_BINDING (id)
+         && IDENTIFIER_BINDING (id)->scope == current_binding_level)
+       {
+         range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
+         gcc_assert (VAR_P (range_for_decl[i])
+                     && DECL_ARTIFICIAL (range_for_decl[i]));
+       }
+    }
+
   add_stmt (do_poplevel (scope));
+
+  for (int i = 0; i < 3; i++)
+    if (range_for_decl[i])
+      DECL_NAME (range_for_decl[i])
+       = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
 }
 
 /* Begin a range-for-statement.  Returns a new RANGE_FOR_STMT.
index 76f935a48adbcbab2ec765372f48a4599ad85966..16027a791a2c9a4dfd22c3bf008b0bd52971ccf4 100644 (file)
@@ -1,3 +1,9 @@
+2018-07-25  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/85515
+       * g++.dg/pr85515-2.C: Add expected dg-error.
+       * g++.dg/cpp0x/range-for36.C: New test.
+
 2018-07-25  Claudiu Zissulescu  <claziss@synopsys.com>
 
        * gcc.target/arc/uncached-1.c: New test.
diff --git a/gcc/testsuite/g++.dg/cpp0x/range-for36.C b/gcc/testsuite/g++.dg/cpp0x/range-for36.C
new file mode 100644 (file)
index 0000000..fdf7f7b
--- /dev/null
@@ -0,0 +1,32 @@
+// PR c++/85515
+// { dg-do compile { target c++11 } }
+
+int a[10];
+
+void
+foo ()
+{
+  for (auto &i : a)
+    if (i != *__for_begin              // { dg-error "was not declared in this scope" }
+       || &i == __for_end              // { dg-error "was not declared in this scope" }
+       || &__for_range[0] != &a[0])    // { dg-error "was not declared in this scope" }
+      __builtin_abort ();
+}
+
+template <int N>
+void
+bar ()
+{
+  for (auto &i : a)
+    if (i != *__for_begin              // { dg-error "was not declared in this scope" }
+       || &i == __for_end              // { dg-error "was not declared in this scope" }
+       || &__for_range[0] != &a[0])    // { dg-error "was not declared in this scope" }
+      __builtin_abort ();
+}
+
+void
+baz ()
+{
+  foo ();
+  bar <0> ();
+}
index 621ddb87636f9e446659fcc65e0bf43e71783916..2b761bf9b929804a6aae22a34246e051732613df 100644 (file)
@@ -15,8 +15,7 @@ int test_2 ()
   int sum = 0;
   for (const auto v: arr) {
     sum += v;
-    // TODO: should we issue an error for the following assignment?
-    __for_begin = __for_end;
+    __for_begin = __for_end;   // { dg-error "was not declared in this scope" }
   }
   return sum;
 }