From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id DC4823857B88 for ; Tue, 7 Jun 2022 19:44:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DC4823857B88 Received: by mail-wm1-x32e.google.com with SMTP id o37-20020a05600c512500b0039c4ba4c64dso4267369wms.2 for ; Tue, 07 Jun 2022 12:44:30 -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=1KJNCaqb8j5AsasKpSRqfLgYGl1DEZEDBEVdM/9nmUI=; b=xmt65jOupt2qOSAIMI2t1pt+QNUCfwNRKBX52fVwF65z4jY9V68kKPYROz80ex63D/ lTnQC+D6WoXHbBeuNILXjpE66oFaGxZpwj6OfprRVLpFufcJNiLOqGo3clwZbcAyiE8k AbZlWERBilsEQHsw+NSSOv/smbDagC6Y5C5a8bzAcWDSvLtJZKT/+ggRVlfNJ2+jpxHl SQC4b2TsLd65DlYHkkbAVZwvJ4ErEfpC7uLjnCCWn5mMB/CPifArcX6LkTNMcOdpdAgz yYPhriWtDoWeNcSVHsB/bfk5EYVNSsqdjQ1muilPjk3C4x78NdFnlj7f3igXU+FNZBj9 NxLA== X-Gm-Message-State: AOAM530TFSiuMpmZh1USmiJg0AtKjYGiRK1pX9b7a6qe5X7aNNHWI9GV vZSPTkObjLm2hxFUumegNAsSPIL/9ROJW8WvHCI= X-Google-Smtp-Source: ABdhPJxlVDUKZD25dQ4YlA07S4lWqu1VywX9PXbLqbSjlY7NpW6Nz3rh0GFNbmCbkFDxyJDWtjV4TAEOdH5giGBFid4= X-Received: by 2002:a05:600c:601f:b0:39c:416c:4069 with SMTP id az31-20020a05600c601f00b0039c416c4069mr24201526wmb.85.1654631069626; Tue, 07 Jun 2022 12:44:29 -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:44:18 +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=-0.8 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:44:32 -0000 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?