From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id DAB4D3857C56 for ; Wed, 23 Sep 2020 21:39:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DAB4D3857C56 Received: by mail-ot1-x32f.google.com with SMTP id g96so1140296otb.12 for ; Wed, 23 Sep 2020 14:39:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=RWx3QbBdvZXUKFqXBnp1qbLkfG0WCO1WnuxyGpihxKg=; b=BDGR3lHooQq+QmMkCHUSkII+1n2YTQCb4y2dTzoZPvAoRotYNPsfqAvc9e6nHImtYr fCfLUKUy1+m0P5RZUeznoa9AnMLKA9Evdutg/VQkC7U8uqEaxTwIT80OrLziYgG7sex2 00qsdGabgfBHJCE989tCpZUYge6Xw5ezKYDZotAyRoa8ZthPo2jLHVaC7AErerUHo+tr GvlNOswVZSpzw4AdHBH9YEUFQ6hXiTnH5Es4YHpD1O79aagGB+ixP2GN4zjkkA9tq1Ag lRMdgUVOFBsy6AQH766jrKJdYTV0femEaeaL9r+X7NFuSgNAn298O4WlNSEKzNbLDQKS pqHw== X-Gm-Message-State: AOAM533+0otKmlPkN41VDDQ3/y7iW50eqp5W+jTpmg8Y4qt96ZvZLPpF 1a1xfLL5+bJL+A9/krUtohqDSwTd//IGNA== X-Google-Smtp-Source: ABdhPJxZA7jx9ucudf1H9J1retuXbC+FD6rFcqA9twQDMu3CWvmxHnlNba3k/I+Sws8qnAqlWpCRig== X-Received: by 2002:a05:6830:1bd7:: with SMTP id v23mr1104444ota.33.1600897150044; Wed, 23 Sep 2020 14:39:10 -0700 (PDT) Received: from [192.168.0.41] (174-16-106-113.hlrn.qwest.net. [174.16.106.113]) by smtp.gmail.com with ESMTPSA id z5sm317802otp.16.2020.09.23.14.39.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Sep 2020 14:39:09 -0700 (PDT) From: Martin Sebor Subject: [committed] build a zero element array type that renders as T[0] in diagnostics To: gcc-patches Message-ID: Date: Wed, 23 Sep 2020 15:39:08 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Wed, 23 Sep 2020 21:39:12 -0000 One of the recently added -Warray-bounds tests fails in ILP32 because the warning code calls build_array_type_nelts(type, zero) and naively expects it to create an array type that renders as type[0] in warnings. While that works in LP64, it apparently isn't enough to produce such a type in other configurations. I have committed the patch below following the C front-end approach of creating a zero-element array that does render as expected regardless of data models. Martin commit 67aeddb785ddcc8688ee5736ecab3c81de34a214 (HEAD -> master, origin/master, origin/HEAD) Author: Martin Sebor Date: Wed Sep 23 15:19:13 2020 -0600 Build a zero element array type that reliably renders as T[0] in diagnostcs. gcc/ChangeLog: * gimple-array-bounds.cc (build_zero_elt_array_type): New function. (array_bounds_checker::check_mem_ref): Call it. diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc index b93ef7a7b74..ab638ba249f 100644 --- a/gcc/gimple-array-bounds.cc +++ b/gcc/gimple-array-bounds.cc @@ -372,6 +372,20 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, return warned; } +/* Hack around the internal representation constraints and build a zero + element array type that actually renders as T[0] in diagnostcs. */ + +static tree +build_zero_elt_array_type (tree eltype) +{ + tree idxtype = build_range_type (sizetype, size_zero_node, NULL_TREE); + tree arrtype = build_array_type (eltype, idxtype); + arrtype = build_distinct_type_copy (TYPE_MAIN_VARIANT (arrtype)); + TYPE_SIZE (arrtype) = bitsize_zero_node; + TYPE_SIZE_UNIT (arrtype) = size_zero_node; + return arrtype; +} + /* Checks one MEM_REF in REF, located at LOCATION, for out-of-bounds references to string constants. If VRP can determine that the array subscript is a constant, check if it is outside valid range. @@ -547,7 +561,10 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref, return false; offset_int nelts = arrbounds[1] / eltsize; - reftype = build_array_type_nelts (reftype, nelts.to_uhwi ()); + if (nelts == 0) + reftype = build_zero_elt_array_type (reftype); + else + reftype = build_array_type_nelts (reftype, nelts.to_uhwi ()); } else if (TREE_CODE (arg) == ADDR_EXPR) {