From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 7A9783858295 for ; Tue, 7 Jun 2022 19:46:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7A9783858295 Received: by mail-wr1-x435.google.com with SMTP id k19so25515753wrd.8 for ; Tue, 07 Jun 2022 12:46:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=N77tk9jgZer2lGovwdZ6/uoKvYdlxx+Q8xJAMWOEVDg=; b=mQSMj39YYpjP+lChkfEVorCIdYn8t6sxRHa4gpu2DOWcnmPsINpKD7q4YlJGtrwx8o wGTz7O70Bvdb6wWHhndzfkfo2irwRRQduxW+qJOOI13JVacRC+Siy6ngg2dXH+qklFDf OZaUUnbtNcjaCdZzO21IgG0fBML5l3g3GMD6a3uph6FQ16Q6dkZ8UI+U0chP/pgXlCiq 9o0kdR7sxBCAt75XEb0xLzzD2ibbqqmyE/vaaaWiSZ8xtapaJ0vJmZwNdp3BPuUUgzZE 8imaemiMmTR2V//mxqPuAES+/5VFWbJ1GbzGCBf0euB1KZ3170c/4QRUBdyxTKHQd6zZ XKlA== X-Gm-Message-State: AOAM532WhuGUJZ2681+8DpMSou0zJAnp3UL9ehtFfqP6s2/7P0qrigAU j4QJNBx5/qdv2XQp4qs+pwfwcpB1GxReIPXv3J8= X-Google-Smtp-Source: ABdhPJyeEEhsvbwFhhn4wkQ3eDRcBau1Niaj0mQP7m8sGKKulujU+HhPEgSl+dautcNERM1rPIGhINRc43gguP4W3F8= X-Received: by 2002:a05:6000:1847:b0:218:4336:5590 with SMTP id c7-20020a056000184700b0021843365590mr11110434wri.511.1654631184160; Tue, 07 Jun 2022 12:46:24 -0700 (PDT) MIME-Version: 1.0 References: <6PeOfngSW_gLKMPtqp7UWs9ZQRv6KpLer221P_B9lNGGC7nJD0p-ta_7b0fwgSdz6cw7UzXsO0OGg0t2UUux81cVfIZkQcbjPwcRHFsNomI=@protonmail.com> In-Reply-To: From: Jonathan Wakely Date: Tue, 7 Jun 2022 20:46:13 +0100 Message-ID: Subject: Re: [RFC] Support for nonzero attribute To: Miika Cc: Ben Boeckel , "gcc@gcc.gnu.org" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Jun 2022 19:46:27 -0000 On Tue, 7 Jun 2022 at 20:44, Jonathan Wakely wrote: > > On Tue, 7 Jun 2022 at 20:40, Miika via Gcc wrote: > > > > On Monday, June 6th, 2022 at 9:42 PM, Ben Boeckel wrote: > > > > Based on Jakub's and Yair's comments I created a new attribute "inrange". > > > > Inrage takes three arguments, pos min and max. > > > > Pos being the argument position in the function, and min and max defines the > > > > range of valid integer. Both min and max are inclusive and work with enums. > > > > Warnings are enabled with the new flag: "-Winrange". > > > > > > > > > Is this something that could be applied to variables or types (I've not > > > much experience with GCC attribute internal mechanisms, so maybe not)? > > > > I took a closer look at it and looks like it can be applied. > > > > So trying to compile this: > > ``` > > typedef int __attribute__((inrange(0, 100))) percentage_t; > > int main() { > > int percentage __attribute__((inrange(0, 100))) = -1; > > percentage_t per __attribute__((inrange(0, 100))) = -1; > > } > > ``` > > > > Would print out something like this: > > > > foo.c: In function 'main': > > foo.c:3:59: warning: inrange variable 'percentage' requires integer in rage of 0..100 [-Winrange] > > N.B. "rage" should be "range". > > From the diagnostic it's not clear to me whether this is an inclusive > range. Is 0 allowed? Is 100 allowed? > > Using [0,100] interval notation would imply both endpoints are valid, > which I think matches the semantics of your attribute. Is interval > notation sufficiently widely understood to use here? Oh, Wikipedia tells me that 0..100 already means that, as an integer interval: https://en.wikipedia.org/wiki/Interval_(mathematics)#Integer_intervals So maybe it's fine as-is (except for the "rage" typo).