From patchwork Tue Jul 12 12:25:22 2022 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: 1655382 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=SXErEYjg; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Lj0RD0tDCz9sB4 for ; Tue, 12 Jul 2022 22:28:40 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BB059388DF2E for ; Tue, 12 Jul 2022 12:28:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BB059388DF2E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1657628917; bh=JaFly3xNa4eldIxCFlA5cLketFpZzYQ8ZakvFkXzQSI=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=SXErEYjgEHPL6z3jYf9p6JAbrW+iF2Pm1SXdm2A0TK46J+6cZHyokdw2q7xR1od35 HTnfG7K2lrc/DYf3rbzjnbE7KHEc/4yhfrBP0yuu/ZhEQIqrmlYszWk2DuhyTlAcgP +c8oq6k/ftDXaH8G1cxFISo92YUgn8znKM6FcVpg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id 98FB03857355 for ; Tue, 12 Jul 2022 12:25:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 98FB03857355 Received: by mail-ej1-x633.google.com with SMTP id ez10so13963994ejc.13 for ; Tue, 12 Jul 2022 05:25:23 -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=JaFly3xNa4eldIxCFlA5cLketFpZzYQ8ZakvFkXzQSI=; b=xbdUDJ86UHZthOWnomBrhYZxC7OagGIR7ames/vdLU0E3f4OW4UWlR9FMjEBYb6VcM YSydKlzrbKZVfSfnJ1cSNNWh5HbGu1TC73UKOjQCnlKkvIshZxvYH//1nD+PE9ZoOvvY 8xMeSvIoKgb8GfJmBjwrfNsdQWs6aPNMRvZ++OjrGPGRO4QNP+7hZAPIuuHEHE77BqrV aMeC+hG/4FwQUVJpJ5mKm7uArkNlGWH7mlXH0gzciVkaJCpCQXemz9CwRu3AzbIUWT2d VH547mH+gEh1HfRqsvxcdUTokkTbBTnT5lZr2QnvSuiNFNHNZFhWKyYUSuttzvh4fvFq 8rag== X-Gm-Message-State: AJIora+IBOnFD482Ua656yjKXsSZLrSJXusZXaxRrIY0+TvO/c6mNa55 ZusLosuQdek0OLBepDWDo8If0qzTRgd2jg== X-Google-Smtp-Source: AGRyM1tojVjUtEdK6ej6zuafNmZvb2xHN6aI/dBbE2aNX1FeUXOPownieCe086l+zVuKmN48ZT+oIA== X-Received: by 2002:a17:907:16a2:b0:72b:87c9:af07 with SMTP id hc34-20020a17090716a200b0072b87c9af07mr1355836ejc.121.1657628723225; Tue, 12 Jul 2022 05:25:23 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id lb16-20020a170907785000b0072b36cbcdaasm3780371ejc.92.2022.07.12.05.25.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 05:25:22 -0700 (PDT) Date: Tue, 12 Jul 2022 12:25:22 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Refine heuristics for unreachable-code warnings Message-ID: <20220712122522.GA3404764@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, 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: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Bob Duff Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch refines the heuristics for when we warn about unreachable code, to avoid common false alarms. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_ch5.adb (Check_Unreachable_Code): Refine heuristics. * sem_util.ads, sem_util.adb (Is_Static_Constant_Name): Remove this; instead we have a new function Is_Simple_Case in Sem_Ch5.Check_Unreachable_Code. diff --git a/gcc/ada/sem_ch5.adb b/gcc/ada/sem_ch5.adb --- a/gcc/ada/sem_ch5.adb +++ b/gcc/ada/sem_ch5.adb @@ -4393,6 +4393,31 @@ package body Sem_Ch5 is ---------------------------- procedure Check_Unreachable_Code (N : Node_Id) is + + function Is_Simple_Case (N : Node_Id) return Boolean; + -- N is the condition of an if statement. True if N is simple enough + -- that we should not set Unblocked_Exit_Count in the special case + -- below. + + -------------------- + -- Is_Simple_Case -- + -------------------- + + function Is_Simple_Case (N : Node_Id) return Boolean is + begin + return + Is_Trivial_Boolean (N) + or else + (Comes_From_Source (N) + and then Is_Static_Expression (N) + and then Nkind (N) in N_Identifier | N_Expanded_Name + and then Ekind (Entity (N)) = E_Constant) + or else + (not In_Instance + and then Nkind (Original_Node (N)) = N_Op_Not + and then Is_Simple_Case (Right_Opnd (Original_Node (N)))); + end Is_Simple_Case; + Error_Node : Node_Id; Nxt : Node_Id; P : Node_Id; @@ -4574,8 +4599,7 @@ package body Sem_Ch5 is and then No (Else_Statements (P)) and then Is_OK_Static_Expression (Condition (P)) and then Is_True (Expr_Value (Condition (P))) - and then not Is_Trivial_Boolean (Condition (P)) - and then not Is_Static_Constant_Name (Condition (P)) + and then not Is_Simple_Case (Condition (P)) then pragma Assert (Unblocked_Exit_Count = 2); Unblocked_Exit_Count := 0; diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb --- a/gcc/ada/sem_util.adb +++ b/gcc/ada/sem_util.adb @@ -21532,18 +21532,6 @@ package body Sem_Util is and then Entity (N) in Standard_True | Standard_False; end Is_Trivial_Boolean; - ----------------------------- - -- Is_Static_Constant_Name -- - ----------------------------- - - function Is_Static_Constant_Name (N : Node_Id) return Boolean is - begin - return Comes_From_Source (N) - and then Is_Static_Expression (N) - and then Nkind (N) in N_Identifier | N_Expanded_Name - and then Ekind (Entity (N)) = E_Constant; - end Is_Static_Constant_Name; - -------------------------------------- -- Is_Unchecked_Conversion_Instance -- -------------------------------------- diff --git a/gcc/ada/sem_util.ads b/gcc/ada/sem_util.ads --- a/gcc/ada/sem_util.ads +++ b/gcc/ada/sem_util.ads @@ -2485,9 +2485,6 @@ package Sem_Util is -- Determine whether source node N denotes "True" or "False". Note that -- this is not true for expressions that got folded to True or False. - function Is_Static_Constant_Name (N : Node_Id) return Boolean; - -- True if N is a name that statically denotes a static constant. - function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean; -- Determine whether an arbitrary entity denotes an instance of function -- Ada.Unchecked_Conversion.