From: Volker Reichelt Date: Mon, 5 Dec 2005 13:12:29 +0000 (+0000) Subject: Backport: X-Git-Tag: releases/gcc-3.4.6~271 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df9ed971efc2335933f84e3b09b061011be7b44e;p=thirdparty%2Fgcc.git Backport: 2005-10-13 Mark Mitchell PR c++/22464 * semantics.c (finish_id_expression): Issue errors about uses of local variables in containing functions even in templates. * g++.dg/template/crash41.C: New test. From-SVN: r108055 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 6a78fa502d30..ae8cad7bf6ba 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2005-12-05 Volker Reichelt + + Backport: + 2005-10-13 Mark Mitchell + + PR c++/22464 + * semantics.c (finish_id_expression): Issue errors about uses of + local variables in containing functions even in templates. + 2005-12-05 Volker Reichelt Backport: diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 55984b75ade2..943e7a1a92a6 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2400,6 +2400,21 @@ finish_id_expression (tree id_expression, was entirely defined. */ if (!scope && decl != error_mark_node) maybe_note_name_used_in_class (id_expression, decl); + + /* Disallow uses of local variables from containing functions. */ + if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL) + { + tree context = decl_function_context (decl); + if (context != NULL_TREE && context != current_function_decl + && ! TREE_STATIC (decl)) + { + error (TREE_CODE (decl) == VAR_DECL + ? "use of `auto' variable from containing function" + : "use of parameter from containing function"); + cp_error_at (" `%#D' declared here", decl); + return error_mark_node; + } + } } /* If we didn't find anything, or what we found was a type, @@ -2666,23 +2681,6 @@ finish_id_expression (tree id_expression, } else { - if (TREE_CODE (decl) == VAR_DECL - || TREE_CODE (decl) == PARM_DECL - || TREE_CODE (decl) == RESULT_DECL) - { - tree context = decl_function_context (decl); - - if (context != NULL_TREE && context != current_function_decl - && ! TREE_STATIC (decl)) - { - error ("use of %s from containing function", - (TREE_CODE (decl) == VAR_DECL - ? "`auto' variable" : "parameter")); - cp_error_at (" `%#D' declared here", decl); - return error_mark_node; - } - } - if (DECL_P (decl) && DECL_NONLOCAL (decl) && DECL_CLASS_SCOPE_P (decl) && DECL_CONTEXT (decl) != current_class_type) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6d38f7e7025f..7063568386a5 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2005-12-05 Volker Reichelt + + Backport: + 2005-10-13 Mark Mitchell + + PR c++/22464 + * g++.dg/template/crash41.C: New test. + 2005-12-05 Volker Reichelt Backport: diff --git a/gcc/testsuite/g++.dg/template/crash41.C b/gcc/testsuite/g++.dg/template/crash41.C new file mode 100644 index 000000000000..2b8028e43107 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/crash41.C @@ -0,0 +1,15 @@ +// PR c++/22464 + +template +void do_something(const T* A) { // { dg-error "declared" } + struct helper_t { + helper_t() { + A[0]; // { dg-error "use" } + } + } helper; +} + +void sub1() { + double A[7]; + do_something (A); +}