From patchwork Wed Jan 2 10:38:39 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 208988 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id E2B092C0095 for ; Wed, 2 Jan 2013 21:38:53 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1357727934; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition:User-Agent: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=CnzFAlDZASj1n9hkcleF 1UYKAXM=; b=m7FTUFUxi20nRJiAwBHNV+lKmbghTsKmQuRn8qtUUJuvag3mK4lX 6nLFr6zSXge15lOgTcIFtJAAORPU832TRm6tE6RkE2HuuUwjgXucWdq7Mv2uyqmz /kxMWj567sG7fMrDdZZtHjDGp2EpZgCauF/Oxd3UYkhuDUH+pdKLeFA= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition:User-Agent:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=B+MiI9bplvxK6/0vOW4LWcpVTdPmxCQpXnIfMMX0eKju/9CZTdJU1wTjU1z5Oz ZkuJxkYeczljFtv0e4gnB8TlSP1QvhlWgi3FMH8o+2Yd9po2GK5m4ryPtwQOJgmi U3j6y8DqTogSw+ivwBUXrLiSvvwfIDXNoZd4zt8NPJbGI=; Received: (qmail 28187 invoked by alias); 2 Jan 2013 10:38:48 -0000 Received: (qmail 28099 invoked by uid 22791); 2 Jan 2013 10:38:47 -0000 X-SWARE-Spam-Status: No, hits=-0.4 required=5.0 tests=AWL, BAYES_50, KHOP_SPAMHAUS_DROP, RCVD_IN_HOSTKARMA_NO X-Spam-Check-By: sourceware.org Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 02 Jan 2013 10:38:40 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 164D32E46A; Wed, 2 Jan 2013 05:38:40 -0500 (EST) 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 FsGqZq41eVvD; Wed, 2 Jan 2013 05:38:40 -0500 (EST) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id E89BB2E219; Wed, 2 Jan 2013 05:38:39 -0500 (EST) Received: by kwai.gnat.com (Postfix, from userid 4192) id E7E6B3FF09; Wed, 2 Jan 2013 05:38:39 -0500 (EST) Date: Wed, 2 Jan 2013 05:38:39 -0500 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Thomas Quinot Subject: [Ada] Improvements to sprint for conditional expressions Message-ID: <20130102103839.GA20009@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) 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 This change improves the circuitry that produces a source-like rendition for an Ada tree by omitting the generation of extraneous parentheses around conditional expressions, and removing an extraneous ELSE keyword. The following compilation must produce the indicated output: $ gcc -c -gnat12 -gnatG condexpr_sprint.adb Source recreated from tree for Condexpr_Sprint (body) procedure condexpr_sprint (b : boolean; i : integer) is type r is record bb : boolean := (if b then b); ii : integer := (case i is when 1 => 1, when others => 2); end record; begin null; end condexpr_sprint; condexpr_sprint.adb:2:04: declaration expected procedure Condexpr_Sprint (B : Boolean; I : Integer) is zz -- Purposely introduce a serious error here to prevent further analysis type R is record BB : Boolean := (if B then B); II : Integer := (case I is when 1 => 1, when others => 2); end record; begin null; end Condexpr_Sprint; Tested on x86_64-pc-linux-gnu, committed on trunk 2013-01-02 Thomas Quinot * sprint.adb (Sprint_Node_Actual): Do not add extra parens for a conditional expression (CASE or IF expression) that already has parens. Also omit ELSE keyword for an IF expression without an ELSE part. Index: sprint.adb =================================================================== --- sprint.adb (revision 194776) +++ sprint.adb (working copy) @@ -1159,14 +1159,19 @@ when N_Case_Expression => declare - Alt : Node_Id; + Has_Parens : constant Boolean := Paren_Count (Node) > 0; + Alt : Node_Id; begin -- The syntax for case_expression does not include parentheses, -- but sometimes parentheses are required, so unconditionally - -- generate them here. + -- generate them here unless already present. - Write_Str_With_Col_Check_Sloc ("(case "); + if not Has_Parens then + Write_Char ('('); + end if; + + Write_Str_With_Col_Check_Sloc ("case "); Sprint_Node (Expression (Node)); Write_Str_With_Col_Check (" is"); @@ -1178,7 +1183,9 @@ Write_Char (','); end loop; - Write_Char (')'); + if not Has_Parens then + Write_Char (')'); + end if; end; when N_Case_Expression_Alternative => @@ -1963,15 +1970,19 @@ when N_If_Expression => declare - Condition : constant Node_Id := First (Expressions (Node)); - Then_Expr : constant Node_Id := Next (Condition); + Has_Parens : constant Boolean := Paren_Count (Node) > 0; + Condition : constant Node_Id := First (Expressions (Node)); + Then_Expr : constant Node_Id := Next (Condition); begin -- The syntax for if_expression does not include parentheses, -- but sometimes parentheses are required, so unconditionally - -- generate them here. + -- generate them here unless already present. - Write_Str_With_Col_Check_Sloc ("(if "); + if not Has_Parens then + Write_Char ('('); + end if; + Write_Str_With_Col_Check_Sloc ("if "); Sprint_Node (Condition); Write_Str_With_Col_Check (" then "); @@ -1979,11 +1990,16 @@ if Present (Then_Expr) then Sprint_Node (Then_Expr); - Write_Str_With_Col_Check (" else "); - Sprint_Node (Next (Then_Expr)); + + if Present (Next (Then_Expr)) then + Write_Str_With_Col_Check (" else "); + Sprint_Node (Next (Then_Expr)); + end if; end if; - Write_Char (')'); + if not Has_Parens then + Write_Char (')'); + end if; end; when N_If_Statement =>