Re: [RFC/PATCH 9/9] constant: add -Wconstant-size warning

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Nov 22, 2018 at 12:13:27AM +0000, Ramsay Jones wrote:
> 
> 
> On 21/11/2018 22:53, Luc Van Oostenryck wrote:
> > On Mon, Nov 19, 2018 at 08:54:38PM +0000, Ramsay Jones wrote:
> >> From 8764999aa78415e217fd3106a8c8518a5b40c20c Mon Sep 17 00:00:00 2001
> >> From: Ramsay Jones <ramsay@xxxxxxxxxxxxxxxxxxxx>
> >> Date: Sun, 18 Nov 2018 23:52:23 +0000
> >> Subject: [PATCH 9/9] constant: add -Wconstant-size warning
> > 
> > Since the problem is not really a problem of size, nor a problem of
> > type, but a mismatch between the real type of the constant and the
> > type indicated by its suffix (or the lack of a suffix), I propose
> > to call this warning -Wconstant-suffix.
> 
> Sounds good to me. (I am bad at naming things). ;-)

I think it's often surprinsingly hard and in this case far from obvious.
 
> >> diff --git a/sparse.1 b/sparse.1
> >> index 3e13523..26299b3 100644
> >> --- a/sparse.1
> >> +++ b/sparse.1
> >> @@ -101,6 +101,16 @@ Sparse issues these warnings by default.  To turn them off, use
> >>  \fB\-Wno\-cast\-truncate\fR.
> >>  .
> >>  .TP
> >> +.B \-Wconstant-size
> >> +Warn if an integer constant is larger than the maximum representable value
> >> +of the type indicated by the type suffix (if any). For example, on a 64-bit
> >> +system, the constant 0xfffff00000000000U is larger than can be represented
> >> +by an int and so requires an unsigned long type suffix. (So, in this case,
> >> +the warning could be suppressed by using the UL type suffix).
> > 
> > I don't 100% agree with the use of 'requires'. The type of the constant is
> > determinated by its value, no suffix is required (but the lack of an 'L'
> > in the suffix may make believe that the type is 'unsigned int' or may
> > force you to count the zeroes, ...).
> 
> Hmm, the standard is a bit ambiguous in its wording. For example, (C99)
> in 6.4.4.1-2, it states "An integer constant begins with a digit, but
> has no period or exponent part. It may have a prefix that specifies
> its base and a suffix that _specifies its type_." Whereas, later in
> 6.4.4.1-5, it states "The type of an integer constant is the first of
> the corresponding list in which its _value can be represented_."
> (followed by a table of types).
> 
> So, this warning is about the discrepancy in the two 'type indicators'
> (the type suffix and the actual value of the constant). BTW, my use of
> 'requires' above was more about avoiding the warning ... ;-)

OK.
Yes, it's a bit ambiguous. I rarely find the standard very clear anyway :).

> > I propose to reword the example by something like:
> >     +Warn if an integer constant is larger than the maximum representable value
> >     +of the type indicated by its type suffix (if any). For example, on a
> >     +system where ints are 32-bit and longs 64-bit, the constant \fB0x100000000U\fR
> >     +is larger than can be represented by an \fBunsigned int\fR but fits in an
> >     +\fBunsigned long\fR. So its type is \fBunsigned long\fR but this is not
> >     +indicated by its suffix. In this case, the warning could be suppressed by
> >     +using the suffix \fBUL\fR: \fB0x100000000UL\fR.
> 
> OK.
> 
> > 
> > Another solution would be to simply remove the example.
> > 
> 
> I could go with that too, but how would you describe the warning
> without an example! ;-)

Yes, it's better with the example.


Thanks,
-- Luc



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux