From patchwork Sat Oct 14 17:08:11 2017 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: 825841 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-464222-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="rUtII4Lg"; 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 3yDrdL5Z3Pz9t2l for ; Sun, 15 Oct 2017 04:08:22 +1100 (AEDT) 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=EZePro9W67HKGGgGR1fQo6H/qCn4ztgk8+SUVgNnFgZiUdlABX TseW2wSnnhS4IOpQwuNxSy8r4NZ2BbiCm68u1Koy+kqmIPiYQQLeKqxoVXinS86y BxkNKfaLq1pxYGujw9hb4xGTMstSNS0WO7ccNrq2MqP2FNwEOQ44MwlmI= 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=l308KzunoY7EgpENqZDcGFnAFCQ=; b=rUtII4LgRqtXKIYjfLeh FLgFQNGMJDHptkhHuh6oONBJ1H/16hWMFZ4fP/CVx8Y5iyoW/aA1bNG9gWKzOTAu 4FiBcZYtgos+CsXMe3dK0FPyJ35S7bJg66B89q0joSBx0DSKrEnr/kPfL52TVBqY gKtcr/Ha6Gk76reFp+h3vDM= Received: (qmail 71173 invoked by alias); 14 Oct 2017 17:08:14 -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 71147 invoked by uid 89); 14 Oct 2017 17:08:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=unwanted, incorporated X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 14 Oct 2017 17:08:13 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id A6D445625E; Sat, 14 Oct 2017 13:08:11 -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 wABMO3E-m9xO; Sat, 14 Oct 2017 13:08:11 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 95FBE56079; Sat, 14 Oct 2017 13:08:11 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id 94F2B319; Sat, 14 Oct 2017 13:08:11 -0400 (EDT) Date: Sat, 14 Oct 2017 13:08:11 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Premature evaluation of message string in Assert pragma Message-ID: <20171014170811.GA123098@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes RM 11.4.2 stipulates that the optional string argument in an Assert pragma is evaluated only if the assertion fails and the string is incorporated into the raise statement. Previous to this patch the string expression was evaluated unconditionally, leading to unwanted side effects if its evaluation only made sense in case of failure of the assertion. Executing: gnatmake -gnata -gnatws -q main main must yield: Assert succeeds raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : P should be null, got A_STRING --- with Text_IO; use Text_IO; procedure Main is P : access String; X : Integer; function Zero return Integer is begin return 0; end; begin X := Zero; pragma Assert (P = null, "P should be null, got " & P.all); Put_Line ("Assert succeeds"); if X = 0 then P := new String'("A_STRING"); end if; pragma Assert (P = null, "P should be null, got " & P.all); end Main; Tested on x86_64-pc-linux-gnu, committed on trunk 2017-10-14 Ed Schonberg * sem_prag.adb (Analyze_Pragma, case Check): Defer evaluation of the optional string in an Assert pragma until the expansion of the pragma has rewritten it as a conditional statement, so that the string argument is only evaluaed if the assertion fails. This is mandated by RM 11.4.2. Index: sem_prag.adb =================================================================== --- sem_prag.adb (revision 253754) +++ sem_prag.adb (working copy) @@ -13249,16 +13249,18 @@ -- If checks are not on we don't want any expansion (since -- such expansion would not get properly deleted) but -- we do want to analyze (to get proper references). - -- The Preanalyze_And_Resolve routine does just what we want + -- The Preanalyze_And_Resolve routine does just what we want. + -- Ditto if pragma is active, because it will be rewritten + -- as an if-statement whose analysis will complete analysis + -- and expansion of the string message. This makes a + -- difference in the unusual case where the expression for + -- the string may have a side effect, such as raising an + -- exception. This is mandated by RM 11.4.2, which specifies + -- that the string expression is only evaluated if the + -- check fails and Assertion_Error is to be raised. - if Is_Ignored (N) then - Preanalyze_And_Resolve (Str, Standard_String); + Preanalyze_And_Resolve (Str, Standard_String); - -- Otherwise we need a proper analysis and expansion - - else - Analyze_And_Resolve (Str, Standard_String); - end if; end if; -- Now you might think we could just do the same with the Boolean