[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-04-10 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

Marek Polacek  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 CC||mpolacek at gcc dot gnu.org
 Resolution|--- |WONTFIX

--- Comment #12 from Marek Polacek  ---
Since this seems to work as designed, I'm going to close it.

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-29 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

Richard Biener  changed:

   What|Removed |Added

   Priority|P3  |P2

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-15 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

--- Comment #11 from Jonathan Wakely  ---
(In reply to Martin Sebor from comment #6)
> But the code above doesn't trigger either warning when compiled as C.  I
> think that suggests that either the manual should be updated to explain the
> difference or the two front ends should be made to behave the same way.  As

C++ is clear that a prvalue of non-class type is cv-unqualified, I assume
that's also true in C. But it matters less for C, because (except for
extensions like __typeof__) the cv-qualifiers of an rvalue don't really matter.
In C++ they affect overloading, template argument deduction, decltype results
etc.

> a data point, Clang doesn't warn in either C or C++ modes on this code so I
> wonder if G++ is being overly pedantic here, especially in the typeof cases.

As I noted in PR 80544 comment 0, EDG warns about casts to cv-qualified scalar
types, and was my inspiration for our warning.

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-15 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

Jonathan Wakely  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2019-03-15
 Ever confirmed|0   |1

--- Comment #10 from Jonathan Wakely  ---
(In reply to Xavier from comment #9)
> We are compiling with -std=gnu++98 so decltype is not available there.

But __decltype is.

> And the "+ 0" trick does not seem to work correctly.

It will cause integer promotion for types with smaller rank than int.

> Do you have a solution that works with -std=gnu++98 ?

__decltype


But I think we want to suppress the warning for (__typeof__(expr))x
(we do still want to strip the qualifiers from the result type, just not warn).

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-15 Thread chantry.xavier at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

--- Comment #9 from Xavier  ---
We are compiling with -std=gnu++98 so decltype is not available there.

And the "+ 0" trick does not seem to work correctly.

% cat toto.c
#include 

int main(void) {
char data[128];
printf("%ju\n", sizeof(typeof(*data)));
printf("%ju\n", sizeof(typeof(*data + 0)));
printf("%ju\n", sizeof(typeof((*data) + 0)));
return 0;
}

% ./a.out
1
4
4

Do you have a solution that works with -std=gnu++98 ?

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-15 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

--- Comment #8 from Jonathan Wakely  ---
(In reply to Martin Sebor from comment #6)
> But the code above doesn't trigger either warning when compiled as C.

Because I only added it for C++, see r248432 and PR 80544.

I don't think I considered cases like this, because I don't think I realised
__typeof__ keeps cv-qualifiers.

I suppose it would be possible to suppress the warning when the target type
being cast to is a typeof expression.

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-15 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

Richard Biener  changed:

   What|Removed |Added

   Target Milestone|--- |8.4

--- Comment #7 from Richard Biener  ---
So everything works as designed?  Then please close as WONTFIX.

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-14 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

Martin Sebor  changed:

   What|Removed |Added

 CC||msebor at gcc dot gnu.org

--- Comment #6 from Martin Sebor  ---
I agree the macros obscure the problem.  Here's a simpler test case:

$ cat t.C && gcc -S -Wall -Wextra -Wignored-qualifiers t.C
int f (void) {
  typedef const int ConstInt;
  return (ConstInt)1;
}

int g (const long *p) { 
  return (__typeof__ (*p))1;
}

#if __cplusplus
template 
T h (T*) {
  return (T)1;
}

int hh (const int *p)
{
  return h (p);
}
#endif
t.C: In function ‘int f()’:
t.C:3:20: warning: type qualifiers ignored on cast result type
[-Wignored-qualifiers]
3 |   return (ConstInt)1;
  |^
t.C: In function ‘int g(const long int*)’:
t.C:7:27: warning: type qualifiers ignored on cast result type
[-Wignored-qualifiers]
7 |   return (__typeof__ (*p))1;
  |   ^

But the code above doesn't trigger either warning when compiled as C.  I think
that suggests that either the manual should be updated to explain the
difference or the two front ends should be made to behave the same way.  As a
data point, Clang doesn't warn in either C or C++ modes on this code so I
wonder if G++ is being overly pedantic here, especially in the typeof cases.

Btw., while G++ manages to avoid warning on template instantiation above, it
does warn the explicit instantiation below.  That seems like a bug to me since
there's no way to avoid the warning if the template comes from some component
the user has no control over.  (Clang has the same problem.)

gcc -O2 -S -Wall -Wextra -Wignored-qualifiers t.C
template 
T h (T*) {
  return (T)1;
}

int hh (const int *p)
{
  return h (p);
}

template const long h (const long*);

t.C:11:10: warning: type qualifiers ignored on function return type
[-Wignored-qualifiers]
   11 | template const long h (const long*);
  |  ^

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-14 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

--- Comment #5 from Jonathan Wakely  ---
-Wignored-qualifiers (C and C++ only)
   Warn if the return type of a function has a type qualifier such as "const".

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-14 Thread chantry.xavier at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

--- Comment #4 from Xavier  ---
Arf I did not understand this was a const problem. Maybe the warning could be a
bit clearer ? :)

I confirm that it works fine with typeof(*(bits) + 0).

This code is in a header shared between C and C++, and actually our codebase is
mostly C, so for now it's probably not worth rewriting all our macros with a
C++ version.

Thanks for the explanation, and for the workaround !

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-14 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

--- Comment #3 from Jonathan Wakely  ---
(In reply to Xavier from comment #0)
> This does not seem to occur with gcc, nor with g++ <= 7.

I implemented the warning for GCC 8.

> I could not find a workaround.

Stop using macros?

template
constexpr size_t bitsizeof()
{ return sizeof(T) * CHAR_BIT; }

template
T bitmask_nth(T n)
{ return (T)1 << (n & (bitsizeof() - 1)); }

template
T test_bit(T* bits, unsigned n)
{ return bits[n / bitsizeof()] & bitmask_nth((T)n); }


> Clearly I cannot remove the cast : for shifting 32 bits or more, we need to
> be on a int64.

You can either use std::remove_const_t to get the unqualified
version of the type, or just use decltype as Andrew suggested.

The warning seems correct, typeof(*d->tab) is const int64_t, and casting (const
int64_t)1 is defined by the language to be the same as (int64_t)1 so the const
is ignored.

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-14 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

--- Comment #2 from Andrew Pinski  ---
typeof(*(bits))
Try using decltype instead; typeof is a GNU extension.
or use:
typeof(*(bits) + 0)
Which will remove the const part.

[Bug c++/89722] [8/9 regression] strange warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]

2019-03-14 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89722

Andrew Pinski  changed:

   What|Removed |Added

   Keywords||diagnostic

--- Comment #1 from Andrew Pinski  ---
typeof(*(bits))

or:
typeof(*(bits) + 0)
Which will remove the const part.

Try using decltype instead; typeof is a GNU extension.