Add a new flag...
authorMatt Austern <austern@apple.com>
Mon, 19 May 2003 19:19:46 +0000 (19:19 +0000)
committerMatt Austern <austern@gcc.gnu.org>
Mon, 19 May 2003 19:19:46 +0000 (19:19 +0000)
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

gcc/ChangeLog
gcc/c-common.c
gcc/c-common.h
gcc/c-opts.c
gcc/cp/ChangeLog
gcc/cp/lang-options.h
gcc/cp/typeck.c
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/other/offsetof3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/other/offsetof4.C [new file with mode: 0644]

index 797e9d530833921e003a46bf3432b644739d0be1..f1cac2f2be79616af0d6a2fa1f9e9feaa79936c7 100644 (file)
@@ -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>
 
index 2bef8dccf86fb16d20d8e02cd8a8277d5edbe613..fe196b124bf3331f2dce916e614da56aec691bab 100644 (file)
@@ -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;
index f43f6d66d956e4fc1181c809fe4775d76421bddb..425a9eab02f8af6ab4f98002bc07236c3c1db8e9 100644 (file)
@@ -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;
index e2722b37f743aeeb5f900d82903b8f09154fec03..49189c4d28991b614a8108c641e1a633f653d095 100644 (file)
@@ -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;
index f2acb5c46db7f6a559d5a024fde9ca1e7aa29711..15a0eb60e01110e9f7342999bcca1112ddba11b2 100644 (file)
@@ -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.
index ede5a58f95aa7560a138ea5cdfa98ca7c2f83b7f..982cc62258e48dceebf9f01f5a712ccea1e1183d 100644 (file)
@@ -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") },
index 7a3c18a59f520470d0a9b3f5a513b84a5a635f65..21068b321eb20930bf5cc8385ace55fbd754ebd9 100644 (file)
@@ -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);
index 6a3a4a039b446519a8de52d9c95d6780b388d189..5a61ce73738d2c46d98c447b3553f9720497e6c9 100644 (file)
@@ -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 (file)
index 0000000..f600765
--- /dev/null
@@ -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 (file)
index 0000000..587231e
--- /dev/null
@@ -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));