[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-05-13 Thread sigra at home dot se


--- Comment #20 from sigra at home dot se  2006-05-13 08:44 ---
I see that the feature I asked for has been implemented in the Ada frontend of
GCC. This program:
with Ada.Text_IO; use Ada.Text_IO;
procedure Prov is
   A : Natural := 0;
begin
   Put_Line ("A =" & A'Img);
end Prov;

compiled with "gnatmake -gnatwa prov" will give the following warning:
prov.adb:3:04: warning: "A" is not modified, could be declared constant

Many thanks for this!

However I have to use other languages as well, especially C++. Therefore I want
the feature there too. You may think that Ada is for serious development while
C++ is just for ugly hacks and therefore does not need the kind of code
checking features that Ada has. But there are still people trying to do serious
development in C++. Why not try to make their work a little mor comfortable
too?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread pinskia at gcc dot gnu dot org


--- Comment #19 from pinskia at gcc dot gnu dot org  2006-01-19 01:00 
---
Closing as won't fix based on GDR's comments.


-- 

pinskia at gcc dot gnu dot org changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution||WONTFIX


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread gdr at cs dot tamu dot edu


--- Comment #18 from gdr at cs dot tamu dot edu  2006-01-19 00:09 ---
Subject: Re:  want optional warning for non-constant declarations that could be
constant

"sigra at home dot se" <[EMAIL PROTECTED]> writes:


| There is some good advice at

that precisely prooves my point: it is a coding style issue best
served by a deicated tool.  We plenty of coding stule rules out
there. 

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread sigra at home dot se


--- Comment #17 from sigra at home dot se  2006-01-18 23:23 ---
There is some good advice at http://www.gotw.ca/publications/advice98.htm which
says that one should be const-correct and use const whenever possible. (But I
do not suggest using const for return values.) This feature request is intended
to help in that task.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread gdr at cs dot tamu dot edu


--- Comment #16 from gdr at cs dot tamu dot edu  2006-01-18 22:37 ---
Subject: Re:  want optional warning for non-constant declarations that could be
constant

"sigra at home dot se" <[EMAIL PROTECTED]> writes:

| > Isn't this a task for lint-like tool?  GCC isn't such thing.
| 
| Are you sure?

yes, there many lint stuff best handled in dedicated tools.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread gdr at cs dot tamu dot edu


--- Comment #15 from gdr at cs dot tamu dot edu  2006-01-18 22:35 ---
Subject: Re:  want optional warning for non-constant declarations that could be
constant

"sigra at home dot se" <[EMAIL PROTECTED]> writes:

| > It does not make any sense to require the compiler to give a warning
| > in that case. 
| 
| Read the subject again: "optional"

That is beside the point.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread sigra at home dot se


--- Comment #14 from sigra at home dot se  2006-01-18 20:49 ---
> Isn't this a task for lint-like tool?  GCC isn't such thing.

Are you sure? http://directory.fsf.org/GNU/gcc.html says: "GCC provides many
levels of source code error checking traditionally provided by other tools
(such as lint)"

Since GCC is the tool that is best at reading and understanding the sourcecode,
I assume that it is best suited for source code diagnosis. What tool are you
thinking of for C++?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread sigra at home dot se


--- Comment #13 from sigra at home dot se  2006-01-18 20:41 ---
> It does not make any sense to require the compiler to give a warning
> in that case. 

Read the subject again: "optional"


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread gdr at cs dot tamu dot edu


--- Comment #12 from gdr at cs dot tamu dot edu  2006-01-18 20:33 ---
Subject: Re:  want optional warning for non-constant declarations that could be
constant

"sigra at home dot se" <[EMAIL PROTECTED]> writes:

| --- Comment #8 from sigra at home dot se  2006-01-18 19:29 ---
| > On Jan 18, 2006, at 11:19 AM, pcarlini at suse dot de wrote:
| > 
| > > --- Comment #4 from pcarlini at suse dot de  2006-01-18 16:19 
| > > ---
| > > (In reply to comment #3)
| > >> const does nothing when it comes to local variables except for not 
| > >> letting you
| > >> touch it in other expressions.  It does nothing for optimizations or 
| > >> anything
| > >> else.
| > >
| > > This last point is not obvious at all, in my opinion. Why not? In 
| > > principle,
| > > certainly const-ness *can* help optimizers one way or the other. Is 
| > > this just a
| > > current limitation? A design choice? Anything in the standard making 
| > > that
| > > tricky? I would like to know more.
| > 
| > 
| > int f(const int *a, int *b)
| > {
| >*b = 1;
| >return *a;
| > }
| > 
| > a and b can alias and there is no way around that at all because that is
| > what the C++ standard says.
| 
| In this case the compiler should warn because a could be declared "const int
*
| const" and b could be declared "int * const".

It does not make any sense to require the compiler to give a warning
in that case. 

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



Re: [Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread Gabriel Dos Reis
"sigra at home dot se" <[EMAIL PROTECTED]> writes:

| --- Comment #8 from sigra at home dot se  2006-01-18 19:29 ---
| > On Jan 18, 2006, at 11:19 AM, pcarlini at suse dot de wrote:
| > 
| > > --- Comment #4 from pcarlini at suse dot de  2006-01-18 16:19 
| > > ---
| > > (In reply to comment #3)
| > >> const does nothing when it comes to local variables except for not 
| > >> letting you
| > >> touch it in other expressions.  It does nothing for optimizations or 
| > >> anything
| > >> else.
| > >
| > > This last point is not obvious at all, in my opinion. Why not? In 
| > > principle,
| > > certainly const-ness *can* help optimizers one way or the other. Is 
| > > this just a
| > > current limitation? A design choice? Anything in the standard making 
| > > that
| > > tricky? I would like to know more.
| > 
| > 
| > int f(const int *a, int *b)
| > {
| >*b = 1;
| >return *a;
| > }
| > 
| > a and b can alias and there is no way around that at all because that is
| > what the C++ standard says.
| 
| In this case the compiler should warn because a could be declared "const int *
| const" and b could be declared "int * const".

It does not make any sense to require the compiler to give a warning
in that case. 

-- Gaby


[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread gdr at cs dot tamu dot edu


--- Comment #11 from gdr at cs dot tamu dot edu  2006-01-18 20:30 ---
Subject: Re:  want optional warning for non-constant declarations that could be
constant

"sigra at home dot se" <[EMAIL PROTECTED]> writes:

|  std::cout << static_cast(t) << std::endl;
| }
| 
| If "static_cast" would work, the compiler should warn.

given call-by-value, you must be joking.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



Re: [Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread Gabriel Dos Reis
"sigra at home dot se" <[EMAIL PROTECTED]> writes:

|  std::cout << static_cast(t) << std::endl;
| }
| 
| If "static_cast" would work, the compiler should warn.

given call-by-value, you must be joking.

-- Gaby


[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread gdr at cs dot tamu dot edu


--- Comment #10 from gdr at cs dot tamu dot edu  2006-01-18 20:29 ---
Subject: Re:   New: want optional warning for non-constant declarations that
could be constant

"sigra at home dot se" <[EMAIL PROTECTED]> writes:

| Declaring variables and parameters as constants is a very useful feature that
| should be used as much as possible. Therefore it would be very useful to have
| an option to warn when something is not declared as constant eventhough it
| could be, at least in C++ and probably in other languages as well.

Isn't this a task for lint-like tool?  GCC isn't such thing.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread pinskia at physics dot uc dot edu


--- Comment #9 from pinskia at gcc dot gnu dot org  2006-01-18 19:33 ---
Subject: Re:  want optional warning for non-constant declarations that could be
constant

> > int f(const int *a, int *b)
> > {
> >*b = 1;
> >return *a;
> > }
> > 
> > a and b can alias and there is no way around that at all because that is
> > what the C++ standard says.
> 
> In this case the compiler should warn because a could be declared "const int *
> const" and b could be declared "int * const".

That still does not change the fact that a could point to the same location as
b is pointing too.

-- Pinski


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



Re: [Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread Andrew Pinski
> > int f(const int *a, int *b)
> > {
> >*b = 1;
> >return *a;
> > }
> > 
> > a and b can alias and there is no way around that at all because that is
> > what the C++ standard says.
> 
> In this case the compiler should warn because a could be declared "const int *
> const" and b could be declared "int * const".

That still does not change the fact that a could point to the same location as
b is pointing too.

-- Pinski


[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread sigra at home dot se


--- Comment #8 from sigra at home dot se  2006-01-18 19:29 ---
> On Jan 18, 2006, at 11:19 AM, pcarlini at suse dot de wrote:
> 
> > --- Comment #4 from pcarlini at suse dot de  2006-01-18 16:19 
> > ---
> > (In reply to comment #3)
> >> const does nothing when it comes to local variables except for not 
> >> letting you
> >> touch it in other expressions.  It does nothing for optimizations or 
> >> anything
> >> else.
> >
> > This last point is not obvious at all, in my opinion. Why not? In 
> > principle,
> > certainly const-ness *can* help optimizers one way or the other. Is 
> > this just a
> > current limitation? A design choice? Anything in the standard making 
> > that
> > tricky? I would like to know more.
> 
> 
> int f(const int *a, int *b)
> {
>*b = 1;
>return *a;
> }
> 
> a and b can alias and there is no way around that at all because that is
> what the C++ standard says.

In this case the compiler should warn because a could be declared "const int *
const" and b could be declared "int * const".


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



Re: [Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread Andrew Pinski


On Jan 18, 2006, at 11:19 AM, pcarlini at suse dot de wrote:

--- Comment #4 from pcarlini at suse dot de  2006-01-18 16:19 
---

(In reply to comment #3)
const does nothing when it comes to local variables except for not 
letting you
touch it in other expressions.  It does nothing for optimizations or 
anything

else.


This last point is not obvious at all, in my opinion. Why not? In 
principle,
certainly const-ness *can* help optimizers one way or the other. Is 
this just a
current limitation? A design choice? Anything in the standard making 
that

tricky? I would like to know more.



int f(const int *a, int *b)
{
  *b = 1;
  return *a;
}

a and b can alias and there is no way around that at all because that is
what the C++ standard says.

-- Pinski



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread pinskia at physics dot uc dot edu


--- Comment #6 from pinskia at gcc dot gnu dot org  2006-01-18 16:29 ---
Subject: Re:  want optional warning for non-constant declarations that could be
constant


On Jan 18, 2006, at 11:19 AM, pcarlini at suse dot de wrote:

> --- Comment #4 from pcarlini at suse dot de  2006-01-18 16:19 
> ---
> (In reply to comment #3)
>> const does nothing when it comes to local variables except for not 
>> letting you
>> touch it in other expressions.  It does nothing for optimizations or 
>> anything
>> else.
>
> This last point is not obvious at all, in my opinion. Why not? In 
> principle,
> certainly const-ness *can* help optimizers one way or the other. Is 
> this just a
> current limitation? A design choice? Anything in the standard making 
> that
> tricky? I would like to know more.


int f(const int *a, int *b)
{
   *b = 1;
   return *a;
}

a and b can alias and there is no way around that at all because that is
what the C++ standard says.

-- Pinski


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread pcarlini at suse dot de


--- Comment #7 from pcarlini at suse dot de  2006-01-18 16:32 ---
(In reply to comment #6)

> int f(const int *a, int *b)
> {
>*b = 1;
>return *a;
> }
> 
> a and b can alias and there is no way around that at all because that is
> what the C++ standard says.

Interesting example. But what if the parameters cannot alias (e.g., different
types). What about the other cases mentioned in the PR (probably more important
to me)? No pointers involved at all?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread sigra at home dot se


--- Comment #5 from sigra at home dot se  2006-01-18 16:25 ---
(In reply to comment #3)
> I still don't understand what this warning is useful for?  
> 
> const does nothing when it comes to local variables except for not letting
> you touch it in other expressions.  It does nothing for optimizations or
> anything else.

That is what const does for local variables. It is useful for preventing
unintended modifications which can cause bugs. That is why the feature exists
at all.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread pcarlini at suse dot de


--- Comment #4 from pcarlini at suse dot de  2006-01-18 16:19 ---
(In reply to comment #3)
> const does nothing when it comes to local variables except for not letting you
> touch it in other expressions.  It does nothing for optimizations or anything
> else.

This last point is not obvious at all, in my opinion. Why not? In principle,
certainly const-ness *can* help optimizers one way or the other. Is this just a
current limitation? A design choice? Anything in the standard making that
tricky? I would like to know more.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread pinskia at gcc dot gnu dot org


--- Comment #3 from pinskia at gcc dot gnu dot org  2006-01-18 16:13 ---
I still don't understand what this warning is useful for?  

const does nothing when it comes to local variables except for not letting you
touch it in other expressions.  It does nothing for optimizations or anything
else.


-- 

pinskia at gcc dot gnu dot org changed:

   What|Removed |Added

   Keywords||diagnostic


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread sigra at home dot se


--- Comment #2 from sigra at home dot se  2006-01-18 16:07 ---
Example 1:
{
 int i = f();
 do_something(i + 1, 7, 'h');
 do_something_else(i % 3, 'e');
}

If i could be declared "const int", the compiler should warn.


Example 2:
float dra(float m, Panel & p) {
 p.do_me(5);
 return p.set_moad(m);
}

If m could be declared "const float", the compiler should warn.


Example 3:
{
 char t = 2;
 if (hej.is_double()) t *= 7;
 std::cout << static_cast(t) << std::endl;
}

If "static_cast" would work, the compiler should warn.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845



[Bug c++/25845] want optional warning for non-constant declarations that could be constant

2006-01-18 Thread pinskia at gcc dot gnu dot org


--- Comment #1 from pinskia at gcc dot gnu dot org  2006-01-18 15:36 ---
Can you give an example?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25845