]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/10538, PR c/5582
authorpinskia <pinskia@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 19 Aug 2003 20:29:01 +0000 (20:29 +0000)
committerpinskia <pinskia@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 19 Aug 2003 20:29:01 +0000 (20:29 +0000)
ChangeLog:
        * langhooks-def.h (lhd_decl_uninit): Declare.
        (LANG_HOOKS_DECL_UNINIT): New macro.
        (LANG_HOOKS_INITIALIZER): Adjust.
        * langhooks.h (struct lang_hooks): Add new field
        decl_uninit.
        * langhooks.c (lhd_decl_uninit): Define.
        * c-common.c (c_decl_uninit_1): New function.
        (c_decl_uninit): New function.
        (warn_init_self): Define.
        * c-common.h (c_decl_uninit): Declare.
        (warn_init_self): Declare.
        * c.opt: Introduce -Winit-self.
        * c-opts.c (c_common_handle_options): Set warn_init_self.
        * c-lang.c (LANG_HOOKS_DECL_UNINIT): Define.
        * objc/objc-lang.c  (LANG_HOOKS_DECL_UNINIT): Define.
        * function.c (uninitialized_vars_warning): Call the language hook.
        * doc/invoke.texi: Document -Winit-self.

cp/ChangeLog:
        * cp/cp-lang.c (LANG_HOOKS_DECL_UNINIT): Define.

testsuite:
        * gcc.dg/uninit-D.c: New Test.
        * gcc.dg/uninit-E.c: New Test.
        * gcc.dg/uninit-F.c: New Test.
        * gcc.dg/uninit-G.c: New Test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@70574 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/c-common.h
gcc/c-lang.c
gcc/c-opts.c
gcc/c.opt
gcc/cp/cp-lang.c
gcc/doc/invoke.texi
gcc/function.c
gcc/objc/objc-lang.c

index 6230ac937ef3469d7fe5e86b111aa98309355123..d38a6e450726efd3fbfd59d534863bbd339a25b5 100644 (file)
@@ -573,6 +573,12 @@ extern int warn_main;
 
 extern int warn_sequence_point;
 
+/* Nonzero means warn about uninitialized variable when it is initialized with itself. 
+   For example: int i = i;, GCC will not warn about this when warn_init_self is nonzero. */
+
+extern int warn_init_self;
+
+
 /* Nonzero means to warn about compile-time division by zero.  */
 extern int warn_div_by_zero;
 
@@ -1289,6 +1295,7 @@ extern void builtin_define_with_value (const char *, const char *, int);
 extern void c_stddef_cpp_builtins (void);
 extern void fe_file_change (const struct line_map *);
 extern int c_estimate_num_insns (tree decl);
+extern bool c_decl_uninit (tree t);
 
 /* In c-ppoutput.c  */
 extern void init_pp_output (FILE *);
index fce866266fce6d1e50ad4ea273a585d251eb3985..f8ab4165f4e1d8a2c95fb4ea757a57a65f4a9e63 100644 (file)
@@ -87,6 +87,8 @@ enum c_language_kind c_language = clk_c;
 #define LANG_HOOKS_FUNCTION_LEAVE_NESTED c_pop_function_context
 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL c_dup_lang_specific_decl
+#undef LANG_HOOKS_DECL_UNINIT
+#define LANG_HOOKS_DECL_UNINIT c_decl_uninit
 
 /* Attribute hooks.  */
 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
index 462ca240b79ce4669bc7a9a0f09cb2b91b251200..da342a89aec19a7ee0d60d1abceb9081117e18dc 100644 (file)
@@ -475,6 +475,10 @@ c_common_handle_option (size_t scode, const char *arg, int value)
       warn_format_zero_length = value;
       break;
 
+    case OPT_Winit_self:
+      warn_init_self = value;
+      break;
+
     case OPT_Wimplicit:
       set_Wimplicit (value);
       break;
index 229414f6c1a7c2815158c41189ad5eeeee8f67ca..bfe81aa3d8f67bf9905b43e05c0913a38d0de55b 100644 (file)
--- a/gcc/c.opt
+++ b/gcc/c.opt
@@ -238,6 +238,10 @@ C ObjC
 Wformat=
 C ObjC C++ ObjC++ Joined
 
+Winit-self
+C ObjC C++ ObjC++
+Warn about variables which are initialized to themselves.
+
 Wimplicit
 C ObjC C++ ObjC++
 
index b8f10109e7db16c06fd8fbfd31b4a4b50b8a0a12..8ca93610dab806ec692126632e892e35fde6abea 100644 (file)
@@ -106,6 +106,8 @@ static void cxx_initialize_diagnostics (diagnostic_context *);
 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
 #undef LANG_HOOKS_WRITE_GLOBALS
 #define LANG_HOOKS_WRITE_GLOBALS lhd_do_nothing
+#undef LANG_HOOKS_DECL_UNINIT
+#define LANG_HOOKS_DECL_UNINIT c_decl_uninit
 
 
 #undef LANG_HOOKS_FUNCTION_INIT
index 40cba57d005bf27095af1b4884b25bde8562f14a..029c469659dc0a085b1e5547380c9800ee4b258e 100644 (file)
@@ -214,7 +214,7 @@ in the following sections.
 -Wimplicit  -Wimplicit-int  @gol
 -Wimplicit-function-declaration @gol
 -Werror-implicit-function-declaration @gol
--Wimport  -Winline  -Winvalid-pch  -Wno-endif-labels @gol
+-Winit-self Wimport  -Winline  -Winvalid-pch  -Wno-endif-labels @gol
 -Wno-invalid-offsetof @gol
 -Wlarger-than-@var{len}  -Wlong-long @gol
 -Wmain  -Wmissing-braces @gol
@@ -2055,6 +2055,24 @@ requiring a non-null value by the @code{nonnull} function attribute.
 @option{-Wnonnull} is included in @option{-Wall} and @option{-Wformat}.  It
 can be disabled with the @option{-Wno-nonnull} option.
 
+@item -Winit-self @r{(C, C++, and Objective-C only)} 
+@opindex Winit-self
+Enable warning about uninitialized variables which are initalized with themselves.
+Note this option can only be used with the @option{-Wuninitialized} option and 
+that only works with @option{-O}.
+
+For an example, the following code will not warn about i being uninitialized 
+without this option:
+@smallexample
+@group
+int f()
+@{
+  int i = i;
+  return i;
+@}
+@end group
+@end smallexample
+
 @item -Wimplicit-int
 @opindex Wimplicit-int
 Warn when a declaration does not specify a type.
@@ -2265,6 +2283,9 @@ because they require data flow information that is computed only
 when optimizing.  If you don't specify @option{-O}, you simply won't
 get these warnings.
 
+If you want to warn about code which uses the uninitialized value of the
+variable in its own initializer, use the @option{-Winit-self} option.
+
 These warnings occur only for variables that are candidates for
 register allocation.  Therefore, they do not occur for a variable that
 is declared @code{volatile}, or whose address is taken, or whose size
index 84c1e6fcc44d0d60a60f77cf830be6711c615dbb..fd7c7f793503002132dda18b5b9b765a813929a0 100644 (file)
@@ -5564,9 +5564,9 @@ uninitialized_vars_warning (tree block)
             with a nonzero DECL_INITIAL had an initializer, so do not
             claim it is potentially uninitialized.
 
-            We do not care about the actual value in DECL_INITIAL, so we do
-            not worry that it may be a dangling pointer.  */
-         && DECL_INITIAL (decl) == NULL_TREE
+            When the DECL_INITIAL is NULL call the language hook to tell us
+            if we want to warn.  */
+         && (DECL_INITIAL (decl) == NULL_TREE || lang_hooks.decl_uninit (decl))
          && regno_uninitialized (REGNO (DECL_RTL (decl))))
        warning ("%H'%D' might be used uninitialized in this function",
                  &DECL_SOURCE_LOCATION (decl), decl);
index eda8c5dd19cff1b063b7c367edf84d263953fc64..7a330ce3b61249eb32152fec7e7ab0213e944de3 100644 (file)
@@ -79,6 +79,8 @@ enum c_language_kind c_language = clk_objc;
 #define LANG_HOOKS_DECL_PRINTABLE_NAME objc_printable_name
 #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL c_warn_unused_global_decl
+#undef LANG_HOOKS_DECL_UNINIT
+#define LANG_HOOKS_DECL_UNINIT c_decl_uninit
 
 #undef LANG_HOOKS_FUNCTION_ENTER_NESTED
 #define LANG_HOOKS_FUNCTION_ENTER_NESTED c_push_function_context