From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-4027.protonmail.ch (mail-4027.protonmail.ch [185.70.40.27]) by sourceware.org (Postfix) with ESMTPS id D82043858281 for ; Tue, 7 Jun 2022 19:56:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D82043858281 Date: Tue, 07 Jun 2022 19:56:29 +0000 To: Jonathan Wakely From: Miika Cc: Ben Boeckel , "gcc@gcc.gnu.org" Reply-To: Miika Subject: [RFC] Support for nonzero attribute Message-ID: In-Reply-To: References: <6PeOfngSW_gLKMPtqp7UWs9ZQRv6KpLer221P_B9lNGGC7nJD0p-ta_7b0fwgSdz6cw7UzXsO0OGg0t2UUux81cVfIZkQcbjPwcRHFsNomI=@protonmail.com> Feedback-ID: 17471336:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, SPF_HELO_PASS, 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:56:40 -0000 On Tuesday, June 7th, 2022 at 10:46 PM, Jonathan Wakely wrote: > On Tue, 7 Jun 2022 at 20:44, Jonathan Wakely wrote: > > > On Tue, 7 Jun 2022 at 20:40, Miika via Gcc gcc@gcc.gnu.org wrote: > > > > > On Monday, June 6th, 2022 at 9:42 PM, Ben Boeckel ben.boeckel@kitware= .com wrote: > > > > > > > > Based on Jakub's and Yair's comments I created a new attribute "i= nrange". > > > > > 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 w= ith 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 no= t)? > > > > > > 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))) =3D -1; percentage_t pe= r __attribute__((inrange(0, 100))) =3D -1; }` > > > > > > Would print out something like this: > > > > > > foo.c: In function 'main': > > > foo.c:3:59: warning: inrange variable 'percentage' requires integer i= n 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 inte= rval: > https://en.wikipedia.org/wiki/Interval_(mathematics)#Integer_intervals > > So maybe it's fine as-is (except for the "rage" typo). Thanks for noticing the typo. I probably need more sleep. And I'm open to any suggestions about the diagnostic message since I'm not = sure what's the best way to explain the error to the user.