From 0cb502930fc1b511dd1acab155ef5b0291d7edef Mon Sep 17 00:00:00 2001 From: pinskia Date: Tue, 19 Aug 2003 20:29:01 +0000 Subject: PR c++/10538, PR c/5582 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 | 7 +++++++ gcc/c-lang.c | 2 ++ gcc/c-opts.c | 4 ++++ gcc/c.opt | 4 ++++ gcc/cp/cp-lang.c | 2 ++ gcc/doc/invoke.texi | 23 ++++++++++++++++++++++- gcc/function.c | 6 +++--- gcc/objc/objc-lang.c | 2 ++ 8 files changed, 46 insertions(+), 4 deletions(-) (limited to 'gcc') diff --git a/gcc/c-common.h b/gcc/c-common.h index 6230ac937ef..d38a6e45072 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -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 *); diff --git a/gcc/c-lang.c b/gcc/c-lang.c index fce866266fc..f8ab4165f4e 100644 --- a/gcc/c-lang.c +++ b/gcc/c-lang.c @@ -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 diff --git a/gcc/c-opts.c b/gcc/c-opts.c index 462ca240b79..da342a89aec 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -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; diff --git a/gcc/c.opt b/gcc/c.opt index 229414f6c1a..bfe81aa3d8f 100644 --- 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++ diff --git a/gcc/cp/cp-lang.c b/gcc/cp/cp-lang.c index b8f10109e7d..8ca93610dab 100644 --- a/gcc/cp/cp-lang.c +++ b/gcc/cp/cp-lang.c @@ -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 diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 40cba57d005..029c469659d 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -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 diff --git a/gcc/function.c b/gcc/function.c index 84c1e6fcc44..fd7c7f79350 100644 --- a/gcc/function.c +++ b/gcc/function.c @@ -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); diff --git a/gcc/objc/objc-lang.c b/gcc/objc/objc-lang.c index eda8c5dd19c..7a330ce3b61 100644 --- a/gcc/objc/objc-lang.c +++ b/gcc/objc/objc-lang.c @@ -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 -- cgit v1.2.1