From patchwork Sat Jun 29 07:53:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 1124725 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-504014-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="ml7TFEHt"; 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 45bQr01qxWz9s4Y for ; Sat, 29 Jun 2019 17:53:45 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=izBXLLqTKP58O+4j yhFGf+Dz/o73XWuCxPgOMY5SXfQ8CAvxcrg2KgsgGAoF/bmr02qyFmBsyWOAlxB1 w73Ylhhc0U73rA+VfdIlpyXpl0KRvV4DFd3vxg6PXALVxnz+JsduYmfULREE22aE +IPtfsTAeLchhCeDXd1l9cVmuwQ= 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:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=IHsCt8Bnt2al5uY6wFfi3W GgYFs=; b=ml7TFEHtwFkLMUQS9UTb/lpbDZLehvVoFVLui1y96l+3dZhhGH2EiG +IRrw5pIaZuqywtCJX1/E4fMIp9G0telc3wDU0oLWrSMgQxfgst8twxAi4w8jK5N PIrhBA8nYa/YpMmA7k981hElzUafUGL6h84Ur3yQS5n5JK3zfUK2A= Received: (qmail 123169 invoked by alias); 29 Jun 2019 07:53:38 -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 123160 invoked by uid 89); 29 Jun 2019 07:53:37 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-6.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=rework, Rework X-HELO: smtp.eu.adacore.com Received: from mel.act-europe.fr (HELO smtp.eu.adacore.com) (194.98.77.210) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 29 Jun 2019 07:53:35 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id A4CF881386 for ; Sat, 29 Jun 2019 09:53:33 +0200 (CEST) Received: from smtp.eu.adacore.com ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id cqTMn00SeHIp for ; Sat, 29 Jun 2019 09:53:33 +0200 (CEST) Received: from polaris.localnet (bon31-6-88-161-99-133.fbx.proxad.net [88.161.99.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.eu.adacore.com (Postfix) with ESMTPSA id 79DAC8137E for ; Sat, 29 Jun 2019 09:53:33 +0200 (CEST) From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [Ada] Improved error message for misaligned fields Date: Sat, 29 Jun 2019 09:53:31 +0200 Message-ID: <2776998.FaM2xUUPn7@polaris> MIME-Version: 1.0 The error message now mentions the alignment of the field's type and gives the position in storage units (bytes in Ada parlance) for the sake of consistency. Tested on x86-64/Linux, applied on mainline. 2019-06-29 Eric Botcazou * gcc-interface/decl.c (gnat_to_gnu_field): Rework error messages for fields requiring strict alignment, add explicit test on Storage_Unit for position and size, and mention type alignment for position. 2019-06-29 Eric Botcazou * gnat.dg/specs/atomic2.ads: Adjust error message. * gnat.dg/specs/clause_on_volatile.ads: Likewise. * gnat.dg/specs/size_clause3.ads: Likewise. Index: ada/gcc-interface/decl.c =================================================================== --- ada/gcc-interface/decl.c (revision 272811) +++ ada/gcc-interface/decl.c (working copy) @@ -7026,7 +7026,7 @@ gnat_to_gnu_field (Entity_Id gnat_field, if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent))) post_error_ne_tree - ("offset of& must be beyond parent{, minimum allowed is ^}", + ("position for& must be beyond parent{, minimum allowed is ^}", Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent)); } @@ -7040,79 +7040,82 @@ gnat_to_gnu_field (Entity_Id gnat_field, && !(type_annotate_only && Is_Tagged_Type (gnat_field_type))) { const unsigned int type_align = TYPE_ALIGN (gnu_field_type); + const char *field_s; if (TYPE_ALIGN (gnu_record_type) && TYPE_ALIGN (gnu_record_type) < type_align) SET_TYPE_ALIGN (gnu_record_type, type_align); - /* If the position is not a multiple of the alignment of the type, - then error out and reset the position. */ + if (is_atomic) + field_s = "atomic &"; + else if (is_aliased) + field_s = "aliased &"; + else if (is_independent) + field_s = "independent &"; + else if (is_strict_alignment) + field_s = "& with aliased or tagged part"; + else + gcc_unreachable (); + + /* If the position is not a multiple of the storage unit, then error + out and reset the position. */ if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos, - bitsize_int (type_align)))) + bitsize_unit_node))) { - const char *s; - - if (is_atomic) - s = "position of atomic field& must be multiple of ^ bits"; - else if (is_aliased) - s = "position of aliased field& must be multiple of ^ bits"; - else if (is_independent) - s = "position of independent field& must be multiple of ^ bits"; - else if (is_strict_alignment) - s = "position of & with aliased or tagged part must be" - " multiple of ^ bits"; - else - gcc_unreachable (); + char s[128]; + snprintf (s, sizeof (s), "position for %s must be " + "multiple of Storage_Unit", field_s); + post_error_ne (s, First_Bit (gnat_clause), gnat_field); + gnu_pos = NULL_TREE; + } + /* If the position is not a multiple of the alignment of the type, + then error out and reset the position. */ + else if (type_align > BITS_PER_UNIT + && !integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos, + bitsize_int (type_align)))) + { + char s[128]; + snprintf (s, sizeof (s), "position for %s must be multiple of ^", + field_s); post_error_ne_num (s, First_Bit (gnat_clause), gnat_field, - type_align); + type_align / BITS_PER_UNIT); + post_error_ne_num ("\\because alignment of its type& is ^", + First_Bit (gnat_clause), Etype (gnat_field), + type_align / BITS_PER_UNIT); gnu_pos = NULL_TREE; } if (gnu_size) { - tree gnu_type_size = TYPE_SIZE (gnu_field_type); - const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size); + tree type_size = TYPE_SIZE (gnu_field_type); + int cmp; - /* If the size is lower than that of the type, or greater for - atomic and aliased, then error out and reset the size. */ - if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased))) + /* If the size is not a multiple of the storage unit, then error + out and reset the size. */ + if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size, + bitsize_unit_node))) { - const char *s; - - if (is_atomic) - s = "size of atomic field& must be ^ bits"; - else if (is_aliased) - s = "size of aliased field& must be ^ bits"; - else if (is_independent) - s = "size of independent field& must be at least ^ bits"; - else if (is_strict_alignment) - s = "size of & with aliased or tagged part must be" - " at least ^ bits"; - else - gcc_unreachable (); - - post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field, - gnu_type_size); + char s[128]; + snprintf (s, sizeof (s), "size for %s must be " + "multiple of Storage_Unit", field_s); + post_error_ne (s, Last_Bit (gnat_clause), gnat_field); gnu_size = NULL_TREE; } - /* Likewise if the size is not a multiple of a byte, */ - else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size, - bitsize_unit_node))) + /* If the size is lower than that of the type, or greater for + atomic and aliased, then error out and reset the size. */ + else if ((cmp = tree_int_cst_compare (gnu_size, type_size)) < 0 + || (cmp > 0 && (is_atomic || is_aliased))) { - const char *s; - - if (is_independent) - s = "size of independent field& must be multiple of" - " Storage_Unit"; - else if (is_strict_alignment) - s = "size of & with aliased or tagged part must be" - " multiple of Storage_Unit"; + char s[128]; + if (is_atomic || is_aliased) + snprintf (s, sizeof (s), "size for %s must be ^", field_s); else - gcc_unreachable (); - - post_error_ne (s, Last_Bit (gnat_clause), gnat_field); + snprintf (s, sizeof (s), "size for %s must be at least ^", + field_s); + post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field, + type_size); gnu_size = NULL_TREE; } } Index: testsuite/gnat.dg/specs/atomic2.ads =================================================================== --- testsuite/gnat.dg/specs/atomic2.ads (revision 272633) +++ testsuite/gnat.dg/specs/atomic2.ads (working copy) @@ -9,7 +9,7 @@ package Atomic2 is end record; for Rec1 use record C at 0 range 0 .. 7; - I at 1 range 0 .. 31; -- { dg-error "position of atomic field" } + I at 1 range 0 .. 31; -- { dg-error "position for atomic|alignment" } end record; type Rec2 is record Index: testsuite/gnat.dg/specs/clause_on_volatile.ads =================================================================== --- testsuite/gnat.dg/specs/clause_on_volatile.ads (revision 272633) +++ testsuite/gnat.dg/specs/clause_on_volatile.ads (working copy) @@ -39,7 +39,7 @@ package Clause_On_Volatile is For A2'Alignment use 4; for A2 use record B at 0 range 0 .. 7; - AW at 1 range 0 .. 31; -- { dg-error "must be multiple" } + AW at 1 range 0 .. 31; -- { dg-error "must be multiple|alignment" } end record; type A3 is record @@ -49,7 +49,7 @@ package Clause_On_Volatile is For A3'Alignment use 4; for A3 use record B at 0 range 0 .. 7; - AW at 1 range 0 .. 15; -- { dg-error "must be (multiple||\[0-9\]*)" } + AW at 1 range 0 .. 15; -- { dg-error "must be (multiple||\[0-9\]*)|alignment" } end record; type V1 is record Index: testsuite/gnat.dg/specs/size_clause3.ads =================================================================== --- testsuite/gnat.dg/specs/size_clause3.ads (revision 272633) +++ testsuite/gnat.dg/specs/size_clause3.ads (working copy) @@ -14,7 +14,7 @@ package Size_Clause3 is rr : R1; -- size must be 40 end record; for S1 use record - rr at 0 range 0 .. 39; -- { dg-error "size of .rr. with aliased or tagged" } + rr at 0 range 0 .. 39; -- { dg-error "size for .rr. with aliased or tagged" } end record; -- The record is explicitly given alignment 1 so its real type is 40. @@ -44,7 +44,7 @@ package Size_Clause3 is rr : R3; -- size must be 40 end record; for S3 use record - rr at 0 range 0 .. 39; -- { dg-error "size of .rr. with aliased or tagged" } + rr at 0 range 0 .. 39; -- { dg-error "size for .rr. with aliased or tagged" } end record; end Size_Clause3;