Commit a01fff59 by Matt Austern Committed by Matt Austern

Add a new flag...

Add a new flag, -W(no-)invalid-offsetof, to control whether or
not the compiler warns about incorrect use of the offsetof macro
in C++.  By default the warning is on.

From-SVN: r66972
parent eab1ff73
2003-05-19 Matt Austern <austern@apple.com>
* c-opts.c (COMMAND_LINE_OPTIONS): Add -Winvalid-offsetof option.
* c-common.h (warn_invalid_offsetof): Declare.
* c-common.c (warn_invalid_offsetof): Define.
* doc/invoke.texi: Document -Winvalid-offsetof.
* testsuite/g++.dg/other/offsetof3.C: New.
* testsuite/g++.dg/other/offsetof4.C: New.
2003-05-19 Kevin B. Hendricks <kevin.hendricks@sympatico.ca> 2003-05-19 Kevin B. Hendricks <kevin.hendricks@sympatico.ca>
David Edelsohn <edelsohn@gnu.org> David Edelsohn <edelsohn@gnu.org>
......
...@@ -612,6 +612,10 @@ int flag_abi_version = 1; ...@@ -612,6 +612,10 @@ int flag_abi_version = 1;
int warn_abi = 0; int warn_abi = 0;
/* Nonzero means warn about invalid uses of offsetof. */
int warn_invalid_offsetof = 1;
/* Nonzero means warn about implicit declarations. */ /* Nonzero means warn about implicit declarations. */
int warn_implicit = 1; int warn_implicit = 1;
......
...@@ -777,6 +777,10 @@ extern int flag_abi_version; ...@@ -777,6 +777,10 @@ extern int flag_abi_version;
extern int warn_abi; extern int warn_abi;
/* Nonzero means warn about invalid uses of offsetof. */
extern int warn_invalid_offsetof;
/* Nonzero means warn about implicit declarations. */ /* Nonzero means warn about implicit declarations. */
extern int warn_implicit; extern int warn_implicit;
......
...@@ -203,6 +203,7 @@ static void finish_options PARAMS ((void)); ...@@ -203,6 +203,7 @@ static void finish_options PARAMS ((void));
OPT("Wimplicit-function-declaration", CL_C, OPT_Wimplicit_function_decl) \ OPT("Wimplicit-function-declaration", CL_C, OPT_Wimplicit_function_decl) \
OPT("Wimplicit-int", CL_C, OPT_Wimplicit_int) \ OPT("Wimplicit-int", CL_C, OPT_Wimplicit_int) \
OPT("Wimport", CL_ALL, OPT_Wimport) \ OPT("Wimport", CL_ALL, OPT_Wimport) \
OPT("Winvalid-offsetof", CL_CXX, OPT_Winvalid_offsetof) \
OPT("Winvalid-pch", CL_ALL, OPT_Winvalid_pch) \ OPT("Winvalid-pch", CL_ALL, OPT_Winvalid_pch) \
OPT("Wlong-long", CL_ALL, OPT_Wlong_long) \ OPT("Wlong-long", CL_ALL, OPT_Wlong_long) \
OPT("Wmain", CL_C, OPT_Wmain) \ OPT("Wmain", CL_C, OPT_Wmain) \
...@@ -942,6 +943,10 @@ c_common_decode_option (argc, argv) ...@@ -942,6 +943,10 @@ c_common_decode_option (argc, argv)
cpp_opts->warn_import = on; cpp_opts->warn_import = on;
break; break;
case OPT_Winvalid_offsetof:
warn_invalid_offsetof = on;
break;
case OPT_Winvalid_pch: case OPT_Winvalid_pch:
cpp_opts->warn_invalid_pch = on; cpp_opts->warn_invalid_pch = on;
break; break;
......
2003-05-19 Matt Austern <austern@apple.com>
* lang-options.h: Document -Wno-invalid-offsetof
* typeck.c (build_class_member_access_expr): Don't complain about
(Foo *)p->x for non-POD Foo if warn_invalid_offset is zero.
2003-05-18 Andrew Pinski <pinskia@physics.uc.edu> 2003-05-18 Andrew Pinski <pinskia@physics.uc.edu>
* name-lookup.c (free_binding_entry): fix where the GTY markers are. * name-lookup.c (free_binding_entry): fix where the GTY markers are.
......
...@@ -157,3 +157,6 @@ DEFINE_LANG_NAME ("C++") ...@@ -157,3 +157,6 @@ DEFINE_LANG_NAME ("C++")
{ "-Wdeprecated", "" }, { "-Wdeprecated", "" },
{ "-Wno-deprecated", { "-Wno-deprecated",
N_("Don't announce deprecation of compiler features") }, N_("Don't announce deprecation of compiler features") },
{ "-Winvalid-offsetof", "" },
{ "-Wno-invalid-offsetof",
N_("Don't warn about invalid uses of the offsetof macro") },
...@@ -1974,7 +1974,8 @@ build_class_member_access_expr (tree object, tree member, ...@@ -1974,7 +1974,8 @@ build_class_member_access_expr (tree object, tree member,
give the right answer. Note that we complain whether or not they give the right answer. Note that we complain whether or not they
actually used the offsetof macro, since there's no way to know at this actually used the offsetof macro, since there's no way to know at this
point. So we just give a warning, instead of a pedwarn. */ point. So we just give a warning, instead of a pedwarn. */
if (null_object_p && CLASSTYPE_NON_POD_P (object_type)) if (null_object_p && warn_invalid_offsetof
&& CLASSTYPE_NON_POD_P (object_type))
{ {
warning ("invalid access to non-static data member `%D' of NULL object", warning ("invalid access to non-static data member `%D' of NULL object",
member); member);
......
...@@ -215,6 +215,7 @@ in the following sections. ...@@ -215,6 +215,7 @@ in the following sections.
-Wimplicit-function-declaration @gol -Wimplicit-function-declaration @gol
-Werror-implicit-function-declaration @gol -Werror-implicit-function-declaration @gol
-Wimport -Winline -Winvalid-pch -Wno-endif-labels @gol -Wimport -Winline -Winvalid-pch -Wno-endif-labels @gol
-Wno-invalid-offsetof @gol
-Wlarger-than-@var{len} -Wlong-long @gol -Wlarger-than-@var{len} -Wlong-long @gol
-Wmain -Wmissing-braces @gol -Wmain -Wmissing-braces @gol
-Wmissing-format-attribute -Wmissing-noreturn @gol -Wmissing-format-attribute -Wmissing-noreturn @gol
...@@ -2808,6 +2809,21 @@ code is to provide behavior which is selectable at compile-time. ...@@ -2808,6 +2809,21 @@ code is to provide behavior which is selectable at compile-time.
@opindex Winline @opindex Winline
Warn if a function can not be inlined and it was declared as inline. Warn if a function can not be inlined and it was declared as inline.
@item -Wno-invalid-offsetof @r{(C++ only)}
@opindex Wno-invalid-offsetof
Suppress warnings from applying the @samp{offsetof} macro to a non-POD
type. According to the 1998 ISO C++ standard, applying @samp{offsetof}
to a non-POD type is undefined. In existing C++ implementations,
however, @samp{offsetof} typically gives meaningful results even when
applied to certain kinds of non-POD types. (Such as a simple
@samp{struct} that fails to be a POD type only by virtue of having a
constructor.) This flag is for users who are aware that they are
writing nonportable code and who have deliberately chosen to ignore the
warning about it.
The restrictions on @samp{offsetof} may be relaxed in a future version
of the C++ standard.
@item -Winvalid-pch @item -Winvalid-pch
@opindex Winvalid-pch @opindex Winvalid-pch
Warn if a precompiled header (@pxref{Precompiled Headers}) is found in Warn if a precompiled header (@pxref{Precompiled Headers}) is found in
......
/* Verify that offsetof warns if given a non-POD */
/* Copyright (C) 2003 Free Software Foundation, Inc. */
/* Contributed by Matt Austern <austern@apple.com> 15 May 2003 */
/* { dg-do compile } */
struct X
{
X() : x(3), y(4) { }
int x, y;
};
typedef X* pX;
int yoff = int(&(pX(0)->y)); /* { dg-warning "invalid access" "" } */
/* { dg-warning "macro was used incorrectly" "" { target *-*-* } 14 } */
/* Verify that -Wno-invalid-offsetof disables warning */
/* Copyright (C) 2003 Free Software Foundation, Inc. */
/* Contributed by Matt Austern <austern@apple.com> 15 May 2003 */
/* { dg-do compile } */
/* { dg-options "-Wno-invalid-offsetof" } */
struct X
{
X() : x(3), y(4) { }
int x, y;
};
typedef X* pX;
int yoff = int(&(pX(0)->y));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment