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. From-SVN: r70574
This commit is contained in:
parent
02a2edc088
commit
3390f9c9be
8 changed files with 46 additions and 4 deletions
|
@ -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 *);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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++
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue