public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Const warning? (was: Re: [Patch] More redundant...)
@ 2003-04-22  6:52 John Reiser
  0 siblings, 0 replies; 5+ messages in thread
From: John Reiser @ 2003-04-22  6:52 UTC (permalink / raw)
  To: gcc

<<attribution snipped:>>
 > > I thinking about a warning for variables which the compiler can see
 > > are not modified. Something like:
 > >
 > > Warning: Variable x should be const.

This is easy for scalars (parameters or locals), but hard for array and struct/union types.
This is too bad, because such a message probably would prevent several bugs per programmer
per year.  Extensive use of 'const' is very beneficial for maintenance and documentation.
It's amazing how helpful it is to see lots of 'const': if it compiles, then the compiler
has told you that the value does not vary in time, so you don't have to check.

A couple years ago I got fairly far along on this, starting with gcc-2.96.  The message
I chose was "warning: const omitted".  There was a bit available in the type word
(the next-to-last unused bit), and I used that.  Then you just look at all places that
the compiler would complain about using as an lvalue something that is declared const,
and mark the type as non-const instead.  Increment/decrement required some fiddling,
as did taking the address of an object.  At the end of each scope, then walk the
declarations and complain for those objects that are not marked as non-const,
and also not marked as const.

This worked quite well for a while [the number of warnings is large], but then
I discovered the fundamental problem with the type system in 2.96.  The existing type
system in 2.96 has the property that, after declaration is finished, then the type word
itself is a constant, and can be freely copied (passed as a parameter, etc.),
indirected (have '*' applied to it), and selected (have '.member' applied to it).
But for non-const marking in one pass, all the non-zero non-const bits must be
forwarded to the "master" copy for the expression [object], and doing so is
difficult.  So I gave up.

Side note: The prefered syntax ought to be "char const *foo;" instead of
"const char *foo;".  Why?  Because then there is a simple rule for where 'const'
may appear in a declaration: immediately to the left of every identifier, and
immediately to the left of every '*'.  So the "maximal const" form in this case
is "char const *const foo;".

-- 
John Reiser, jreiser@BitWagon.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Const warning? (was: Re: [Patch] More redundant...)
@ 2003-04-22  0:07 Robert Dewar
  0 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 2003-04-22  0:07 UTC (permalink / raw)
  To: ghazi, gp.bolton; +Cc: gcc, pcarlini

> 
> I'm glad to hear that there may already be a patch to have a "const" 
> warning!  I can certainly see that pointers to pointers to pointers etc. 
> would be more difficult to do, especially since const can be at any level.
> 
> To me, it is certainly worth having a warning if it can result in faster 
> compile times and/or better code being generated.
> 
> As Paolo says, ideally the compiler would be able to generate the same 
> code whether variables are marked const or not.  However, this is maybe 
> not realistic.  So maybe it would just be a good idea to encourage use 
> of const to save the compiler from having to do all the analysis to 
> determine that a variable is const.
> 


Two points. First of all, I think the warning is very valuable even if it
does not cover the pointer case fully (don't let best be the enemy of
good). Most certainly we have found this valuable in Ada.

Second, I think the issue of better code is besides the point. It contributes
significantly to the readability and maintainability of code to label 
constant objects as constant, since you do not have to worry about the
possibility of them being changed. So this is something that should be
done as a matter of style. Since for a long time C lacked this capability,
C (and C++) programmers are not in the habit of designated things as
constant when they should, and a nudge from the compiler is helpful.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Const warning? (was: Re: [Patch] More redundant...)
  2003-04-21  4:58     ` Kaveh R. Ghazi
@ 2003-04-21 16:06       ` Gawain Bolton
  0 siblings, 0 replies; 5+ messages in thread
From: Gawain Bolton @ 2003-04-21 16:06 UTC (permalink / raw)
  To: Kaveh R. Ghazi; +Cc: pcarlini, gcc

I'm glad to hear that there may already be a patch to have a "const" 
warning!  I can certainly see that pointers to pointers to pointers etc. 
would be more difficult to do, especially since const can be at any level.

To me, it is certainly worth having a warning if it can result in faster 
compile times and/or better code being generated.

As Paolo says, ideally the compiler would be able to generate the same 
code whether variables are marked const or not.  However, this is maybe 
not realistic.  So maybe it would just be a good idea to encourage use 
of const to save the compiler from having to do all the analysis to 
determine that a variable is const.


Gawain

Kaveh R. Ghazi wrote:

