From patchwork Wed Oct 20 19:27:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1544022 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=RWaVl8EP; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HZLR62bWDz9sPB for ; Thu, 21 Oct 2021 06:34:42 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 747773857C52 for ; Wed, 20 Oct 2021 19:34:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 747773857C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634758479; bh=1ERQIlNXMIla/d/Vvd9mFzpU/ySlFqVqXcOuJx7PcYE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=RWaVl8EPIwSFuVw7p2pFCU9rsBXpLp3YK1IxnLlvgaTz5MML1HOXBAyAlV8opNvV4 hA1oFa2O6Gr4MWvgs3ZoVBCOoU42cstZy0rB94S1vFJ5a2DpR/LjCAGoz+wzxq392e bLt1Wt3basqw2l3HuY0RgM6bxin9b3lLGxgPkalQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by sourceware.org (Postfix) with ESMTPS id 6F8F63858437 for ; Wed, 20 Oct 2021 19:27:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6F8F63858437 Received: by mail-lj1-x22b.google.com with SMTP id n7so147965ljp.5 for ; Wed, 20 Oct 2021 12:27:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=1ERQIlNXMIla/d/Vvd9mFzpU/ySlFqVqXcOuJx7PcYE=; b=cDlhEuTrTyj8/MyGk+Zb+BKIajidxQDhK4f3UMTtDSui3NjWZmJgvB+gR+HedfzvJg 712VNphNMown4Vyhp5MIndfDg/xiSfZ8YL3sKco0ew30bzUmYb6tJ4OA2npCJGADaZ5p k/7Hzy1hzYm8p2ewb84BuYOmtoPcPih5swIUBBCY8pc59yGECY0FaRt/1kDJARGUNs/F R8Z65rDtdHZeeJgaZraS2PzucSOnTBaFhW3tErS6nKy45pWoXJoKdIY4miMEqjgPr0p+ gxHquBwxDjq4GfIqDdLDFEyXBKoWNHphpx0rvrygkdsu3YZLYlt+i7LqLMmoUnsehEvO SP0Q== X-Gm-Message-State: AOAM530Bwhfuko3o7OI0uhDoTF7P55DG6LP75R//seC5wKNoRIJuvUrY STDtEcwNqKrU4XLubKgEVSnLO18OAq4EWmJ9 X-Google-Smtp-Source: ABdhPJx9OkQWzdbhHWgj96VmcuzFXn6akTxuB3vhvG6zyUXzgnjmvtw30CMWqOEtgQI8b+KKhRT+HA== X-Received: by 2002:a2e:320f:: with SMTP id y15mr1093305ljy.286.1634758063296; Wed, 20 Oct 2021 12:27:43 -0700 (PDT) Received: from adacore.com ([2a02:2ab8:224:2ce:72b5:e8ff:feef:ee60]) by smtp.gmail.com with ESMTPSA id z12sm262026lfs.101.2021.10.20.12.27.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 12:27:41 -0700 (PDT) Date: Wed, 20 Oct 2021 19:27:40 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Warning on nonmatching subtypes in fully conforming subprogram specs and bodies Message-ID: <20211020192740.GA3154181@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-13.0 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Gary Dismukes Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" When corresponding parameter subtypes or result subtypes denote different declarations between the declaration and body of a subprogram, but those are fully conforming, a warning will be issued indicating that the subtypes come from different declarations. In the case of anonymous access subtypes, the designated subtypes are checked. The warning is conditioned on the switch -gnatw_p (for "pedantic checks"), introduced as part of these changes. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_ch6.adb: Add with and use of Warnsw. (Check_Conformance): Report a warning when subtypes or designated subtypes of formal parameters or result subtypes denote different declarations between the spec and body of the (Subprogram_Subtypes_Have_Same_Declaration): New function nested within Check_Conformance that determines whether two subtype entities referenced in a subprogram come from the same declaration. Returns True immediately if the subprogram is in a generic instantiation, or the subprogram is marked Is_Internal or is declared in an internal (GNAT library) unit, or GNAT_Mode is enabled, otherwise compares the nonlimited views of the entities (or their designated subtypes' nonlimited views in the anonymous access cases). (Nonlimited_View_Of_Subtype): New function nested within function Subprogram_Subtypes_Have_Same_Declaration that returns Non_Limited_View of a type or subtype that is an incomplete or class-wide type that comes from a limited of a package (From_Limited_With is True for the entity), or returns Full_View when the nonlimited view is an incomplete type. Otherwise returns the entity passed in. * warnsw.ads (Warn_On_Pedantic_Checks): New warning flag. (type Warning_Record): New component Warn_On_Pedantic_Checks. * warnsw.adb (All_Warnings): Set Warn_On_Pedantic_Checks from parameter Setting. (Restore_Warnings): Restore the value of the Warn_On_Pedantic_Checks flag. (Save_Warnings): Save the value of the Warn_On_Pedantic_Checks flag. (Set_Underscore_Warning_Switch): Add settings of the Warn_On_Pedantic flag according to the switch ("-gnatw_p" vs. "-gnatw_P"). * doc/gnat_ugn/building_executable_programs_with_gnat.rst: Add documentation of new switches -gnatw_p and -gnatw_P (warnings for pedantic checks). * gnat_ugn.texi: Regenerate. * usage.adb: Add Warn_On_Pedantic_Checks. diff --git a/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst b/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst --- a/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst +++ b/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst @@ -3582,6 +3582,25 @@ of the pragma in the :title:`GNAT_Reference_manual`). ordering. +.. index:: -gnatw_p (gcc) + +:switch:`-gnatw_p` + *Activate warnings for pedantic checks.* + + This switch activates warnings for the failure of certain pedantic checks. + The only case currently supported is a check that the subtype_marks given + for corresponding formal parameter and function results in a subprogram + declaration and its body denote the same subtype declaration. The default + is that such warnings are not given. + +.. index:: -gnatw_P (gcc) + +:switch:`-gnatw_P` + *Suppress warnings for pedantic checks.* + + This switch suppresses warnings on violations of pedantic checks. + + .. index:: -gnatwq (gcc) .. index:: Parentheses, warnings diff --git a/gcc/ada/gnat_ugn.texi b/gcc/ada/gnat_ugn.texi --- a/gcc/ada/gnat_ugn.texi +++ b/gcc/ada/gnat_ugn.texi @@ -21,7 +21,7 @@ @copying @quotation -GNAT User's Guide for Native Platforms , Sep 28, 2021 +GNAT User's Guide for Native Platforms , Oct 19, 2021 AdaCore @@ -11800,6 +11800,34 @@ This switch suppresses warnings on cases of suspicious parameter ordering. @end table +@geindex -gnatw_p (gcc) + + +@table @asis + +@item @code{-gnatw_p} + +@emph{Activate warnings for pedantic checks.} + +This switch activates warnings for the failure of certain pedantic checks. +The only case currently supported is a check that the subtype_marks given +for corresponding formal parameter and function results in a subprogram +declaration and its body denote the same subtype declaration. The default +is that such warnings are not given. +@end table + +@geindex -gnatw_P (gcc) + + +@table @asis + +@item @code{-gnatw_P} + +@emph{Suppress warnings for pedantic checks.} + +This switch suppresses warnings on violations of pedantic checks. +@end table + @geindex -gnatwq (gcc) @geindex Parentheses diff --git a/gcc/ada/sem_ch6.adb b/gcc/ada/sem_ch6.adb --- a/gcc/ada/sem_ch6.adb +++ b/gcc/ada/sem_ch6.adb @@ -90,6 +90,7 @@ with Tbuild; use Tbuild; with Uintp; use Uintp; with Urealp; use Urealp; with Validsw; use Validsw; +with Warnsw; use Warnsw; package body Sem_Ch6 is @@ -5962,6 +5963,17 @@ package body Sem_Ch6 is -- True if the null exclusions of two formals of anonymous access type -- match. + function Subprogram_Subtypes_Have_Same_Declaration + (Subp : Entity_Id; + Decl_Subtype : Entity_Id; + Body_Subtype : Entity_Id) return Boolean; + -- Checks whether corresponding subtypes named within a subprogram + -- declaration and body originate from the same declaration, and returns + -- True when they do. In the case of anonymous access-to-object types, + -- checks the designated types. Also returns True when GNAT_Mode is + -- enabled, or when the subprogram is marked Is_Internal or occurs + -- within a generic instantiation or internal unit (GNAT library unit). + ----------------------- -- Conformance_Error -- ----------------------- @@ -6094,6 +6106,86 @@ package body Sem_Ch6 is end if; end Null_Exclusions_Match; + function Subprogram_Subtypes_Have_Same_Declaration + (Subp : Entity_Id; + Decl_Subtype : Entity_Id; + Body_Subtype : Entity_Id) return Boolean + is + + function Nonlimited_View_Of_Subtype + (Subt : Entity_Id) return Entity_Id; + -- Returns the nonlimited view of a type or subtype that is an + -- incomplete or class-wide type that comes from a limited view of + -- a package (From_Limited_With is True for the entity), or the + -- full view when the subtype is an incomplete type. Otherwise + -- returns the entity passed in. + + function Nonlimited_View_Of_Subtype + (Subt : Entity_Id) return Entity_Id + is + Subt_Temp : Entity_Id := Subt; + begin + if Ekind (Subt) in Incomplete_Kind | E_Class_Wide_Type + and then From_Limited_With (Subt) + then + Subt_Temp := Non_Limited_View (Subt); + end if; + + -- If the subtype is incomplete, return full view if present + -- (and accounts for the case where a type from a limited view + -- is itself an incomplete type). + + if Ekind (Subt_Temp) in Incomplete_Kind + and then Present (Full_View (Subt_Temp)) + then + Subt_Temp := Full_View (Subt_Temp); + end if; + + return Subt_Temp; + end Nonlimited_View_Of_Subtype; + + -- Start of processing for Subprogram_Subtypes_Have_Same_Declaration + + begin + if not In_Instance + and then not In_Internal_Unit (Subp) + and then not Is_Internal (Subp) + and then not GNAT_Mode + and then + Ekind (Etype (Decl_Subtype)) not in Access_Subprogram_Kind + then + if Ekind (Etype (Decl_Subtype)) = E_Anonymous_Access_Type then + if Nonlimited_View_Of_Subtype (Designated_Type (Decl_Subtype)) + /= Nonlimited_View_Of_Subtype (Designated_Type (Body_Subtype)) + then + return False; + end if; + + elsif Nonlimited_View_Of_Subtype (Decl_Subtype) + /= Nonlimited_View_Of_Subtype (Body_Subtype) + then + -- Avoid returning False (and a false-positive warning) for + -- the case of "not null" itypes, which will appear to be + -- different subtypes even when the subtype_marks denote + -- the same subtype. + + if Ekind (Decl_Subtype) = E_Access_Subtype + and then Ekind (Body_Subtype) = E_Access_Subtype + and then Is_Itype (Body_Subtype) + and then Can_Never_Be_Null (Body_Subtype) + and then Etype (Decl_Subtype) = Etype (Body_Subtype) + then + return True; + + else + return False; + end if; + end if; + end if; + + return True; + end Subprogram_Subtypes_Have_Same_Declaration; + -- Local Variables Old_Type : constant Entity_Id := Etype (Old_Id); @@ -6147,6 +6239,18 @@ package body Sem_Ch6 is end if; return; + + -- If the result subtypes conform and pedantic checks are enabled, + -- check to see whether the subtypes originate from different + -- declarations, and issue a warning when they do. + + elsif Ctype = Fully_Conformant + and then Warn_On_Pedantic_Checks + and then not Subprogram_Subtypes_Have_Same_Declaration + (Old_Id, Old_Type, New_Type) + then + Error_Msg_N ("result subtypes conform but come from different " + & "declarations??", New_Id); end if; -- Ada 2005 (AI-231): In case of anonymous access types check the @@ -6343,6 +6447,18 @@ package body Sem_Ch6 is end if; return; + + -- If the formals' subtypes conform and pedantic checks are enabled, + -- check to see whether the subtypes originate from different + -- declarations, and issue a warning when they do. + + elsif Ctype = Fully_Conformant + and then Warn_On_Pedantic_Checks + and then not Subprogram_Subtypes_Have_Same_Declaration + (Old_Id, Old_Formal_Base, New_Formal_Base) + then + Error_Msg_N ("formal subtypes conform but come from " + & "different declarations??", New_Formal); end if; -- For mode conformance, mode must match diff --git a/gcc/ada/usage.adb b/gcc/ada/usage.adb --- a/gcc/ada/usage.adb +++ b/gcc/ada/usage.adb @@ -557,6 +557,8 @@ begin "order"); Write_Line (" .P* turn off warnings for suspicious parameter " & "order"); + Write_Line (" _p turn on warnings for pedantic checks"); + Write_Line (" _P turn off warnings for pedantic checks"); Write_Line (" q*+ turn on warnings for questionable " & "missing parenthesis"); Write_Line (" Q turn off warnings for questionable " & diff --git a/gcc/ada/warnsw.adb b/gcc/ada/warnsw.adb --- a/gcc/ada/warnsw.adb +++ b/gcc/ada/warnsw.adb @@ -76,6 +76,7 @@ package body Warnsw is Warn_On_Overlap := Setting; Warn_On_Overridden_Size := Setting; Warn_On_Parameter_Order := Setting; + Warn_On_Pedantic_Checks := Setting; Warn_On_Questionable_Layout := Setting; Warn_On_Questionable_Missing_Parens := Setting; Warn_On_Record_Holes := Setting; @@ -172,6 +173,8 @@ package body Warnsw is W.Warn_On_Overridden_Size; Warn_On_Parameter_Order := W.Warn_On_Parameter_Order; + Warn_On_Pedantic_Checks := + W.Warn_On_Pedantic_Checks; Warn_On_Questionable_Layout := W.Warn_On_Questionable_Layout; Warn_On_Questionable_Missing_Parens := @@ -284,6 +287,8 @@ package body Warnsw is Warn_On_Overridden_Size; W.Warn_On_Parameter_Order := Warn_On_Parameter_Order; + W.Warn_On_Pedantic_Checks := + Warn_On_Pedantic_Checks; W.Warn_On_Questionable_Layout := Warn_On_Questionable_Layout; W.Warn_On_Questionable_Missing_Parens := @@ -505,6 +510,12 @@ package body Warnsw is when 'C' => Warn_On_Unknown_Compile_Time_Warning := False; + when 'p' => + Warn_On_Pedantic_Checks := True; + + when 'P' => + Warn_On_Pedantic_Checks := False; + when 'r' => Warn_On_Component_Order := True; diff --git a/gcc/ada/warnsw.ads b/gcc/ada/warnsw.ads --- a/gcc/ada/warnsw.ads +++ b/gcc/ada/warnsw.ads @@ -58,6 +58,13 @@ package Warnsw is -- set with an explicit size clause. Off by default, modified by use of -- -gnatw.s/.S (but not -gnatwa). + Warn_On_Pedantic_Checks : Boolean := False; + -- Warn for violation of miscellaneous pedantic rules (such as when the + -- subtype of a formal parameter given in a subprogram body's specification + -- comes from a different subtype declaration that the subtype of the + -- formal in the subprogram declaration). Off by default, and set by + -- -gnatw_p (but not -gnatwa). + Warn_On_Questionable_Layout : Boolean := False; -- Warn when default layout of a record type is questionable for run-time -- efficiency reasons and would be improved by reordering the components. @@ -128,6 +135,7 @@ package Warnsw is Warn_On_Overlap : Boolean; Warn_On_Overridden_Size : Boolean; Warn_On_Parameter_Order : Boolean; + Warn_On_Pedantic_Checks : Boolean; Warn_On_Questionable_Layout : Boolean; Warn_On_Questionable_Missing_Parens : Boolean; Warn_On_Record_Holes : Boolean;