From patchwork Tue May 23 08:08:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 97884 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1980285vqo; Tue, 23 May 2023 01:27:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7RuscPgYwkxz+43vfUk972TF3Ehy0UWLFFTqWeGYz2QGiGoScYZ8ZaeYv+iaqRQRSaj6ND X-Received: by 2002:a17:907:2da2:b0:96f:d345:d100 with SMTP id gt34-20020a1709072da200b0096fd345d100mr7835298ejc.59.1684830425697; Tue, 23 May 2023 01:27:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684830425; cv=none; d=google.com; s=arc-20160816; b=fVrBpQjGhqpqkMA9BViP/KJaCE49s6A7gCYJEy5oVlJFyou1AAZfN+PwknDRMTFewC qFGCnD1Lv61lL57M/O6ULonG/a8nlpkRiDCw1Gn0oyadKzB3YF6Gc05QPNYFzfarOatJ Y7GVh6lHEw0X5RySGNiaU5HByy6lMpsmHCMzNwicheTBcEZjylqjEgnsAnlptGe2rPox x+amKB8o48QjZJfh3n4Fllw32RZH0AzBVK577MjlU+oL68jIIsZFpwEaNdyt4WnaoXmT HNX+HZjRDa/Qm4HePzAnDd0iK/fimiXDZAFWvlesHe0k+WnRvgxQPFe3fiKLF23hEKHa vW9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=PW9bGq9muiHH9Dtyuj9kE1qPlNk6CP0ypKpsIhJl0G0=; b=KeLHSel2qdEWqloVtM/nOApD9qp/uxidVEuWjQ6boEzLWafMZdhtmWk5oudBbH7A4K UtnpRAvdxR7dCHif3fJMwyIAliByC3di+6Pn4B/XbzyBTqMZ4hAY/XWJgyTPIg/Pw+Br D02trfIIxs+cCbRcy8y+TYDpf3FOv1wnnf3AX1+Rl8BUXoAh39nvbCN93hq0p59snMaf /9d9s7cgi1dMb5seH1iYVo3pz7+CiCkn6M0e4LJLHnVyVAk2B3GW8Fv7Mb68GfDVPI6w SZrsoBvALixdtpYgGe2Pgsfnf2mbx1Lho8zg5eYNJaTrzkeUUKcqPwUa3yD64HodTGvV ZAhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fA8s0AP7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id oy11-20020a170907104b00b0096f70603c8dsi1242959ejb.30.2023.05.23.01.27.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 May 2023 01:27:05 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fA8s0AP7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 782363943401 for ; Tue, 23 May 2023 08:16:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 782363943401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684829818; bh=PW9bGq9muiHH9Dtyuj9kE1qPlNk6CP0ypKpsIhJl0G0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=fA8s0AP7M8rwlyMErFckZTVHy6On0KC81ORqCWYu+igfJSeYHPdwLFN2HpRVZ1fzn iUt9IMICESF0vIqusWBQuDoe3fEwcvsGAfJYqGrkp+0AnnZFqXKTDUq5aoBcmF4boT kOPRZt4o0tiwpeVPjO9rhSIbQ5RmOFLgXsJSj+/U= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 47BDF385B503 for ; Tue, 23 May 2023 08:08:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 47BDF385B503 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3f427118644so69781525e9.0 for ; Tue, 23 May 2023 01:08:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684829303; x=1687421303; 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=PW9bGq9muiHH9Dtyuj9kE1qPlNk6CP0ypKpsIhJl0G0=; b=ebfvH7iHl7DootGY39kGB2+vZ6rpq91zC+p0Ireao1eBYeX3fPnSLfqX8ueX4bbpts nXHf/gEYq8lDJPdWwD/ioZvOtLNJL+Cok4LtfKG29CoFl0YTX/be2lLhcw8Pd1uGPHBQ a0N3ZFCB50kqqBdVxq83SPC0NfTDvs/hmiGNNQb3H/SfH6ST53z2v7q0D0DxHORpECiW kWhSo9Al8TH8Igu3symfx1DlmVdHNB8vcirth2z7gXWLeIaVVbD7au9QfKvtrcnrxfaX lWYS+LxZc/ShS2kRFNHkC8v0Mtnot4/XRO5e8CrxheMS28jwTGgYPIB5AdgpYweNf0AC hWpA== X-Gm-Message-State: AC+VfDwX+5HxL8KfNiW43RjG3hrnuB/ALZ247HXUyKQEmSTFts3nQ//k 9Fjj1owwVe5tvT2+q5Xi2iSJ8r/rlxEs46zyPmH1bg== X-Received: by 2002:a1c:f217:0:b0:3f4:2c71:b9ad with SMTP id s23-20020a1cf217000000b003f42c71b9admr9592180wmc.30.1684829302908; Tue, 23 May 2023 01:08:22 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id n11-20020a5d484b000000b00307c0afc030sm10390514wrs.4.2023.05.23.01.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 May 2023 01:08:22 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Piotr Trojanek Subject: [COMMITTED] ada: Turn assertions into defensive code in error locations Date: Tue, 23 May 2023 10:08:21 +0200 Message-Id: <20230523080821.1873611-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 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, 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: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766672748731154405?= X-GMAIL-MSGID: =?utf-8?q?1766672748731154405?= From: Piotr Trojanek We pretty-print numeric literals that do not come from source by relying on their Sloc. This generally works well, but sporadically the Sloc is set wrongly. We might want to trace and fix such occurrences, but for now it is simpler to replace an otherwise reasonable assertions with defensive code. gcc/ada/ * errout.adb (Last_Sloc): Refactor a heavily repeated "S := S + 1" statement into a subprogram; replace assertions with defensive code; fix few more off-by-one errors. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/errout.adb | 89 +++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 28 deletions(-) diff --git a/gcc/ada/errout.adb b/gcc/ada/errout.adb index 6531410f0d2..1c6222b3a29 100644 --- a/gcc/ada/errout.adb +++ b/gcc/ada/errout.adb @@ -1994,6 +1994,20 @@ package body Errout is --------------- function Last_Sloc (N : Node_Id) return Source_Ptr is + procedure Skip_Char (S : in out Source_Ptr); + -- Skip one character of the source buffer at location S + + --------------- + -- Skip_Char -- + --------------- + + procedure Skip_Char (S : in out Source_Ptr) is + begin + S := S + 1; + end Skip_Char; + + -- Local variables + SI : constant Source_File_Index := Get_Source_File_Index (Sloc (N)); SF : constant Source_Ptr := Source_First (SI); SL : constant Source_Ptr := Source_Last (SI); @@ -2001,6 +2015,8 @@ package body Errout is F : Node_Id; S : Source_Ptr; + -- Start of processing for Last_Sloc + begin F := Last_Node (N); S := Sloc (F); @@ -2035,7 +2051,7 @@ package body Errout is while S < SL and then Src (S + 1) in '0' .. '9' | '_' loop - S := S + 1; + Skip_Char (S); end loop; -- Skip past #based_numeral#, if present @@ -2043,30 +2059,36 @@ package body Errout is if S < SL and then Src (S + 1) = '#' then - S := S + 1; + Skip_Char (S); while S < SL and then Src (S + 1) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' loop - S := S + 1; + Skip_Char (S); end loop; - pragma Assert (S < SL and then Src (S + 1) = '#'); - - S := S + 1; + if S < SL + and then Src (S + 1) = '#' + then + Skip_Char (S); + end if; end if; -- Skip past exponent, if present - if S < SL + 1 + if S < SL and then Src (S + 1) in 'e' | 'E' then + Skip_Char (S); + -- For positive exponents the plus sign is optional, but we -- can simply skip past both plus and minus. - if Src (S + 2) in '+' | '-' then - S := S + 1; + if S < SL + and then Src (S + 1) in '+' | '-' + then + Skip_Char (S); end if; -- Skip past the numeral part @@ -2074,7 +2096,7 @@ package body Errout is while S < SL and then Src (S + 1) in '0' .. '9' | '_' loop - S := S + 1; + Skip_Char (S); end loop; end if; @@ -2085,55 +2107,66 @@ package body Errout is while S < SL and then Src (S + 1) in '0' .. '9' | '_' loop - S := S + 1; + Skip_Char (S); end loop; if S < SL then + + -- Skip the dot and continue with a decimal literal + if Src (S + 1) = '.' then + Skip_Char (S); + while S < SL and then Src (S + 1) in '0' .. '9' | '_' loop - S := S + 1; + Skip_Char (S); end loop; - else - pragma Assert (Src (S + 1) = '#'); - S := S + 1; + -- Skip the hash and continue with a based literal + + elsif Src (S + 1) = '#' then + Skip_Char (S); while S < SL and then Src (S + 1) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' loop - S := S + 1; + Skip_Char (S); end loop; - pragma Assert (S < SL and then Src (S + 1) = '.'); - - S := S + 1; + if S < SL + and then Src (S + 1) = '.' + then + Skip_Char (S); + end if; while S < SL and then Src (S + 1) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' loop - S := S + 1; + Skip_Char (S); end loop; - pragma Assert (S < SL and then Src (S + 1) = '#'); - - S := S + 1; + if S < SL + and then Src (S + 1) = '#' + then + Skip_Char (S); + end if; end if; end if; -- Skip past exponent, if present - if S < SL + 1 + if S < SL and then Src (S + 1) in 'e' | 'E' then + Skip_Char (S); -- For positive exponents the plus sign is optional, but we -- can simply skip past both plus and minus. - if Src (S + 2) in '+' | '-' then - S := S + 1; + if Src (S + 1) in '+' | '-' then + Skip_Char (S); end if; -- Skip past the numeral part @@ -2141,7 +2174,7 @@ package body Errout is while S < SL and then Src (S + 1) in '0' .. '9' | '_' loop - S := S + 1; + Skip_Char (S); end loop; end if; @@ -2153,7 +2186,7 @@ package body Errout is in '0' .. '9' | 'a' .. 'z' | 'A' .. 'Z' | '_' loop - S := S + 1; + Skip_Char (S); end loop; end case;