From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 4C8F33858C2F for ; Mon, 7 Nov 2022 08:40:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4C8F33858C2F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wr1-x42c.google.com with SMTP id h9so15097026wrt.0 for ; Mon, 07 Nov 2022 00:40:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=r+aG/vEpVJwoBUkfYR8rBZJHu2w4+DofoSo+j/tR2rs=; b=MmyM6GHpeQHCT+jKyZ5ZpPTPAz5HM7bfbwdQqj8nP+cVsLJ427pBtvuSxsvPDV39kw gEP6LBWjU4DP8hSRfFW20ILHZiD9Ndx1LcmlE8H3UPW8lOoLbTtpBQCmbRTC8gpfqJsX IMNU9ok90Tu+wmG7wKS7N26EFOjQMoyzO8ab4UN+EH+Bdn4okSQPHaIRZ+Qf9zy2LBjg twftxuU05YkhCsVmhl6X7n9UU659TX5YfxYCIzVCSp/Pn2e66Sm50MY+w9yT2VyJPoHX N1PAKcrAM4bLNv3plB6Nkasbw/SShKMfIxMyh7sIv3F/rHfiz+zV/gAd60zlXe2LEMxK DISw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=r+aG/vEpVJwoBUkfYR8rBZJHu2w4+DofoSo+j/tR2rs=; b=N+gjntPlmJFfroMiNitVd/NpaWVL9s+8OhAPjaBJNRE0AxHy1rKumrXawXOUvGJBOh vgpNMnqclqFJU0Z5gNp/ffXAagJaaQNzsG1muUSBBtImgZhId8sJjtcO8/vTYi7JWkXH B2MbyYzuE0S/4UZwviFLbU5s/C/VnzIvlXD+4x7S/hQMVZQlcZp/eYbIarDHrX0vSdml PI+9XIpCtaF8CEqZRgg+DXkRxbrUuK6QWMqtoTVCQf/eWNMH2JuvN6C7VPNFlRrF2saQ 55iaz8c/HUyGgxFABfQJjc37IDtiElci6U4+YFEItO8aeBfiT8GyLRx1xp8yzDIPvrTd SGag== X-Gm-Message-State: ACrzQf2n0EYYaOaXlAZpw2aMq5GkzGfDzAiDcE8tJx6sagz5bNaIW/K0 uXee7pKsPuwEdC6dyFrtvnQ+Dj6TcHnHrA== X-Google-Smtp-Source: AMsMyM7pgERFbkMRe4WDEp59mqdje97klFTOUY9ozKFLXX5JVYtPDYJfjllKe03B6aP6TA7//9LHNQ== X-Received: by 2002:a5d:4b42:0:b0:236:70c1:b48b with SMTP id w2-20020a5d4b42000000b0023670c1b48bmr30632410wrs.443.1667810449108; Mon, 07 Nov 2022 00:40:49 -0800 (PST) Received: from poulhies-Precision-5550.lan (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id h2-20020a5d4302000000b0022ae0965a8asm6566086wrq.24.2022.11.07.00.40.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 00:40:48 -0800 (PST) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Bob Duff Subject: [COMMITTED] ada: Suppress warnings on derived True/False Date: Mon, 7 Nov 2022 09:40:46 +0100 Message-Id: <20221107084046.151369-1-poulhies@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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,KAM_ASCII_DIVIDERS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Bob Duff GNAT normally warns on "return ...;" if the "..." is known to be True or False, but not when it is a Boolean literal True or False. This patch also suppresses the warning when the type is derived from Boolean, and has convention C or Fortran (and therefore True is represented as "nonzero"). Without this fix, GNAT would give warnings like "False is always False". gcc/ada/ * sem_warn.adb (Check_For_Warnings): Remove unnecessary exception handler. (Warn_On_Known_Condition): Suppress warning when we detect a True or False that has been turned into a more complex expression because True is represented as "nonzero". (Note that the complex expression will subsequently be constant-folded to a Boolean True or False). Also simplify to always print "condition is always ..." instead of special-casing object names. The special case was unhelpful, and indeed wrong when the expression is a literal. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/sem_warn.adb | 119 ++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 70 deletions(-) diff --git a/gcc/ada/sem_warn.adb b/gcc/ada/sem_warn.adb index 9dccf0d73d1..0a46c66ae80 100644 --- a/gcc/ada/sem_warn.adb +++ b/gcc/ada/sem_warn.adb @@ -2948,21 +2948,6 @@ package body Sem_Warn is begin return Traverse (N) = Abandon; - - -- If any exception occurs, then something has gone wrong, and this is - -- only a minor aesthetic issue anyway, so just say we did not find what - -- we are looking for, rather than blow up. - - exception - when others => - -- With debug flag K we will get an exception unless an error has - -- already occurred (useful for debugging). - - if Debug_Flag_K then - Check_Error_Detected; - end if; - - return False; end Operand_Has_Warnings_Suppressed; ----------------------------------------- @@ -3379,11 +3364,10 @@ package body Sem_Warn is -- determined, and Test_Result is set True/False accordingly. Otherwise -- False is returned, and Test_Result is unchanged. - procedure Track (N : Node_Id; Loc : Node_Id); + procedure Track (N : Node_Id); -- Adds continuation warning(s) pointing to reason (assignment or test) -- for the operand of the conditional having a known value (or at least - -- enough is known about the value to issue the warning). N is the node - -- which is judged to have a known value. Loc is the warning location. + -- enough is known about the value to issue the warning). --------------------- -- Is_Known_Branch -- @@ -3417,36 +3401,45 @@ package body Sem_Warn is -- Track -- ----------- - procedure Track (N : Node_Id; Loc : Node_Id) is - Nod : constant Node_Id := Original_Node (N); + procedure Track (N : Node_Id) is - begin - if Nkind (Nod) in N_Op_Compare then - Track (Left_Opnd (Nod), Loc); - Track (Right_Opnd (Nod), Loc); + procedure Rec (Sub_N : Node_Id); + -- Recursive helper to do the work of Track, so we can refer to N's + -- Sloc in error messages. Sub_N is initially N, and a proper subnode + -- when recursively walking comparison operations. - elsif Is_Entity_Name (Nod) and then Is_Object (Entity (Nod)) then - declare - CV : constant Node_Id := Current_Value (Entity (Nod)); + procedure Rec (Sub_N : Node_Id) is + Orig : constant Node_Id := Original_Node (Sub_N); + begin + if Nkind (Orig) in N_Op_Compare then + Rec (Left_Opnd (Orig)); + Rec (Right_Opnd (Orig)); - begin - if Present (CV) then - Error_Msg_Sloc := Sloc (CV); + elsif Is_Entity_Name (Orig) and then Is_Object (Entity (Orig)) then + declare + CV : constant Node_Id := Current_Value (Entity (Orig)); + begin + if Present (CV) then + Error_Msg_Sloc := Sloc (CV); - if Nkind (CV) not in N_Subexpr then - Error_Msg_N ("\\??(see test #)", Loc); + if Nkind (CV) not in N_Subexpr then + Error_Msg_N ("\\??(see test #)", N); - elsif Nkind (Parent (CV)) = - N_Case_Statement_Alternative - then - Error_Msg_N ("\\??(see case alternative #)", Loc); + elsif Nkind (Parent (CV)) = + N_Case_Statement_Alternative + then + Error_Msg_N ("\\??(see case alternative #)", N); - else - Error_Msg_N ("\\??(see assignment #)", Loc); + else + Error_Msg_N ("\\??(see assignment #)", N); + end if; end if; - end if; - end; - end if; + end; + end if; + end Rec; + + begin + Rec (N); end Track; -- Local variables @@ -3464,11 +3457,8 @@ package body Sem_Warn is and then Is_Known_Branch then declare - Atrue : Boolean; - + Atrue : Boolean := Test_Result; begin - Atrue := Test_Result; - if Present (Parent (C)) and then Nkind (Parent (C)) = N_Op_Not then Atrue := not Atrue; end if; @@ -3550,7 +3540,6 @@ package body Sem_Warn is declare True_Branch : Boolean := Test_Result; Cond : Node_Id := C; - begin if Present (Parent (C)) and then Nkind (Parent (C)) = N_Op_Not @@ -3559,37 +3548,27 @@ package body Sem_Warn is Cond := Parent (C); end if; - -- Condition always True - - if True_Branch then - if Is_Entity_Name (Original_Node (C)) - and then Nkind (Cond) /= N_Op_Not - then - Error_Msg_NE - ("object & is always True at this point?c?", - Cond, Original_Node (C)); - Track (Original_Node (C), Cond); + -- Suppress warning if this is True/False of a derived boolean + -- type with Nonzero_Is_True, which gets rewritten as Boolean + -- True/False. - else - Error_Msg_N ("condition is always True?c?", Cond); - Track (Cond, Cond); - end if; + if Is_Entity_Name (Original_Node (C)) + and then Ekind (Entity (Original_Node (C))) + = E_Enumeration_Literal + and then Nonzero_Is_True (Etype (Original_Node (C))) + then + null; - -- Condition always False + -- Give warning for nontrivial always True/False case else - if Is_Entity_Name (Original_Node (C)) - and then Nkind (Cond) /= N_Op_Not - then - Error_Msg_NE - ("object & is always False at this point?c?", - Cond, Original_Node (C)); - Track (Original_Node (C), Cond); - + if True_Branch then + Error_Msg_N ("condition is always True?c?", Cond); else Error_Msg_N ("condition is always False?c?", Cond); - Track (Cond, Cond); end if; + + Track (Cond); end if; end; end if; -- 2.34.1