Commit 7eb918cc by Dodji Seketeli Committed by Dodji Seketeli

Make expand_location resolve to locus in main source file

Apparently, quite some places in the compiler (like the C/C++
preprocessor, the debug info machinery) expect expand_location to
resolve to locations that are in the main source file, even if the
token at stake comes from a macro that was defined in a header
somewhere.  Turning on -ftrack-macro-expansion by default was
triggering a lot of failures (not necessarily related to diagnostics)
because expand_location resolves to spelling locations instead.

So I have changed expand_location to honour the initial expectation.

In addition, I came up with the new expand_location_to_spelling_point
used in diagnostic_build_prefix because the diagnostic system, on the
other hand, wants to point to the location of the token where it was
spelled, and then display the error context involving all the macro
whose expansion led to that spelling point - if we are in the context
of a macro expansion there.

This seems to me like a reasonable balance.

Tested and bootstrapped on x86_64-unknown-linux-gnu against trunk and
whitnessed that a lot more tests were PASSing.

Note that the bootstrap with -ftrack-macro-expansion 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/

	* input.c (expand_location_1): New.  Takes a parameter to choose
	whether to resolve the location to spelling or expansion point.
	Was factorized from ...
	(expand_location): ... here.
	(expand_location_to_spelling_point): New.  Implemented in terms of
	expand_location_1.
	* diagnostic.c (diagnostic_build_prefix): Use the new
	expand_location_to_spelling_point instead of expand_location.

From-SVN: r186969
parent 3600218c
2012-04-30 Dodji Seketeli <dodji@redhat.com> 2012-04-30 Dodji Seketeli <dodji@redhat.com>
Make expand_location resolve to locus in main source file
* input.c (expand_location_1): New. Takes a parameter to choose
whether to resolve the location to spelling or expansion point.
Was factorized from ...
(expand_location): ... here.
(expand_location_to_spelling_point): New. Implemented in terms of
expand_location_1.
* diagnostic.c (diagnostic_build_prefix): Use the new
expand_location_to_spelling_point instead of expand_location.
Fix PCH crash on GTYed pointer-to-scalar field of a struct Fix PCH crash on GTYed pointer-to-scalar field of a struct
* gengtype.c (write_types_process_field): Force second argument of * gengtype.c (write_types_process_field): Force second argument of
the call to the PCH object hierarchy walker to be 'x'. the call to the PCH object hierarchy walker to be 'x'.
......
...@@ -214,7 +214,7 @@ diagnostic_build_prefix (diagnostic_context *context, ...@@ -214,7 +214,7 @@ diagnostic_build_prefix (diagnostic_context *context,
"must-not-happen" "must-not-happen"
}; };
const char *text = _(diagnostic_kind_text[diagnostic->kind]); const char *text = _(diagnostic_kind_text[diagnostic->kind]);
expanded_location s = expand_location (diagnostic->location); expanded_location s = expand_location_to_spelling_point (diagnostic->location);
if (diagnostic->override_column) if (diagnostic->override_column)
s.column = diagnostic->override_column; s.column = diagnostic->override_column;
gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
...@@ -266,7 +266,7 @@ diagnostic_show_locus (diagnostic_context * context, ...@@ -266,7 +266,7 @@ diagnostic_show_locus (diagnostic_context * context,
|| diagnostic->location <= BUILTINS_LOCATION) || diagnostic->location <= BUILTINS_LOCATION)
return; return;
s = expand_location(diagnostic->location); s = expand_location_to_spelling_point (diagnostic->location);
line = location_get_source_line (s); line = location_get_source_line (s);
if (line == NULL) if (line == NULL)
return; return;
......
...@@ -32,16 +32,22 @@ struct line_maps *line_table; ...@@ -32,16 +32,22 @@ struct line_maps *line_table;
/* Expand the source location LOC into a human readable location. If /* Expand the source location LOC into a human readable location. If
LOC resolves to a builtin location, the file name of the readable LOC resolves to a builtin location, the file name of the readable
location is set to the string "<built-in>". */ location is set to the string "<built-in>". If EXPANSION_POINT_P is
TRUE and LOC is virtual, then it is resolved to the expansion
expanded_location point of the involved macro. Otherwise, it is resolved to the
expand_location (source_location loc) spelling location of the token. */
static expanded_location
expand_location_1 (source_location loc,
bool expansion_point_p)
{ {
expanded_location xloc; expanded_location xloc;
const struct line_map *map; const struct line_map *map;
loc = linemap_resolve_location (line_table, loc, loc = linemap_resolve_location (line_table, loc,
LRK_SPELLING_LOCATION, &map); expansion_point_p
? LRK_MACRO_EXPANSION_POINT
: LRK_SPELLING_LOCATION, &map);
xloc = linemap_expand_location (line_table, map, loc); xloc = linemap_expand_location (line_table, map, loc);
if (loc <= BUILTINS_LOCATION) if (loc <= BUILTINS_LOCATION)
...@@ -109,6 +115,30 @@ location_get_source_line(expanded_location xloc) ...@@ -109,6 +115,30 @@ location_get_source_line(expanded_location xloc)
return buffer; return buffer;
} }
/* Expand the source location LOC into a human readable location. If
LOC is virtual, it resolves to the expansion point of the involved
macro. If LOC resolves to a builtin location, the file name of the
readable location is set to the string "<built-in>". */
expanded_location
expand_location (source_location loc)
{
return expand_location_1 (loc, /*expansion_point_p=*/true);
}
/* Expand the source location LOC into a human readable location. If
LOC is virtual, it resolves to the expansion location of the
relevant macro. If LOC resolves to a builtin location, the file
name of the readable location is set to the string
"<built-in>". */
expanded_location
expand_location_to_spelling_point (source_location loc)
{
return expand_location_1 (loc, /*expansion_piont_p=*/false);
}
#define ONE_K 1024 #define ONE_K 1024
#define ONE_M (ONE_K * ONE_K) #define ONE_M (ONE_K * ONE_K)
......
...@@ -39,6 +39,7 @@ extern char builtins_location_check[(BUILTINS_LOCATION ...@@ -39,6 +39,7 @@ extern char builtins_location_check[(BUILTINS_LOCATION
extern expanded_location expand_location (source_location); extern expanded_location expand_location (source_location);
extern const char * location_get_source_line(expanded_location xloc); extern const char * location_get_source_line(expanded_location xloc);
extern expanded_location expand_location_to_spelling_point (source_location);
/* Historically GCC used location_t, while cpp used source_location. /* Historically GCC used location_t, while cpp used source_location.
This could be removed but it hardly seems worth the effort. */ This could be removed but it hardly seems worth the effort. */
......
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