From: Matt Austern Date: Mon, 19 May 2003 19:19:46 +0000 (+0000) Subject: Add a new flag... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a01fff59e70fba464453852c561213c3a24fb2c7;p=gcc.git 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 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 797e9d53083..f1cac2f2be7 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2003-05-19 Matt Austern + + * 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 David Edelsohn diff --git a/gcc/c-common.c b/gcc/c-common.c index 2bef8dccf86..fe196b124bf 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -612,6 +612,10 @@ int flag_abi_version = 1; int warn_abi = 0; +/* Nonzero means warn about invalid uses of offsetof. */ + +int warn_invalid_offsetof = 1; + /* Nonzero means warn about implicit declarations. */ int warn_implicit = 1; diff --git a/gcc/c-common.h b/gcc/c-common.h index f43f6d66d95..425a9eab02f 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -777,6 +777,10 @@ extern int flag_abi_version; extern int warn_abi; +/* Nonzero means warn about invalid uses of offsetof. */ + +extern int warn_invalid_offsetof; + /* Nonzero means warn about implicit declarations. */ extern int warn_implicit; diff --git a/gcc/c-opts.c b/gcc/c-opts.c index e2722b37f74..49189c4d289 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -203,6 +203,7 @@ static void finish_options PARAMS ((void)); OPT("Wimplicit-function-declaration", CL_C, OPT_Wimplicit_function_decl) \ OPT("Wimplicit-int", CL_C, OPT_Wimplicit_int) \ OPT("Wimport", CL_ALL, OPT_Wimport) \ + OPT("Winvalid-offsetof", CL_CXX, OPT_Winvalid_offsetof) \ OPT("Winvalid-pch", CL_ALL, OPT_Winvalid_pch) \ OPT("Wlong-long", CL_ALL, OPT_Wlong_long) \ OPT("Wmain", CL_C, OPT_Wmain) \ @@ -942,6 +943,10 @@ c_common_decode_option (argc, argv) cpp_opts->warn_import = on; break; + case OPT_Winvalid_offsetof: + warn_invalid_offsetof = on; + break; + case OPT_Winvalid_pch: cpp_opts->warn_invalid_pch = on; break; diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index f2acb5c46db..15a0eb60e01 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2003-05-19 Matt Austern + + * 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 * name-lookup.c (free_binding_entry): fix where the GTY markers are. diff --git a/gcc/cp/lang-options.h b/gcc/cp/lang-options.h index ede5a58f95a..982cc62258e 100644 --- a/gcc/cp/lang-options.h +++ b/gcc/cp/lang-options.h @@ -157,3 +157,6 @@ DEFINE_LANG_NAME ("C++") { "-Wdeprecated", "" }, { "-Wno-deprecated", N_("Don't announce deprecation of compiler features") }, + { "-Winvalid-offsetof", "" }, + { "-Wno-invalid-offsetof", + N_("Don't warn about invalid uses of the offsetof macro") }, diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 7a3c18a59f5..21068b321eb 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -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 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. */ - 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", member); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 6a3a4a039b4..5a61ce73738 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -215,6 +215,7 @@ in the following sections. -Wimplicit-function-declaration @gol -Werror-implicit-function-declaration @gol -Wimport -Winline -Winvalid-pch -Wno-endif-labels @gol +-Wno-invalid-offsetof @gol -Wlarger-than-@var{len} -Wlong-long @gol -Wmain -Wmissing-braces @gol -Wmissing-format-attribute -Wmissing-noreturn @gol @@ -2808,6 +2809,21 @@ code is to provide behavior which is selectable at compile-time. @opindex Winline 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 @opindex Winvalid-pch Warn if a precompiled header (@pxref{Precompiled Headers}) is found in diff --git a/gcc/testsuite/g++.dg/other/offsetof3.C b/gcc/testsuite/g++.dg/other/offsetof3.C new file mode 100644 index 00000000000..f600765fd90 --- /dev/null +++ b/gcc/testsuite/g++.dg/other/offsetof3.C @@ -0,0 +1,15 @@ +/* Verify that offsetof warns if given a non-POD */ +/* Copyright (C) 2003 Free Software Foundation, Inc. */ +/* Contributed by Matt Austern 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 } */ diff --git a/gcc/testsuite/g++.dg/other/offsetof4.C b/gcc/testsuite/g++.dg/other/offsetof4.C new file mode 100644 index 00000000000..587231ef88e --- /dev/null +++ b/gcc/testsuite/g++.dg/other/offsetof4.C @@ -0,0 +1,15 @@ +/* Verify that -Wno-invalid-offsetof disables warning */ +/* Copyright (C) 2003 Free Software Foundation, Inc. */ +/* Contributed by Matt Austern 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));