From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by sourceware.org (Postfix) with ESMTPS id 5C773385B50B for ; Tue, 7 May 2024 08:00:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5C773385B50B Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5C773385B50B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715068822; cv=none; b=pl+t7yaXoPSnWcsvpYSjTOio8LWnwbap/jM676Aes2L944WnSNXFv57nEQT1JoS5rSKj4eavlje9gJgMxgrYUcTwUaFO9acDjgGKkt6gezhOuSYdjGkz1ZqdxWLYtZYlKldjiXaDMo+J2D6NnnEa6eW6lpBeeK0aSeTSDcNvSUA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715068822; c=relaxed/simple; bh=XG3pHfBLTV8UD4lYOdirMmR9fMLvIwhT/vU4eUegCVA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Fnax5AW6ys7Msthsup8qmxyZRVMj9EETYDLIEoEy3gqfUu0DpJHzZ075dXe7xQ1bvGxdlutZZnFdznduyZRG3js+UsnhBlXoGELsRW6TQMIxKTDNusRglYBvdjn13TCiIAUXLGg5/2JWYHt6gxLk6TdGYI4bHB7LhaxtlLmTDqM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-41b5e74fa83so21121795e9.0 for ; Tue, 07 May 2024 01:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1715068815; x=1715673615; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=TBbzIs3K8ATclMHmpkLjmrZrcBgLnNGQKxAby6+o5mY=; b=VvNrUGq2R2cPBS1H4wB+zicgY/Q2BI1kRjQ3se0DCA9sg4M63ENgHF3JBpm5VOs4Dx 876V/FiXkuSgUM9B0j/gKqtPxgg+m/irn6K0PL/czzunuJsZVUKbJvSpgTuj8ZetxIO7 +aeFNzbdLxUp9NWk+8KPy01dmP91fcfATUC01h8NAlO/5S0wTZj8WFrO8IgI8np3RUXI 71r5ZU3+E5EINnnSAdyyRriOPS9/vzJ2yO3eUPn9KlyXHbQ/HPbRqGKz8+QmgF1EcLMt 07R+49XUY5ZrLt9Zo1GtV3QiZuAFA+Qj3tkeK4fhEfz0aWEkFOaBNLwRkiVo8b2htSGR 3J0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715068815; x=1715673615; 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=TBbzIs3K8ATclMHmpkLjmrZrcBgLnNGQKxAby6+o5mY=; b=MQzlYxA6vM3lb8W9kR4vcFiHJpHXwAihNvJh+k59VdbIHkDIWBrRB2L2YtNRrb52vP uZSjZbf9L9UB9yS4MeGHkkU76kpWVImoaRK5oo/fQ/lMhtup/TQ/IvTVrRPW1gqsj4U9 gn8Bmu7dq5dcH+2QOm78befObLvxexUyT5F0JL5XrSjoLhav+cvKsqpBDnzIWEQLwE+O MfnRoiGpY91a1Y8E0hYB0w6DnK2JOQeNg5bHOO4aHlYepQBYeF9eUgrnRbr6BQ4qRgB7 SxojGUvoI3ATfAtOIil5iXb9a/Si9sH0YndAJef9+D+JRL8CZ+ph2A6wXbqY875KeFH0 jC2A== X-Gm-Message-State: AOJu0YydYrJWAQZMcNz0GHr7obsInlY3MCv3p1aCs+ILd0P96jllrnGw LwimvIjbCi5C0UhYn0DVCRYosZzrjBWax94BZubyxqJHgXXDIfjWE6BONQO9lBAH9DgbMT6d+8A = X-Google-Smtp-Source: AGHT+IF4HcWoUOHyUCjGIk6zX1PNhW/fXsnGMPdshCWQKN0SUMBs929ydKaTKEdBRxhWCXfdA7xTeA== X-Received: by 2002:a05:600c:3b20:b0:41b:e55c:8e14 with SMTP id m32-20020a05600c3b2000b0041be55c8e14mr8893117wms.14.1715068815017; Tue, 07 May 2024 01:00:15 -0700 (PDT) Received: from localhost.localdomain ([2001:861:3382:1a90:11a6:1c9d:5099:b1e8]) by smtp.gmail.com with ESMTPSA id je16-20020a05600c1f9000b0041c7ac6b0ffsm22489700wmb.37.2024.05.07.01.00.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 01:00:14 -0700 (PDT) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Steve Baird Subject: [COMMITTED] ada: Improve pragma No_Return's pre-Ada2022 handling of functions Date: Tue, 7 May 2024 10:00:13 +0200 Message-ID: <20240507080013.37218-1-poulhies@adacore.com> X-Mailer: git-send-email 2.43.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Steve Baird Ada 2022 allows pragma No_Return to apply to a function (or a generic function). For earlier Ada versions, if a No_Return pragma argument's possible resolutions include a function (or a generic function) then we want to ignore that candidate if a non-function candidate is also available and otherwise to generate an error message mentioning that this is an Ada 2022 feature. gcc/ada/ * sem_prag.adb (Analyze_Pragma): Restructure the loop over possible resolutions of a No_Return pragma's argument so that functions (and generic functions) are not processed until after it is known whether there is a non-function candidate resolution. For a pre-2022 Ada version, terminate the iteration before processing functions if a non-function resolution is found. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/sem_prag.adb | 166 +++++++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 69 deletions(-) diff --git a/gcc/ada/sem_prag.adb b/gcc/ada/sem_prag.adb index a2996137648..ff02ae9a7af 100644 --- a/gcc/ada/sem_prag.adb +++ b/gcc/ada/sem_prag.adb @@ -20771,96 +20771,124 @@ package body Sem_Prag is raise Pragma_Exit; end if; + Found := False; -- Loop to find matching procedures or functions (Ada 2022) - E := Entity (Id); + Outer_Loop : + for Process_Functions in Boolean loop - Found := False; - while Present (E) - and then Scope (E) = Current_Scope - loop - -- Ada 2022 (AI12-0269): A function can be No_Return + -- We make two passes over the Homonym list, first looking + -- at procedures and then at functions. This is done + -- in order to get the desired behavior in the pre-Ada2022 + -- case. There are two subcases of the pre-Ada2022 case - + -- either we found a non-function candidate in the first + -- pass or we didn't. If we found one, then exit early + -- (i.e., skip the second pass); we want to silently ignore + -- any functions. But if we didn't find one then we do not + -- want to exit early because looking at functions will + -- allow us (if we find one) to generate a more useful + -- error message ("this is an Ada 2022 construct" instead of + -- "name could not be resolved"). - if Ekind (E) in E_Generic_Procedure | E_Procedure - | E_Generic_Function | E_Function - then - -- Check that the pragma is not applied to a body. - -- First check the specless body case, to give a - -- different error message. These checks do not apply - -- if Relaxed_RM_Semantics, to accommodate other Ada - -- compilers. Disable these checks under -gnatd.J. - - if not Debug_Flag_Dot_JJ then - if Nkind (Parent (Declaration_Node (E))) = - N_Subprogram_Body - and then not Relaxed_RM_Semantics - then - Error_Pragma - ("pragma% requires separate spec and must come " - & "before body"); - end if; + exit Outer_Loop when Found and Ada_Version < Ada_2022; - -- Now the "specful" body case + E := Entity (Id); - if Rep_Item_Too_Late (E, N) then - raise Pragma_Exit; - end if; - end if; + while Present (E) + and then Scope (E) = Current_Scope + loop + -- Ada 2022 (AI12-0269): A function can be No_Return - if Check_No_Return (E, N) then - Set_No_Return (E); - end if; + if (if Process_Functions + then Ekind (E) in E_Generic_Function | E_Function + else Ekind (E) in E_Generic_Procedure | E_Procedure) - -- A pragma that applies to a Ghost entity becomes Ghost - -- for the purposes of legality checks and removal of - -- ignored Ghost code. + -- if From_Aspect_Specification, then only one + -- candidate should be considered. - Mark_Ghost_Pragma (N, E); + and then (not From_Aspect_Specification (N) + or else E = Entity (Id) + or else No (Entity (Id))) - -- Capture the entity of the first Ghost procedure being - -- processed for error detection purposes. + then + -- Check that the pragma is not applied to a body. + -- First check the specless body case, to give a + -- different error message. These checks do not apply + -- if Relaxed_RM_Semantics, to accommodate other Ada + -- compilers. Disable these checks under -gnatd.J. + + if not Debug_Flag_Dot_JJ then + if Nkind (Parent (Declaration_Node (E))) = + N_Subprogram_Body + and then not Relaxed_RM_Semantics + then + Error_Pragma + ("pragma% requires separate spec and must " + & "come before body"); + end if; - if Is_Ghost_Entity (E) then - if No (Ghost_Id) then - Ghost_Id := E; + -- Now the "specful" body case + + if Rep_Item_Too_Late (E, N) then + raise Pragma_Exit; + end if; end if; - -- Otherwise the subprogram is non-Ghost. It is illegal - -- to mix references to Ghost and non-Ghost entities - -- (SPARK RM 6.9). + if Check_No_Return (E, N) then + Set_No_Return (E); + end if; - elsif Present (Ghost_Id) - and then not Ghost_Error_Posted - then - Ghost_Error_Posted := True; + -- A pragma that applies to a Ghost entity becomes + -- Ghost for the purposes of legality checks and + -- removal of ignored Ghost code. - Error_Msg_Name_1 := Pname; - Error_Msg_N - ("pragma % cannot mention ghost and non-ghost " - & "procedures", N); + Mark_Ghost_Pragma (N, E); - Error_Msg_Sloc := Sloc (Ghost_Id); - Error_Msg_NE ("\& # declared as ghost", N, Ghost_Id); + -- Capture the entity of the first Ghost procedure + -- being processed for error detection purposes. - Error_Msg_Sloc := Sloc (E); - Error_Msg_NE ("\& # declared as non-ghost", N, E); - end if; + if Is_Ghost_Entity (E) then + if No (Ghost_Id) then + Ghost_Id := E; + end if; - -- Set flag on any alias as well + -- Otherwise the subprogram is non-Ghost. It is + -- illegal to mix references to Ghost and non-Ghost + -- entities (SPARK RM 6.9). - if Is_Overloadable (E) - and then Present (Alias (E)) - and then Check_No_Return (Alias (E), N) - then - Set_No_Return (Alias (E)); - end if; + elsif Present (Ghost_Id) + and then not Ghost_Error_Posted + then + Ghost_Error_Posted := True; - Found := True; - end if; + Error_Msg_Name_1 := Pname; + Error_Msg_N + ("pragma % cannot mention ghost and non-ghost " + & "procedures", N); - exit when From_Aspect_Specification (N); - E := Homonym (E); - end loop; + Error_Msg_Sloc := Sloc (Ghost_Id); + Error_Msg_NE + ("\& # declared as ghost", N, Ghost_Id); + + Error_Msg_Sloc := Sloc (E); + Error_Msg_NE ("\& # declared as non-ghost", N, E); + end if; + + -- Set flag on any alias as well + + if Is_Overloadable (E) + and then Present (Alias (E)) + and then Check_No_Return (Alias (E), N) + then + Set_No_Return (Alias (E)); + end if; + + Found := True; + end if; + + E := Homonym (E); + end loop; + end loop Outer_Loop; -- If entity in not in current scope it may be the enclosing -- subprogram body to which the aspect applies. -- 2.43.2