> > [discussion moved to gcc]
> > 
> > Gawain Bolton wrote:
> > 
> > 
> > These patches to add "const" make me wonder if the compiler couldn't
> > help us.
> > 
> > I thinking about a warning for variables which the compiler can see
> > are not modified. Something like:
> > 
> > Warning: Variable x should be const.
> > 
> > Is this possible? Could this be useful? 
> > 
> > Hi Gawain (by the way those patches mainly remove some NULL pointer
> > checks, only secondarily const-ify variables).
> > 
> > I find your question interesting: as for many other warnings of this
> > kind (unreachable code, uninitialized variables, and so on) how many
> > such warnings would be missed or spuriously emitted would constitute a
> > good testbed for the optimizers of the compiler. Ideally the code
> > generated would be identical with and without const qualifiers but we
> > all know that in the real world this is not the case!
> > 
> > Ideas from the compiler people???
> > Paolo.
>
>I once toyed with a patch to warn about variables that could be
>constified.  It worked fine for regular objects, but the hard part
>(for me at least) was pointers.  E.g:
>
> > char *****foo;
> > ***foo = bar;
>
>Getting it to understand and mark which of the pointer layers could be
>constified and which couldn't was beyond my abilities.  It's probably
>not hard for someone who understands how these trees are constructed.
>
>Note fixing it for pointers is important for finding large statically
>initialized data and sticking it in .rodata sections.
>
>If someone is interested in carrying this across the goal-line, I can
>try and dig it up.
>
>		--Kaveh
>--
>Kaveh R. Ghazi			ghazi@caip.rutgers.edu
>
>
>  
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Const warning? (was: Re: [Patch] More redundant...)
  2003-04-21  2:21   ` Paolo Carlini
@ 2003-04-21  4:58     ` Kaveh R. Ghazi
  2003-04-21 16:06       ` Gawain Bolton
  0 siblings, 1 reply; 5+ messages in thread
From: Kaveh R. Ghazi @ 2003-04-21  4:58 UTC (permalink / raw)
  To: pcarlini; +Cc: gcc, gp.bolton

 > [discussion moved to gcc]
 > 
 > Gawain Bolton wrote:
 > 
 > 
 > These patches to add "const" make me wonder if the compiler couldn't
 > help us.
 > 
 > I thinking about a warning for variables which the compiler can see
 > are not modified. Something like:
 > 
 > Warning: Variable x should be const.
 > 
 > Is this possible? Could this be useful? 
 > 
 > Hi Gawain (by the way those patches mainly remove some NULL pointer
 > checks, only secondarily const-ify variables).
 > 
 > I find your question interesting: as for many other warnings of this
 > kind (unreachable code, uninitialized variables, and so on) how many
 > such warnings would be missed or spuriously emitted would constitute a
 > good testbed for the optimizers of the compiler. Ideally the code
 > generated would be identical with and without const qualifiers but we
 > all know that in the real world this is not the case!
 > 
 > Ideas from the compiler people???
 > Paolo.

I once toyed with a patch to warn about variables that could be
constified.  It worked fine for regular objects, but the hard part
(for me at least) was pointers.  E.g:

 > char *****foo;
 > ***foo = bar;

Getting it to understand and mark which of the pointer layers could be
constified and which couldn't was beyond my abilities.  It's probably
not hard for someone who understands how these trees are constructed.

Note fixing it for pointers is important for finding large statically
initialized data and sticking it in .rodata sections.

If someone is interested in carrying this across the goal-line, I can
try and dig it up.

		--Kaveh
--
Kaveh R. Ghazi			ghazi@caip.rutgers.edu

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Const warning? (was: Re: [Patch] More redundant...)
       [not found] ` <3EA1B709.3060204@free.fr>
@ 2003-04-21  2:21   ` Paolo Carlini
  2003-04-21  4:58     ` Kaveh R. Ghazi
  0 siblings, 1 reply; 5+ messages in thread
From: Paolo Carlini @ 2003-04-21  2:21 UTC (permalink / raw)
  To: gp.bolton; +Cc: gcc

[discussion moved to gcc]

Gawain Bolton wrote:

> These patches to add "const" make me wonder if the compiler couldn't 
> help us.
>
> I thinking about a warning for variables which the compiler can see 
> are not modified.   Something like:
>
>    Warning: Variable x should be const.
>
> Is this possible?  Could this be useful?

Hi Gawain (by the way those patches mainly remove some NULL pointer 
checks, only secondarily const-ify variables).

I find your question interesting: as for many other warnings of this 
kind (unreachable code, uninitialized variables, and so on) how many 
such warnings would be missed or spuriously emitted would constitute a 
good testbed for the optimizers of the compiler. Ideally the code 
generated would be identical with and without const qualifiers but we 
all know that in the real world this is not the case!

Ideas from the compiler people???

Paolo.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-04-22  3:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-22  6:52 Const warning? (was: Re: [Patch] More redundant...) John Reiser
  -- strict thread matches above, loose matches on Subject: below --
2003-04-22  0:07 Robert Dewar
     [not found] <3EA18026.70900@unitus.it>
     [not found] ` <3EA1B709.3060204@free.fr>
2003-04-21  2:21   ` Paolo Carlini
2003-04-21  4:58     ` Kaveh R. Ghazi
2003-04-21 16:06       ` Gawain Bolton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).