From patchwork Tue Aug 20 09:51:21 2019 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: 1150006 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-507361-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="IiaiwKpM"; 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 46CR4m0f1bz9sBF for ; Tue, 20 Aug 2019 19:55:51 +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:mime-version:content-type; q=dns; s=default; b=tOC1Ht8DZWUMj06JOV2s7xuX33kzNjjv5+45Z/BxgomCke/z/N 602Ho2nT+cV1m66WzSw9Zvz/FpRHScmewiA1ZBUyJq82ci6TeG6zhnyQsVv5RddD TEEZ1AbZSvGhzBTNSvaK3D6iM7b9nLa+aHHtZYgBjUpz3UGDl2+rttW/g= 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:mime-version:content-type; s= default; bh=3uk5a0o8mk5wdEcFxWCHTRnmrDE=; b=IiaiwKpMGjbaR0uqyTgT WEGHx99tJJ6tvN5hHZ/plTI5Y2Ly6eYzn25hvONwv5OD5hyorz8ettojMSq7VezT Af/CB2fqonlp6n1MzAHMt5QwT28VMoPFCUR0pMkst1LHnKbmOeRYX08cQpWdg1ms PNFWSdE2/DOKlUmJl7DOjRU= Received: (qmail 125189 invoked by alias); 20 Aug 2019 09:51:55 -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 122693 invoked by uid 89); 20 Aug 2019 09:51:35 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-12.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, SPF_NEUTRAL autolearn=ham version=3.3.1 spammy=Activate, increasing, ooo, gnatwa X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (209.51.188.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 20 Aug 2019 09:51:31 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i00nX-0005LD-D3 for gcc-patches@gcc.gnu.org; Tue, 20 Aug 2019 05:51:29 -0400 Received: from rock.gnat.com ([205.232.38.15]:45428) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i00nX-0005IF-7V for gcc-patches@gcc.gnu.org; Tue, 20 Aug 2019 05:51:27 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id EE6A2560BB; Tue, 20 Aug 2019 05:51:21 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id dQurijqxEd-6; Tue, 20 Aug 2019 05:51:21 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id DC89F560C0; Tue, 20 Aug 2019 05:51:21 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id DBAB963E; Tue, 20 Aug 2019 05:51:21 -0400 (EDT) Date: Tue, 20 Aug 2019 05:51:21 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Bob Duff Subject: [Ada] Warning for out-of-order record representation clauses Message-ID: <20190820095121.GA75434@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 205.232.38.15 X-IsSubscribed: yes The compiler can now warn for out-of-order record representation clauses. A warning is given if the order of component declarations, component clauses, and bit-level layout do not all agree. The warning is disabled by default, and may be enabled by the -gnatw_r switch. Tested on x86_64-pc-linux-gnu, committed on trunk 2019-08-20 Bob Duff gcc/ada/ * sem_ch13.adb (Component_Order_Check): New procedure to check for out-of-order clauses. * warnsw.ads, warnsw.adb: New -gnatw_r switch. * doc/gnat_ugn/building_executable_programs_with_gnat.rst: Document new switch. * gnat_ugn.texi: Regenerate. --- gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst +++ gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst @@ -2760,7 +2760,7 @@ of the pragma in the :title:`GNAT_Reference_manual`). :switch:`-gnatwa` *Activate most optional warnings.* - This switch activates most optional warning messages. See the remaining list + This switch activates most optional warning messages. See the remaining list in this section for details on optional warning messages that can be individually controlled. The warnings that are not turned on by this switch are: @@ -2790,6 +2790,8 @@ of the pragma in the :title:`GNAT_Reference_manual`). * :switch:`-gnatw.q` (questionable layout of record types) + * :switch:`-gnatw_r` (out-of-order record representation clauses) + * :switch:`-gnatw.s` (overridden size clause) * :switch:`-gnatwt` (tracking of deleted conditional code) @@ -3708,7 +3710,7 @@ of the pragma in the :title:`GNAT_Reference_manual`). warnings are given. -.. index:: -gnatwT (gcc) +.. index:: -gnatw.R (gcc) :switch:`-gnatw.R` *Suppress warnings for object renaming function.* @@ -3716,6 +3718,23 @@ of the pragma in the :title:`GNAT_Reference_manual`). This switch suppresses warnings for object renaming function. +.. index:: -gnatw_r (gcc) + +:switch:`-gnatw_r` + *Activate warnings for out-of-order record representation clauses.* + + This switch activates warnings for record representation clauses, + if the order of component declarations, component clauses, + and bit-level layout do not all agree. + The default is that these warnings are not given. + + +.. index:: -gnatw_R (gcc) + +:switch:`-gnatw_R` + *Suppress warnings for out-of-order record representation clauses.* + + .. index:: -gnatws (gcc) :switch:`-gnatws` --- gcc/ada/gnat_ugn.texi +++ gcc/ada/gnat_ugn.texi @@ -10925,7 +10925,7 @@ of the pragma in the @cite{GNAT_Reference_manual}). @emph{Activate most optional warnings.} -This switch activates most optional warning messages. See the remaining list +This switch activates most optional warning messages. See the remaining list in this section for details on optional warning messages that can be individually controlled. The warnings that are not turned on by this switch are: @@ -10970,6 +10970,9 @@ switch are: @code{-gnatw.q} (questionable layout of record types) @item +@code{-gnatw_r} (out-of-order record representation clauses) + +@item @code{-gnatw.s} (overridden size clause) @item @@ -12225,7 +12228,7 @@ opposed to renaming the function itself). The default is that these warnings are given. @end table -@geindex -gnatwT (gcc) +@geindex -gnatw.R (gcc) @table @asis @@ -12237,6 +12240,31 @@ warnings are given. This switch suppresses warnings for object renaming function. @end table +@geindex -gnatw_r (gcc) + + +@table @asis + +@item @code{-gnatw_r} + +@emph{Activate warnings for out-of-order record representation clauses.} + +This switch activates warnings for record representation clauses, +if the order of component declarations, component clauses, +and bit-level layout do not all agree. +The default is that these warnings are not given. +@end table + +@geindex -gnatw_R (gcc) + + +@table @asis + +@item @code{-gnatw_R} + +@emph{Suppress warnings for out-of-order record representation clauses.} +@end table + @geindex -gnatws (gcc) --- gcc/ada/sem_ch13.adb +++ gcc/ada/sem_ch13.adb @@ -10133,6 +10133,11 @@ package body Sem_Ch13 is -- recursively to compute After_Last for the parent type; in this case -- Warn is False and the warnings are suppressed. + procedure Component_Order_Check (Rectype : Entity_Id); + -- Check that the order of component clauses agrees with the order of + -- component declarations, and that the component clauses are given in + -- increasing order of bit offset. + ----------------------------- -- Check_Component_Overlap -- ----------------------------- @@ -10175,6 +10180,53 @@ package body Sem_Ch13 is end if; end Check_Component_Overlap; + --------------------------- + -- Component_Order_Check -- + --------------------------- + + procedure Component_Order_Check (Rectype : Entity_Id) is + Comp : Entity_Id := First_Component (Rectype); + Clause : Node_Id := First (Component_Clauses (N)); + Prev_Bit_Offset : Uint := Uint_0; + OOO : constant String := + "?component clause out of order with respect to declaration"; + + begin + -- Step Comp through components and Clause through component clauses, + -- skipping pragmas. We ignore discriminants and variant parts, + -- because we get most of the benefit from the plain vanilla + -- component cases, without the extra complexity. If we find a Comp + -- and Clause that don't match, give a warning on both and quit. If + -- we find two subsequent clauses out of order by bit layout, give + -- warning and quit. On each iteration, Prev_Bit_Offset is the one + -- from the previous iteration (or 0 to start). + + while Present (Comp) and then Present (Clause) loop + if Nkind (Clause) = N_Component_Clause + and then Ekind (Entity (Component_Name (Clause))) = E_Component + then + if Entity (Component_Name (Clause)) /= Comp then + Error_Msg_N (OOO, Comp); + Error_Msg_N (OOO, Clause); + exit; + end if; + + if not Reverse_Bit_Order (Rectype) + and then not Reverse_Storage_Order (Rectype) + and then Component_Bit_Offset (Comp) < Prev_Bit_Offset + then + Error_Msg_N ("?memory layout out of order", Clause); + exit; + end if; + + Prev_Bit_Offset := Component_Bit_Offset (Comp); + Comp := Next_Component (Comp); + end if; + + Next (Clause); + end loop; + end Component_Order_Check; + -------------------- -- Find_Component -- -------------------- @@ -10821,16 +10873,25 @@ package body Sem_Ch13 is end Overlap_Check2; end if; - -- Check for record holes (gaps). We skip this check if overlap was - -- detected, since it makes sense for the programmer to fix this - -- error before worrying about warnings. + -- Skip the following warnings if overlap was detected; programmer + -- should fix the errors first. - if Warn_On_Record_Holes and not Overlap_Detected then - declare - Ignore : Uint; - begin - Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True); - end; + if not Overlap_Detected then + -- Check for record holes (gaps) + + if Warn_On_Record_Holes then + declare + Ignore : Uint; + begin + Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True); + end; + end if; + + -- Check for out-of-order component clauses + + if Warn_On_Component_Order then + Component_Order_Check (Rectype); + end if; end if; -- For records that have component clauses for all components, and whose --- gcc/ada/warnsw.adb +++ gcc/ada/warnsw.adb @@ -79,6 +79,7 @@ package body Warnsw is Warn_On_Questionable_Layout := Setting; Warn_On_Questionable_Missing_Parens := Setting; Warn_On_Record_Holes := Setting; + Warn_On_Component_Order := Setting; Warn_On_Redundant_Constructs := Setting; Warn_On_Reverse_Bit_Order := Setting; Warn_On_Size_Alignment := Setting; @@ -177,6 +178,8 @@ package body Warnsw is W.Warn_On_Questionable_Missing_Parens; Warn_On_Record_Holes := W.Warn_On_Record_Holes; + Warn_On_Component_Order := + W.Warn_On_Component_Order; Warn_On_Redundant_Constructs := W.Warn_On_Redundant_Constructs; Warn_On_Reverse_Bit_Order := @@ -287,6 +290,8 @@ package body Warnsw is Warn_On_Questionable_Missing_Parens; W.Warn_On_Record_Holes := Warn_On_Record_Holes; + W.Warn_On_Component_Order := + Warn_On_Component_Order; W.Warn_On_Redundant_Constructs := Warn_On_Redundant_Constructs; W.Warn_On_Reverse_Bit_Order := @@ -500,6 +505,12 @@ package body Warnsw is when 'C' => Warn_On_Unknown_Compile_Time_Warning := False; + when 'r' => + Warn_On_Component_Order := True; + + when 'R' => + Warn_On_Component_Order := False; + when others => if Ignore_Unrecognized_VWY_Switches then Write_Line ("unrecognized switch -gnatw_" & C & " ignored"); --- gcc/ada/warnsw.ads +++ gcc/ada/warnsw.ads @@ -67,6 +67,12 @@ package Warnsw is -- Warn when explicit record component clauses leave uncovered holes (gaps) -- in a record layout. Off by default, set by -gnatw.h (but not -gnatwa). + Warn_On_Component_Order : Boolean := False; + -- Warn when record component clauses are out of order with respect to the + -- component declarations, or if the memory layout is out of order with + -- respect to component declarations and clauses. Off by default, set by + -- -gnatw_r (but not -gnatwa). + Warn_On_Size_Alignment : Boolean := True; -- Warn when explicit Size and Alignment clauses are given for a type, and -- the size is not a multiple of the alignment. Off by default, modified @@ -123,6 +129,7 @@ package Warnsw is Warn_On_Questionable_Layout : Boolean; Warn_On_Questionable_Missing_Parens : Boolean; Warn_On_Record_Holes : Boolean; + Warn_On_Component_Order : Boolean; Warn_On_Redundant_Constructs : Boolean; Warn_On_Reverse_Bit_Order : Boolean; Warn_On_Size_Alignment : Boolean;