public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/54161] New: sizeof(void) expressions are accepted
@ 2012-08-02 18:03 daniel.kruegler at googlemail dot com
2012-08-02 18:42 ` [Bug c++/54161] " paolo.carlini at oracle dot com
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-08-02 18:03 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
Bug #: 54161
Summary: sizeof(void) expressions are accepted
Classification: Unclassified
Product: gcc
Version: 4.8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: daniel.kruegler@googlemail.com
gcc 4.8.0 20120729 (experimental) accepts the following code even using the
following options:
-Wall -pedantic -ansi
with either of -std=c++98 or -std=c++0x
//----
void f();
void (&g())();
const int a = sizeof(void);
const int b = sizeof(void());
const int c = sizeof(f());
const int d = sizeof(g());
typedef char test[a + b + c + d > 0 ? 1 : -1];
//----
albeit issuing warnings:
"4|warning: invalid application of 'sizeof' to a void type [-Wpedantic]|
5|warning: invalid application of 'sizeof' to a function type [-Wpedantic]|
6|warning: invalid application of 'sizeof' to a void type [-Wpedantic]|
7|warning: invalid application of 'sizeof' to a function type [-Wpedantic]|
"
This code is ill-formed according to [expr.sizeof] p1:
"The sizeof operator shall not be applied to an expression that has function or
incomplete type, [..], to the parenthesized name of such types, or to an lvalue
that designates a bit-field."
and thus should be rejected.
The current behaviour is especially annoying, because such expression can occur
in SFINAE expression where corresponding template specializations are not
excluded from the set.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
@ 2012-08-02 18:42 ` paolo.carlini at oracle dot com
2012-08-02 20:13 ` daniel.kruegler at googlemail dot com
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-02 18:42 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jason at gcc dot gnu.org
--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-02 18:41:54 UTC ---
The code checking for this is in c-common.c, thus changing the behavior is a
*tad* less trivial than it could be, we have to check c_dialect_cxx (), but
definitely very easy to do, if we wanted. Jason can you double check whether we
want to reject even without -pedantic?
Anyway, Daniel, it would be nice if you could add also SFINAE testcase too,
because likely it's a different issue: AFAICS, when we are in a SFINAE context,
the complain passed to c_sizeof_or_alignof_type should be false, thus the
function should return error_mark_node anyway, which, if properly checked by
the caller, should be enough for a correct SFINAE, irrespective of -pedantic or
any other command line option. Eg, for function type:
if (is_sizeof)
{
if (complain && (pedantic || warn_pointer_arith))
pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
"invalid application of %<sizeof%> to a function type");
else if (!complain)
return error_mark_node;
value = size_one_node;
}
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
2012-08-02 18:42 ` [Bug c++/54161] " paolo.carlini at oracle dot com
@ 2012-08-02 20:13 ` daniel.kruegler at googlemail dot com
2012-08-02 20:35 ` jason at gcc dot gnu.org
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-08-02 20:13 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
--- Comment #2 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2012-08-02 20:13:29 UTC ---
(In reply to comment #1)
> Jason can you double check whether we
> want to reject even without -pedantic?
I hope it will be active even without -pedantic
> Anyway, Daniel, it would be nice if you could add also SFINAE testcase too,
> because likely it's a different issue:[..]
Yes, you are right, I was generalizing too early here. The actual test case was
this one:
template<class T, class = decltype(sizeof(T))>
auto f(int) -> char;
template<class>
auto f(...) -> char(&)[2];
static_assert(sizeof(f<void>(0)) != 1, ""); // OK - Oops
static_assert(sizeof(f<void()>(0)) != 1, ""); // OK - Oops
but Jason reported on the core reflector that this is already a very special
situation that the core language needs to handle first. So lets take the SFINAE
rumors away from this report.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
2012-08-02 18:42 ` [Bug c++/54161] " paolo.carlini at oracle dot com
2012-08-02 20:13 ` daniel.kruegler at googlemail dot com
@ 2012-08-02 20:35 ` jason at gcc dot gnu.org
2012-08-02 21:11 ` paolo.carlini at oracle dot com
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2012-08-02 20:35 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
--- Comment #3 from Jason Merrill <jason at gcc dot gnu.org> 2012-08-02 20:34:52 UTC ---
A SFINAE testcase that doesn't depend on core 1172 would be
template<class T, unsigned = sizeof(T)>
auto f(int) -> char;
template<class>
auto f(...) -> char(&)[2];
static_assert(sizeof(f<void>(0)) != 1, ""); // OK - Oops
static_assert(sizeof(f<void()>(0)) != 1, ""); // OK - Oops
which already works fine.
I agree that we should get the pedwarn without -pedantic, but that seems to be
all there is to this bug.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
` (2 preceding siblings ...)
2012-08-02 20:35 ` jason at gcc dot gnu.org
@ 2012-08-02 21:11 ` paolo.carlini at oracle dot com
2012-08-03 14:20 ` jason at redhat dot com
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-02 21:11 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Last reconfirmed| |2012-08-02
Ever Confirmed|0 |1
--- Comment #4 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-02 21:11:37 UTC ---
Ok, thanks. Thus this specific issue seems quite minor, because even without
-pedantic we warn anyway *by default* because warn_pointer_arith is enabled by
default (ie, -Wall -pedantic are not needed).
(-pedantic-errors can always be used to have hard errors, of course)
In summary, I understand we want something like this (untested):
Index: c-common.c
===================================================================
--- c-common.c (revision 190092)
+++ c-common.c (working copy)
@@ -4578,10 +4578,17 @@ c_sizeof_or_alignof_type (location_t loc,
{
if (is_sizeof)
{
- if (complain && (pedantic || warn_pointer_arith))
- pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
- "invalid application of %<sizeof%> to a function type");
- else if (!complain)
+ if (complain)
+ {
+ if (c_dialect_cxx ())
+ pedwarn (loc, 0, "invalid application of %<sizeof%> to "
+ "a function type");
+ else if (pedantic || warn_pointer_arith)
+ pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
+ "invalid application of %<sizeof%> to "
+ "a function type");
+ }
+ else
return error_mark_node;
value = size_one_node;
}
@@ -4601,12 +4608,17 @@ c_sizeof_or_alignof_type (location_t loc,
}
else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
{
- if (type_code == VOID_TYPE
- && complain && (pedantic || warn_pointer_arith))
- pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
- "invalid application of %qs to a void type", op_name);
+ if (complain && type_code == VOID_TYPE)
+ {
+ if (c_dialect_cxx ())
+ pedwarn (loc, 0,
+ "invalid application of %qs to a void type", op_name);
+ else if (pedantic || warn_pointer_arith)
+ pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
+ "invalid application of %qs to a void type", op_name);
+ }
else if (!complain)
- return error_mark_node;
+ return error_mark_node;
value = size_one_node;
}
else if (!COMPLETE_TYPE_P (type)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
` (3 preceding siblings ...)
2012-08-02 21:11 ` paolo.carlini at oracle dot com
@ 2012-08-03 14:20 ` jason at redhat dot com
2012-08-04 12:35 ` paolo.carlini at oracle dot com
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: jason at redhat dot com @ 2012-08-03 14:20 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
--- Comment #5 from Jason Merrill <jason at redhat dot com> 2012-08-03 14:19:58 UTC ---
Makes sense to me.
Jason
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
` (4 preceding siblings ...)
2012-08-03 14:20 ` jason at redhat dot com
@ 2012-08-04 12:35 ` paolo.carlini at oracle dot com
2012-08-06 9:46 ` paolo.carlini at oracle dot com
2012-08-06 10:02 ` paolo.carlini at oracle dot com
7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-04 12:35 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |ASSIGNED
AssignedTo|unassigned at gcc dot |paolo.carlini at oracle dot
|gnu.org |com
Target Milestone|--- |4.8.0
--- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-04 12:35:32 UTC ---
On it.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
` (5 preceding siblings ...)
2012-08-04 12:35 ` paolo.carlini at oracle dot com
@ 2012-08-06 9:46 ` paolo.carlini at oracle dot com
2012-08-06 10:02 ` paolo.carlini at oracle dot com
7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-06 9:46 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution| |WORKSFORME
AssignedTo|paolo.carlini at oracle dot |unassigned at gcc dot
|com |gnu.org
--- Comment #7 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-06 09:46:18 UTC ---
In terms of warnings, we decided that the status quo is overall preferable:
http://gcc.gnu.org/ml/gcc-patches/2012-08/msg00262.html
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/54161] sizeof(void) expressions are accepted
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
` (6 preceding siblings ...)
2012-08-06 9:46 ` paolo.carlini at oracle dot com
@ 2012-08-06 10:02 ` paolo.carlini at oracle dot com
7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-06 10:02 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54161
--- Comment #8 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-06 10:01:49 UTC ---
I don't see in the testsuite something identical to Jason's Comment #3, thus to
be safe I'm adding it.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2012-08-06 10:02 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-02 18:03 [Bug c++/54161] New: sizeof(void) expressions are accepted daniel.kruegler at googlemail dot com
2012-08-02 18:42 ` [Bug c++/54161] " paolo.carlini at oracle dot com
2012-08-02 20:13 ` daniel.kruegler at googlemail dot com
2012-08-02 20:35 ` jason at gcc dot gnu.org
2012-08-02 21:11 ` paolo.carlini at oracle dot com
2012-08-03 14:20 ` jason at redhat dot com
2012-08-04 12:35 ` paolo.carlini at oracle dot com
2012-08-06 9:46 ` paolo.carlini at oracle dot com
2012-08-06 10:02 ` paolo.carlini at oracle dot com
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).