This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch] Fix PR 5582 and 10538, missed uninitialized variable warning


This is a follow up with my pervious patch which did not compile at all in any non C language.
This fixes PR 5582 only when pedantic is on because it as a GCC extension to do int i = i; to turn
off the uninitialized warnings for that variable.
I also do not warn about int *x = &x; because x is initialized.


There is one case where I warn when we should not: int *x = &(x+2);.
I will add some testcases when approved.

Testing on i686-unknown-openbsd3.1.

Okay if the no regressions (or with a follow up to fix the testcases)?


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.
	* c-common.h (c_decl_uninit): Declare.
	* 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.

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


Patch: Index: langhooks-def.h =================================================================== RCS file: /cvs/gcc/gcc/gcc/langhooks-def.h,v retrieving revision 1.60 diff -u -p -r1.60 langhooks-def.h --- langhooks-def.h 4 Aug 2003 20:44:13 -0000 1.60 +++ langhooks-def.h 17 Aug 2003 04:20:05 -0000 @@ -63,6 +63,7 @@ extern void lhd_incomplete_type_error (t extern tree lhd_type_promotes_to (tree); extern bool lhd_decl_ok_for_sibcall (tree); extern tree lhd_expr_size (tree); +extern bool lhd_decl_uninit (tree); extern size_t lhd_tree_size (enum tree_code);

 /* Declarations of default tree inlining hooks.  */
@@ -114,6 +115,7 @@ extern void lhd_initialize_diagnostics (
 #define LANG_HOOKS_PRINT_ERROR_FUNCTION lhd_print_error_function
 #define LANG_HOOKS_DECL_PRINTABLE_NAME	lhd_decl_printable_name
 #define LANG_HOOKS_EXPR_SIZE		lhd_expr_size
+#define LANG_HOOKS_DECL_UNINIT		lhd_decl_uninit
 #define LANG_HOOKS_TREE_SIZE		lhd_tree_size

#define LANG_HOOKS_FUNCTION_INIT lhd_do_nothing_f
@@ -278,6 +280,7 @@ extern int lhd_tree_dump_type_quals (tre
LANG_HOOKS_DECL_PRINTABLE_NAME, \
LANG_HOOKS_PRINT_ERROR_FUNCTION, \
LANG_HOOKS_EXPR_SIZE, \
+ LANG_HOOKS_DECL_UNINIT, \
LANG_HOOKS_ATTRIBUTE_TABLE, \
LANG_HOOKS_COMMON_ATTRIBUTE_TABLE, \
LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE, \
Index: langhooks.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks.c,v
retrieving revision 1.48
diff -u -p -r1.48 langhooks.c
--- langhooks.c 3 Aug 2003 22:14:47 -0000 1.48
+++ langhooks.c 17 Aug 2003 04:20:05 -0000
@@ -430,6 +430,14 @@ lhd_expr_size (tree exp)
else
return size_in_bytes (TREE_TYPE (exp));
}
+/* lang_hooks.decl_uninit: Find out if a variable is uninitialized based
+ on DECL_INITIAL. */
+
+bool
+lhd_decl_uninit (tree t ATTRIBUTE_UNUSED)
+{
+ return false;
+}


 /* lang_hooks.tree_size: Determine the size of a tree with code C,
    which is a language-specific tree code in category 'x'.  The
Index: langhooks.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks.h,v
retrieving revision 1.69
diff -u -p -r1.69 langhooks.h
--- langhooks.h	4 Aug 2003 20:44:13 -0000	1.69
+++ langhooks.h	17 Aug 2003 04:20:05 -0000
@@ -361,6 +361,10 @@ struct lang_hooks
      semantics in cases that it doesn't want to handle specially.  */
   tree (*expr_size) (tree);

+ /* Called from uninitialized_vars_warning to find out if a variable is
+ uninitialized based on DECL_INITIAL. */
+ bool (*decl_uninit) (tree);
+
/* Pointers to machine-independent attribute tables, for front ends
using attribs.c. If one is NULL, it is ignored. Respectively, a
table of attributes specific to the language, a table of
Index: c-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.445
diff -u -p -r1.445 c-common.c
--- c-common.c 5 Aug 2003 21:15:52 -0000 1.445
+++ c-common.c 17 Aug 2003 04:20:06 -0000
@@ -5727,6 +5715,36 @@ c_estimate_num_insns (tree decl)
int num = 0;
walk_tree_without_duplicates (&DECL_SAVED_TREE (decl), c_estimate_num_insns_1, &num);
return num;
+}
+
+/* Used by c_decl_uninit to find where expressions like x = x + 1; */
+static tree
+c_decl_uninit_1 (tree *t, int *walk_sub_trees, void *x)
+{
+ /* If x = EXP(&x)EXP, then do not warn about the use of x.
+ FIXME: Should not warn about this: int *x = &(x+2);. */
+ if (TREE_CODE (*t) == ADDR_EXPR && TREE_OPERAND (*t, 0) == x)
+ {
+ *walk_sub_trees = 0;
+ return NULL_TREE;
+ }
+ if (*t == x)
+ return *t;
+ return NULL_TREE;
+}
+
+/* Find out if a variable is uninitialized based on DECL_INITIAL. */
+bool
+c_decl_uninit (tree t)
+{
+ /* int x = x; is GCC extension to turn off this warning, only if not pedantic. */
+ if (DECL_INITIAL (t) == t)
+ return pedantic ? true : false;
+
+ /* Walk the trees look for the variable it self. */
+ if (walk_tree_without_duplicates (&DECL_INITIAL (t), c_decl_uninit_1, t))
+ return true;
+ return false;
}


 #include "gt-c-common.h"
Index: c-common.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.h,v
retrieving revision 1.202
diff -u -p -r1.202 c-common.h
--- c-common.h	5 Aug 2003 21:15:52 -0000	1.202
+++ c-common.h	17 Aug 2003 04:20:06 -0000
@@ -1289,6 +1288,7 @@ extern void builtin_define_with_value (c
 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: c-lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-lang.c,v
retrieving revision 1.113
diff -u -p -r1.113 c-lang.c
--- c-lang.c	10 Aug 2003 18:17:05 -0000	1.113
+++ c-lang.c	17 Aug 2003 04:20:06 -0000
@@ -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: function.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/function.c,v
retrieving revision 1.449
diff -u -p -r1.449 function.c
--- function.c	25 Jul 2003 09:52:25 -0000	1.449
+++ function.c	17 Aug 2003 04:20:08 -0000
@@ -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: objc/objc-lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/objc/objc-lang.c,v
retrieving revision 1.37
diff -u -p -r1.37 objc-lang.c
--- objc/objc-lang.c 4 Aug 2003 20:44:20 -0000 1.37
+++ objc/objc-lang.c 17 Aug 2003 04:20:09 -0000
@@ -79,6 +79,8 @@ enum c_language_kind c_language = clk_ob
#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
Index: cp/cp-lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-lang.c,v
retrieving revision 1.60
diff -u -p -r1.60 cp-lang.c
--- cp/cp-lang.c	5 Aug 2003 19:23:14 -0000	1.60
+++ cp/cp-lang.c	17 Aug 2003 04:20:09 -0000
@@ -106,6 +106,8 @@ static void cxx_initialize_diagnostics (
 #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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]