Commit 2d48bdca by Dodji Seketeli Committed by Dodji Seketeli

Fix -Wuninitialized for -ftrack-macro-expansion

Besides the warning emitted by warn_uninit, this function wants
to hint the user at where the uninitialized variable was declared, for
cases where the declaration location is outside the current function.

Now that expand_location expands to the location that is in the main
source file (even for -ftrack-macro-expansion) the hinting part was
not working well for cases where the variable is declared in a macro
(outside the function), which is then expanded in the function.

So I had to adjust warn_uninit a little bit to make it consider the
spelling location of the variable declaration.

I have fixed the test gcc.dg/cpp/pragma-diagnostic-2.c on which I
believe gcc shouldn't emit any error.

Here is the new output on that test:

=~=
gcc.dg/cpp/pragma-diagnostic-2.c: In function 'g':
gcc.dg/cpp/pragma-diagnostic-2.c:10:5: warning: 'a' is used uninitialized in this function [-Wuninitialized]
gcc.dg/cpp/pragma-diagnostic-2.c:9:7: note: 'a' was declared here
gcc.dg/cpp/pragma-diagnostic-2.c:9:7: note: in expansion of macro 'CODE_WITH_WARNING'
gcc.dg/cpp/pragma-diagnostic-2.c:17:3: note: expanded from here
gcc.dg/cpp/pragma-diagnostic-2.c: In function 'h':
gcc.dg/cpp/pragma-diagnostic-2.c:10:5: warning: 'a' is used uninitialized in this function [-Wuninitialized]
gcc.dg/cpp/pragma-diagnostic-2.c:9:7: note: 'a' was declared here
gcc.dg/cpp/pragma-diagnostic-2.c:9:7: note: in expansion of macro 'CODE_WITH_WARNING'
gcc.dg/cpp/pragma-diagnostic-2.c:27:3: note: expanded from here
=~=

Tested and bootstrapped on x86_64-unknown-linux-gnu against trunk.

Note that the bootstrap with -ftrack-macro-expansion turned on
exhibits other separate issues that are addressed in subsequent
patches.  This patch just fixes one class of problems.

The patch does pass bootstrap with -ftrack-macro-expansion turned off,
though.

gcc/
	* tree-ssa.c (warn_uninit): Use the spelling location of the
	variable declaration.  Use linemap_location_before_p for source
	locations.

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-2.c:  Fix this.

From-SVN: r186971
parent c4ca1a09
2012-04-30 Dodji Seketeli <dodji@redhat.com> 2012-04-30 Dodji Seketeli <dodji@redhat.com>
Fix -Wuninitialized for -ftrack-macro-expansion
* tree-ssa.c (warn_uninit): Use the spelling location of the
variable declaration. Use linemap_location_before_p for source
locations.
Strip "<built-in>" loc from displayed expansion context Strip "<built-in>" loc from displayed expansion context
* input.c (expand_location_1): When expanding to spelling location * input.c (expand_location_1): When expanding to spelling location
in a context of a macro expansion, skip reserved system header in a context of a macro expansion, skip reserved system header
......
2012-04-30 Dodji Seketeli <dodji@redhat.com> 2012-04-30 Dodji Seketeli <dodji@redhat.com>
Fix -Wuninitialized for -ftrack-macro-expansion
* gcc.dg/cpp/pragma-diagnostic-2.c: Fix this.
Strip "<built-in>" loc from displayed expansion context Strip "<built-in>" loc from displayed expansion context
* g++.dg/warn/Wconversion-real-integer2.C: New test. * g++.dg/warn/Wconversion-real-integer2.C: New test.
* g++.dg/warn/Wconversion-real-integer-3.C: Likewise. * g++.dg/warn/Wconversion-real-integer-3.C: Likewise.
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
void f (unsigned); void f (unsigned);
#define CODE_WITH_WARNING \ #define CODE_WITH_WARNING \
int a; /* { dg-message "expansion|declared here" } */ \ int a; /* { dg-message "was declared here" } */ \
f (a) /* { dg-message "expansion" } */ f (a) /* { dg-warning "used uninitialized" } */
#pragma GCC diagnostic ignored "-Wuninitialized" #pragma GCC diagnostic ignored "-Wuninitialized"
...@@ -26,9 +26,3 @@ h (void) ...@@ -26,9 +26,3 @@ h (void)
{ {
CODE_WITH_WARNING; /* { dg-message "expanded" } */ CODE_WITH_WARNING; /* { dg-message "expanded" } */
} }
/*
{ dg-message "some warnings being treated as errors" "" {target *-*-*} 0 }
*/
/* { dg-error "uninitialized" "" { target *-*-* } { 10 } } */
...@@ -1613,7 +1613,7 @@ warn_uninit (enum opt_code wc, tree t, ...@@ -1613,7 +1613,7 @@ warn_uninit (enum opt_code wc, tree t,
tree expr, tree var, const char *gmsgid, void *data) tree expr, tree var, const char *gmsgid, void *data)
{ {
gimple context = (gimple) data; gimple context = (gimple) data;
location_t location; location_t location, cfun_loc;
expanded_location xloc, floc; expanded_location xloc, floc;
if (!ssa_undefined_value_p (t)) if (!ssa_undefined_value_p (t))
...@@ -1631,8 +1631,12 @@ warn_uninit (enum opt_code wc, tree t, ...@@ -1631,8 +1631,12 @@ warn_uninit (enum opt_code wc, tree t,
location = (context != NULL && gimple_has_location (context)) location = (context != NULL && gimple_has_location (context))
? gimple_location (context) ? gimple_location (context)
: DECL_SOURCE_LOCATION (var); : DECL_SOURCE_LOCATION (var);
location = linemap_resolve_location (line_table, location,
LRK_SPELLING_LOCATION,
NULL);
cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
xloc = expand_location (location); xloc = expand_location (location);
floc = expand_location (DECL_SOURCE_LOCATION (cfun->decl)); floc = expand_location (cfun_loc);
if (warning_at (location, wc, gmsgid, expr)) if (warning_at (location, wc, gmsgid, expr))
{ {
TREE_NO_WARNING (expr) = 1; TREE_NO_WARNING (expr) = 1;
...@@ -1640,8 +1644,11 @@ warn_uninit (enum opt_code wc, tree t, ...@@ -1640,8 +1644,11 @@ warn_uninit (enum opt_code wc, tree t,
if (location == DECL_SOURCE_LOCATION (var)) if (location == DECL_SOURCE_LOCATION (var))
return; return;
if (xloc.file != floc.file if (xloc.file != floc.file
|| xloc.line < floc.line || linemap_location_before_p (line_table,
|| xloc.line > LOCATION_LINE (cfun->function_end_locus)) location, cfun_loc)
|| linemap_location_before_p (line_table,
cfun->function_end_locus,
location))
inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var); inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment