From patchwork Wed Jun 1 08:44:40 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: 1637801 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=cM0sCsYp; 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 4LCjQG70ZSz9s1l for ; Wed, 1 Jun 2022 18:45:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C7FB38356A9 for ; Wed, 1 Jun 2022 08:45:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C7FB38356A9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654073107; bh=UJuZ8nLGnaw6StH6VZcw3FaBd6GevEB+AkZFJW+jSJc=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=cM0sCsYphbQR0CG+PEj97ibyPs/TWe6lpLDXFt5gecXXXWim/fXdN6HMfFItgzIJm 5EVyn2Oxz5LZtlGxWpxni2VwbQUuOQviOW8HugkU5yQ96Uyp2DFnH69wLvrU7XBmdm 75ylIgGe0AZ/EljaqPDwt61RunXxCi2iAuN2w/UY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id C5C8538356B1 for ; Wed, 1 Jun 2022 08:44:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C5C8538356B1 Received: by mail-ej1-x632.google.com with SMTP id jx22so2227973ejb.12 for ; Wed, 01 Jun 2022 01:44:43 -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=UJuZ8nLGnaw6StH6VZcw3FaBd6GevEB+AkZFJW+jSJc=; b=URKL3QFrvuHw2iUxLaVD+vR48bcdfrYvZFE80TTV/zJr/kbmdeaQj6vhfmX7qqcFhu F9SbM/1Bwfz59vrWiVfXnAT2uTrqN0daNZfU+bDyTYU7yGQw9z3LkKQenDBsXvG14bPf sPgA+YxskYPwLqygAaxY//xXPBgaZ1BPM/JxpyNwwhgF3hFGbAaS6XVQs1BTWtae8AS5 LfuXTo+if5QFhQDLyMUvccqzdT8Y2C6ok7mFIf33xnKLYvisXyDoGMx9hBjp88GBQo5f GN+fBJ4RMlJwfb9l2ERtDM0ylafeHLK++nGkRhbWXSfK1fqBBvzYTw1qiqc+yAKDTPi2 Lnuw== X-Gm-Message-State: AOAM532h44au5spE2EW9WeA9sZIC6ZdCi7QLlj6FZfaGrEig7Eg6hnKU eNpBDC9QMYNT4apAY7gi9Zkzn3eUxhp0GQ== X-Google-Smtp-Source: ABdhPJwqxQPFTZG2SvISir7JzRVmGVwo6TL4jOCaM+hev9NPrscEWDqgYWC1Pf3JXTOxOqZeJUYnsA== X-Received: by 2002:a17:906:dc89:b0:6f4:d3a9:34ed with SMTP id cs9-20020a170906dc8900b006f4d3a934edmr58271388ejc.459.1654073082429; Wed, 01 Jun 2022 01:44:42 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id u2-20020aa7d982000000b0042ab4e20543sm602257eds.48.2022.06.01.01.44.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 01:44:41 -0700 (PDT) Date: Wed, 1 Jun 2022 08:44:40 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Suppress warnings on membership test of ranges Message-ID: <20220601084440.GA1234272@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-13.2 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: 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" For a membership test "X in A .. B", the compiler used to warn if it could prove that X is within one of the bounds. For example, if we know at compile time that X >= A, then the above could be replaced by "X <= B". This patch suppresses that warning, because there is really nothing wrong with the membership test, and programmers sometimes find it annoying. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * exp_ch4.adb (Expand_N_In): Do not warn in the above-mentioned cases. * fe.h (Assume_No_Invalid_Values): Remove from fe.h, because this is not used in gigi. * opt.ads (Assume_No_Invalid_Values): Improve the comment. We don't need to "clearly prove"; we can just "prove". Remove the comment about fe.h, which is no longer true. diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -6388,7 +6388,7 @@ package body Exp_Ch4 is Lcheck : Compare_Result; Ucheck : Compare_Result; - Warn1 : constant Boolean := + Warn : constant Boolean := Constant_Condition_Warnings and then Comes_From_Source (N) and then not In_Instance; @@ -6397,16 +6397,6 @@ package body Exp_Ch4 is -- also skip these warnings in an instance since it may be the -- case that different instantiations have different ranges. - Warn2 : constant Boolean := - Warn1 - and then Nkind (Original_Node (Rop)) = N_Range - and then Is_Integer_Type (Etype (Lo)); - -- For the case where only one bound warning is elided, we also - -- insist on an explicit range and an integer type. The reason is - -- that the use of enumeration ranges including an end point is - -- common, as is the use of a subtype name, one of whose bounds is - -- the same as the type of the expression. - begin -- If test is explicit x'First .. x'Last, replace by valid check @@ -6491,7 +6481,7 @@ package body Exp_Ch4 is -- legality checks, because we are constant-folding beyond RM 4.9. if Lcheck = LT or else Ucheck = GT then - if Warn1 then + if Warn then Error_Msg_N ("?c?range test optimized away", N); Error_Msg_N ("\?c?value is known to be out of range", N); end if; @@ -6505,7 +6495,7 @@ package body Exp_Ch4 is -- since we know we are in range. elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then - if Warn1 then + if Warn then Error_Msg_N ("?c?range test optimized away", N); Error_Msg_N ("\?c?value is known to be in range", N); end if; @@ -6520,11 +6510,6 @@ package body Exp_Ch4 is -- a comparison against the upper bound. elsif Lcheck in Compare_GE then - if Warn2 and then not In_Instance then - Error_Msg_N ("??lower bound test optimized away", Lo); - Error_Msg_N ("\??value is known to be in range", Lo); - end if; - Rewrite (N, Make_Op_Le (Loc, Left_Opnd => Lop, @@ -6532,16 +6517,9 @@ package body Exp_Ch4 is Analyze_And_Resolve (N, Restyp); goto Leave; - -- If upper bound check succeeds and lower bound check is not - -- known to succeed or fail, then replace the range check with - -- a comparison against the lower bound. + -- Inverse of previous case. elsif Ucheck in Compare_LE then - if Warn2 and then not In_Instance then - Error_Msg_N ("??upper bound test optimized away", Hi); - Error_Msg_N ("\??value is known to be in range", Hi); - end if; - Rewrite (N, Make_Op_Ge (Loc, Left_Opnd => Lop, @@ -6555,7 +6533,7 @@ package body Exp_Ch4 is -- see if we can determine the outcome assuming everything is -- valid, and if so give an appropriate warning. - if Warn1 and then not Assume_No_Invalid_Values then + if Warn and then not Assume_No_Invalid_Values then Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); @@ -6570,18 +6548,6 @@ package body Exp_Ch4 is elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then Error_Msg_N ("?c?value can only be out of range if it is invalid", N); - - -- Lower bound check succeeds if value is valid - - elsif Warn2 and then Lcheck in Compare_GE then - Error_Msg_N - ("?c?lower bound check only fails if it is invalid", Lo); - - -- Upper bound check succeeds if value is valid - - elsif Warn2 and then Ucheck in Compare_LE then - Error_Msg_N - ("?c?upper bound check only fails for invalid values", Hi); end if; end if; end; diff --git a/gcc/ada/fe.h b/gcc/ada/fe.h --- a/gcc/ada/fe.h +++ b/gcc/ada/fe.h @@ -203,7 +203,6 @@ extern Boolean In_Extended_Main_Code_Unit (Entity_Id); /* opt: */ #define Ada_Version opt__ada_version -#define Assume_No_Invalid_Values opt__assume_no_invalid_values #define Back_End_Inlining opt__back_end_inlining #define Debug_Generated_Code opt__debug_generated_code #define Enable_128bit_Types opt__enable_128bit_types @@ -220,7 +219,6 @@ typedef enum { } Ada_Version_Type; extern Ada_Version_Type Ada_Version; -extern Boolean Assume_No_Invalid_Values; extern Boolean Back_End_Inlining; extern Boolean Debug_Generated_Code; extern Boolean Enable_128bit_Types; diff --git a/gcc/ada/opt.ads b/gcc/ada/opt.ads --- a/gcc/ada/opt.ads +++ b/gcc/ada/opt.ads @@ -186,12 +186,9 @@ package Opt is Assume_No_Invalid_Values : Boolean := False; -- GNAT Normally, in accordance with (RM 13.9.1 (9-11)) the front end -- assumes that values could have invalid representations, unless it can - -- clearly prove that the values are valid. If this switch is set (by + -- prove that the values are valid. If this switch is set (by -gnatB or -- pragma Assume_No_Invalid_Values (On)), then the compiler assumes values - -- are valid and in range of their representations. This feature is now - -- fully enabled in the compiler. - - -- WARNING: There is a matching C declaration of this variable in fe.h + -- are valid and in range of their representations. Back_Annotate_Rep_Info : Boolean := False; -- GNAT