This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Why we can't have nice discarding const warnings


> All of these warnings are in c-typeck.c and go through
> warn_for_assignment.  

All of them? Not all of them. There is a small tiny village ... oops,
wrong story.

Given

void foo()
{
  const char *s = 0;
  (volatile char*)s;
}

compiled with -Wcast-qual, I believe the pedwarn comes from

	  if (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type))
	    /* There are qualifiers present in IN_OTYPE that are not
	       present in IN_TYPE.  */
	    pedwarn ("cast discards qualifiers from pointer target type");

so that can be improved, no? But that was not the original complaint.

> To be able to tell the user which qualifiers they discarded, we must
> make warning_for_assignment printflike.

Not necessarily. Currently, it says

	      else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
		warn_for_assignment ("%s discards qualifiers from pointer target type",
				     errtype, funname,
				     parmnum);

That could be changed to

	      else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
	        {
                  char *msg = "%s discards qualifiers from pointer target type";
                  if (discards_const (ttr, ttl))
                    msg = "%s discards `const' from pointer target type";
                  if (discards_volatile (ttr, ttl))
                    msg = "%s discards `volatile from pointer target type";
                  if (discards_restrict (ttr, ttl))
                    msg = "%s discards `restrict from pointer target type";
		  warn_for_assignment (msg,
				       errtype, funname,
				       parmnum);
                }

Perhaps not the most elegant approach, and similar to your original
patch, but I can't see a problem with it.

I don't think you *have* to provide messages for all combinations of
qualifiers discarded. If it says 'discards const', people know there
is an error in the program. In the quite unlikely event that they also
discard volatile in the same statement, fixing the 'discards const'
will most likely get the other qualifiers right, as well. In the even
more unlikely case that they only fix the const, the next compiler run
will tell them that they also should fix the volatile.

I think the complaint was that nobody really knows what a "qualifier"
is, whereas 'discards const' can be understood by every C programmer
(familiar with ANSI C).

Regards,
Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]