aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Austern <austern@apple.com>2003-05-19 19:19:46 +0000
committerMatt Austern <austern@gcc.gnu.org>2003-05-19 19:19:46 +0000
commita01fff59e70fba464453852c561213c3a24fb2c7 (patch)
treedb823fd432e76eee10d350bcaede94deebaa3066
parenteab1ff73c813a9eb0f7e6e8c784d8d41b37af687 (diff)
downloadgcc-a01fff59e70fba464453852c561213c3a24fb2c7.zip
gcc-a01fff59e70fba464453852c561213c3a24fb2c7.tar.gz
gcc-a01fff59e70fba464453852c561213c3a24fb2c7.tar.bz2
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
-rw-r--r--gcc/ChangeLog9
-rw-r--r--gcc/c-common.c4
-rw-r--r--gcc/c-common.h4
-rw-r--r--gcc/c-opts.c5
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/lang-options.h3
-rw-r--r--gcc/cp/typeck.c3
-rw-r--r--gcc/doc/invoke.texi16
-rw-r--r--gcc/testsuite/g++.dg/other/offsetof3.C15
-rw-r--r--gcc/testsuite/g++.dg/other/offsetof4.C15
10 files changed, 79 insertions, 1 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 797e9d5..f1cac2f 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+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>
David Edelsohn <edelsohn@gnu.org>
diff --git a/gcc/c-common.c b/gcc/c-common.c
index 2bef8dc..fe196b1 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 f43f6d6..425a9ea 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 e2722b3..49189c4 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 f2acb5c..15a0eb6 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+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>
* 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 ede5a58..982cc62 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 7a3c18a..21068b3 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 6a3a4a0..5a61ce7 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 0000000..f600765
--- /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 <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 } */
diff --git a/gcc/testsuite/g++.dg/other/offsetof4.C b/gcc/testsuite/g++.dg/other/offsetof4.C
new file mode 100644
index 0000000..587231e
--- /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 <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));