From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-40138.protonmail.ch (mail-40138.protonmail.ch [185.70.40.138]) by sourceware.org (Postfix) with ESMTPS id A61A03858295 for ; Tue, 7 Jun 2022 19:39:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A61A03858295 Date: Tue, 07 Jun 2022 19:39:33 +0000 To: Ben Boeckel From: Miika Cc: "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=-3.8 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:39:39 -0000 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 define= s the > > range of valid integer. Both min and max are inclusive and work with en= ums. > > 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))) =3D -1; percentage_t per __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 in rage= of 0..100 [-Winrange] 3 | int percentage __attribute__((inrange(0, 100))) =3D -1; | ^ foo.c:4:61: warning: inrange variable 'per' requires integer in rage of 0..= 100 [-Winrange] 4 | percentage_t per __attribute__((inrange(0, 100))) =3D -1; | Miika