From patchwork Fri Jun 2 15:15:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 70516 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 71C573858D3C for ; Fri, 2 Jun 2023 15:16:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 71C573858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685718969; bh=KIRuLi75Uclqllczvmef55yduNBkdwjkVqCBIp4Ib3U=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=AQRdXE6HFTmjgD24fJEDKoknqiT+s9v9GfQIHuCQ3VtNTJqweAJbey7FEjRyK4jQe DIynNoPU/w+3wmlv6dmhjH86FVJ9ReojXyPfcXveGp7uHpZRX3jkygwm6t3lV3Akg5 ia2YVSET38tJn9MffhXDkW5YXRNHey9Z2TfqSGf0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0B8983858D32 for ; Fri, 2 Jun 2023 15:15:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0B8983858D32 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-605-gjJZb4NmOTyA_nqZEOUFsw-1; Fri, 02 Jun 2023 11:15:39 -0400 X-MC-Unique: gjJZb4NmOTyA_nqZEOUFsw-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-75b1bc4a19aso257812385a.0 for ; Fri, 02 Jun 2023 08:15:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685718938; x=1688310938; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KIRuLi75Uclqllczvmef55yduNBkdwjkVqCBIp4Ib3U=; b=YyTUU7wEA60wVmHRHD76Kj8dT9LfhopOIP1V2oUG4dSMciyTq12jFxQj05I35TfBih sH1lTc98D66KLeiWwsY1flKrqfStlkDVlAr2DixTutplVFH7hmOgM9n2Q58OOFpwdCI/ 4o4USbKU4jyN+H/TDHtOoZsTQxm6Qh2fRN0krwxU3T/MacXMmHPfwMgkhY42mLzaqJqd x02AYqyqS1vWgz2zAQy9xhEcCuxMbONe3cREE75xdz7xApM5nkcqxaWUT+rKpQ+xgB/g yz9lsdtUtmy82PTzaCwtFqwgUUR32xgF2Fy3HtSO/V1jsNPYIfbYXoKqN+jTGjkjhAqX 1MjA== X-Gm-Message-State: AC+VfDwNEbK+8a7GZPvkR5vtBuDy4rV0bUWbveKKPC65WzdWqVzTwo2l EejRTq5dFQougG24Fhh8ysrFunBcbTQ9f9V565OvxH4u93UCJF1xABX6NvYRpNyg0iexuBFZH5p 79YJtfApxIdovAAb0L0rw37qDoj/uaTgPGe+n/jrQGlw78vxDGXlDbxJZ6d9kQMWa6hw4s9X56y A= X-Received: by 2002:a05:622a:24b:b0:3e6:3611:46a1 with SMTP id c11-20020a05622a024b00b003e6361146a1mr6255113qtx.9.1685718938602; Fri, 02 Jun 2023 08:15:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5wvPO6eXn2/lPCGElscZd//MCb0mBy5+n1ZadKxpytIsEhr95zL7A4om3OoOHANVeWibHQJw== X-Received: by 2002:a05:622a:24b:b0:3e6:3611:46a1 with SMTP id c11-20020a05622a024b00b003e6361146a1mr6255013qtx.9.1685718937513; Fri, 02 Jun 2023 08:15:37 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id jy7-20020a0562142b4700b006238ac6c3f3sm946634qvb.33.2023.06.02.08.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 08:15:37 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: replace in_template_function Date: Fri, 2 Jun 2023 11:15:34 -0400 Message-ID: <20230602151534.2132668-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.rc1.10.g9e49351c30 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- All uses of in_template_function besides the one in cp_make_fname_decl seem like they could be generalized to apply to all template contexts, not just function templates. To that end this patch replaces the predicate with a cheaper and more general in_template_context predicate that returns true for all template contexts. If we legitimately need to consider only function template contexts, as in cp_make_fname_decl, we can just additionallly check e.g. current_function_decl. One concrete benefit of this is that we no longer instantiate/odr-use entities based on uses within a non-function template such as in the adjusted testcase below. gcc/cp/ChangeLog: * class.cc (build_base_path): Check in_template_context instead of in_template_function. (resolves_to_fixed_type_p): Likewise. * cp-tree.h (in_template_context): Define. (in_template_function): Remove. * decl.cc (cp_make_fname_decl): Check current_function_decl and in_template_context instead of in_template_function. * decl2.cc (mark_used): Check in_template_context instead of in_template_function. * pt.cc (in_template_function): Remove. * semantics.cc (enforce_access): Check in_template_context instead of current_template_parms directly. gcc/testsuite/ChangeLog: * g++.dg/warn/Waddress-of-packed-member2.C: No longer expect a() to be marked as odr-used. --- gcc/cp/class.cc | 4 ++-- gcc/cp/cp-tree.h | 2 +- gcc/cp/decl.cc | 2 +- gcc/cp/decl2.cc | 2 +- gcc/cp/pt.cc | 19 ------------------- gcc/cp/semantics.cc | 2 +- .../g++.dg/warn/Waddress-of-packed-member2.C | 2 +- 7 files changed, 7 insertions(+), 26 deletions(-) diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index bc84f4f731a..778759237dc 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -344,7 +344,7 @@ build_base_path (enum tree_code code, bool uneval = (cp_unevaluated_operand != 0 || processing_template_decl - || in_template_function ()); + || in_template_context); /* For a non-pointer simple base reference, express it as a COMPONENT_REF without taking its address (and so causing lambda capture, 91933). */ @@ -8055,7 +8055,7 @@ resolves_to_fixed_type_p (tree instance, int* nonnull) /* processing_template_decl can be false in a template if we're in instantiate_non_dependent_expr, but we still want to suppress this check. */ - if (in_template_function ()) + if (in_template_context) { /* In a template we only care about the type of the result. */ if (nonnull) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index a1b882f11fe..ce2095c7aaa 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -1924,6 +1924,7 @@ extern GTY(()) struct saved_scope *scope_chain; #define current_template_parms scope_chain->template_parms #define current_template_depth \ (current_template_parms ? TMPL_PARMS_DEPTH (current_template_parms) : 0) +#define in_template_context (current_template_parms != NULL_TREE) #define processing_template_decl scope_chain->x_processing_template_decl #define processing_specialization scope_chain->x_processing_specialization @@ -7353,7 +7354,6 @@ extern tree lookup_template_variable (tree, tree); extern bool uses_template_parms (tree); extern bool uses_template_parms_level (tree, int); extern bool uses_outer_template_parms_in_constraints (tree); -extern bool in_template_function (void); extern bool need_generic_capture (void); extern tree instantiate_class_template (tree); extern tree instantiate_template (tree, tree, tsubst_flags_t); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index a672e4844f1..3985c6d2d1f 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -5021,7 +5021,7 @@ cp_make_fname_decl (location_t loc, tree id, int type_dep) tree domain = NULL_TREE; tree init = NULL_TREE; - if (!(type_dep && in_template_function ())) + if (!(type_dep && current_function_decl && in_template_context)) { const char *name = NULL; bool release_name = false; diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index b510cdac554..b402befba6d 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -5782,7 +5782,7 @@ mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */) && DECL_OMP_DECLARE_REDUCTION_P (decl))) maybe_instantiate_decl (decl); - if (processing_template_decl || in_template_function ()) + if (processing_template_decl || in_template_context) return true; /* Check this too in case we're within instantiate_non_dependent_expr. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 7fb3e75bceb..244b0b03454 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10951,25 +10951,6 @@ uses_template_parms (tree t) return instantiation_dependent_expression_p (t); } -/* Returns true iff we're processing an incompletely instantiated function - template. Useful instead of processing_template_decl because the latter - is set to 0 during instantiate_non_dependent_expr. */ - -bool -in_template_function (void) -{ - /* Inspect the less volatile cfun->decl instead of current_function_decl; - the latter might get set for e.g. access checking during satisfaction. */ - tree fn = cfun ? cfun->decl : NULL_TREE; - bool ret; - ++processing_template_decl; - ret = (fn && DECL_LANG_SPECIFIC (fn) - && DECL_TEMPLATE_INFO (fn) - && any_dependent_template_arguments_p (DECL_TI_ARGS (fn))); - --processing_template_decl; - return ret; -} - /* Returns true if T depends on any template parameter with level LEVEL. */ bool diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 05df6282302..0843b5eae73 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -346,7 +346,7 @@ enforce_access (tree basetype_path, tree decl, tree diag_decl, } tree cs = current_scope (); - if (current_template_parms + if (in_template_context && (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)) if (tree template_info = get_template_info (cs)) { diff --git a/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C b/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C index e9bf7cac04c..d619b28cfe1 100644 --- a/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C +++ b/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C @@ -1,7 +1,7 @@ // PR c++/89973 // { dg-do compile { target c++14 } } -constexpr int a(); // { dg-warning "used but never defined" } +constexpr int a(); template constexpr void *b = a(); // { dg-error "invalid conversion" }