From patchwork Wed Jul 18 10:19:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 945655 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-481793-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="QO+GW8Qd"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41VtS72WBDz9s3R for ; Wed, 18 Jul 2018 20:19:50 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=pkfQX4IER9lWEdmsY luiDAvGVfjaAINXDeUv9SwYf3JkCsxzlJT7IvnW680wckC1yuH3myzIlOHsCivWB Sgq5CyOR4RdmmaTNlxaHih+IlxEKq4LK+K9xs3ZifpImv/DrG3ZWRYm2TFz/f1Sp ClWtSn325dil7vKBvxgParpInc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; s=default; bh=r9nwM/BOi1PR+gdCFsZXaT8 eD5g=; b=QO+GW8QdQGc/lF2uZxP6LFudkdwrveKXNS78lDtALUoGhrKthpA3XI3 SFsG58aPsDF55cj/Ve/hPxecmgKK+PA/k8C6ytMwIGaeKQ+l+VhdVVr9HAFcEKB6 CMqy0/9r9pd98CXOaCFMAuXbKq8p9wptCb5HioAEUjW125w80LyQ= Received: (qmail 33154 invoked by alias); 18 Jul 2018 10:19:41 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 33131 invoked by uid 89); 18 Jul 2018 10:19:40 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.9 required=5.0 tests=BAYES_00, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=30pm, 30PM, fes, cptreeh X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Jul 2018 10:19:37 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BA93C3A9E2; Wed, 18 Jul 2018 10:19:36 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-117-71.ams2.redhat.com [10.36.117.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 029215CC0F; Wed, 18 Jul 2018 10:19:35 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id w6IAJX2x020069; Wed, 18 Jul 2018 12:19:34 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id w6IAJVKk020068; Wed, 18 Jul 2018 12:19:31 +0200 Date: Wed, 18 Jul 2018 12:19:31 +0200 From: Jakub Jelinek To: Nathan Sidwell Cc: Jason Merrill , gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Hide __for_{range, begin, end} symbols (PR c++/85515, take 2) Message-ID: <20180718101931.GX7166@tucnak> Reply-To: Jakub Jelinek References: <20180713134935.GV7166@tucnak> <7802d658-cc0f-664d-9261-d2e9388047a4@acm.org> <20180713165330.GW7166@tucnak> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20180713165330.GW7166@tucnak> User-Agent: Mutt/1.9.2 (2017-12-15) X-IsSubscribed: yes On Fri, Jul 13, 2018 at 06:53:30PM +0200, Jakub Jelinek wrote: > On Fri, Jul 13, 2018 at 12:24:02PM -0400, Nathan Sidwell wrote: > > On 07/13/2018 09:49 AM, Jakub Jelinek wrote: > > > Hi! > > > > > > I'd like to ping the following C++ patches: > > > > > > - PR c++/85515 > > > make range for temporaries unspellable during parsing and only > > > turn them into spellable for debug info purposes > > > http://gcc.gnu.org/ml/gcc-patches/2018-07/msg00086.html > > > > > > How hard would it be to add the 6 special identifiers to the C++ global > > table via initialize_predefined_identifiers (decl.c) and then use them > > directly in the for range machinery? repeated get_identifier > > ("string-const") just smells bad. > > Probably not too hard, but we have hundreds of other > get_identifier ("string-const") calls in the middle-end, C++ FE, other FEs. > Are those 6 more important than say "abi_tag", "gnu", "begin", "end", "get", > "tuple_size", "tuple_element", and many others? > > Is it worth caching those? Anyway, here is an updated patch that uses the get_identifier caching. Ok for trunk? Shall I submit an incremental patch for the "abi_tag", "gnu", "begin", "end", "get", "tuple_size", "tuple_element" etc. identifiers? 2018-07-18 Jakub Jelinek 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. Jakub --- gcc/cp/cp-tree.h.jj 2018-06-29 09:38:17.790306399 +0200 +++ gcc/cp/cp-tree.h 2018-07-18 11:57:55.980529748 +0200 @@ -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 #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] --- gcc/cp/decl.c.jj 2018-07-12 21:34:44.798598796 +0200 +++ gcc/cp/decl.c 2018-07-18 11:59:06.220595473 +0200 @@ -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} }; --- gcc/cp/parser.c.jj 2018-07-18 10:09:10.655030931 +0200 +++ gcc/cp/parser.c 2018-07-18 12:00:22.907667232 +0200 @@ -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, tr } /* 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, tr 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); --- gcc/cp/semantics.c.jj 2018-07-10 09:11:24.254062152 +0200 +++ gcc/cp/semantics.c 2018-07-18 12:03:24.482837137 +0200 @@ -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. --- gcc/testsuite/g++.dg/pr85515-2.C.jj 2018-07-05 11:41:51.439718304 +0200 +++ gcc/testsuite/g++.dg/pr85515-2.C 2018-07-18 11:27:36.452850086 +0200 @@ -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; } --- gcc/testsuite/g++.dg/cpp0x/range-for36.C.jj 2018-07-18 11:27:36.452850086 +0200 +++ gcc/testsuite/g++.dg/cpp0x/range-for36.C 2018-07-18 11:27:36.452850086 +0200 @@ -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 +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> (); +}