Re: [PATCH 05/11] Make expand_location resolve to locus in main source file
Jason Merrill writes: > On 04/25/2012 11:31 AM, Dodji Seketeli wrote: >> +#define EXPANSION_POINT_LOCATION_FILE(LOC) \ >> + ((expand_location_to_expansion_point (LOC)).file) >> +#define EXPANSION_POINT_LOCATION_LINE(LOC) \ >> + ((expand_location_to_expansion_point (LOC)).line) >> +#define EXPANSION_POINT_LOCATION_COLUMN(LOC)\ >> + ((expand_location_to_expansion_point (LOC)).column) > > These macros don't seem to be used anywhere. The rest of the patch is > OK. Thanks. Here is the updated patch that I will commit with the rest when all the patches are ACKed. 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. --- gcc/diagnostic.c |4 ++-- gcc/input.c | 40 +++- gcc/input.h |1 + 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index 4496803..729e865 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -214,7 +214,7 @@ diagnostic_build_prefix (diagnostic_context *context, "must-not-happen" }; 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) s.column = diagnostic->override_column; gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); @@ -266,7 +266,7 @@ diagnostic_show_locus (diagnostic_context * context, || diagnostic->location <= BUILTINS_LOCATION) return; - s = expand_location(diagnostic->location); + s = expand_location_to_spelling_point (diagnostic->location); line = location_get_source_line (s); if (line == NULL) return; diff --git a/gcc/input.c b/gcc/input.c index bf5fe48..e9ba301 100644 --- a/gcc/input.c +++ b/gcc/input.c @@ -32,16 +32,22 @@ struct line_maps *line_table; /* Expand the source location LOC into a human readable location. If LOC resolves to a builtin location, the file name of the readable - location is set to the string "". */ - -expanded_location -expand_location (source_location loc) + location is set to the string "". If EXPANSION_POINT_P is + TRUE and LOC is virtual, then it is resolved to the expansion + point of the involved macro. Otherwise, it is resolved to the + spelling location of the token. */ + +static expanded_location +expand_location_1 (source_location loc, + bool expansion_point_p) { expanded_location xloc; const struct line_map *map; 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); if (loc <= BUILTINS_LOCATION) @@ -109,6 +115,30 @@ location_get_source_line(expanded_location xloc) 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 "". */ + +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 + "". */ + +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_M (ONE_K * ONE_K) diff --git a/gcc/input.h b/gcc/input.h index 4b15222..ea19e07 100644 --- a/gcc/input.h +++ b/gcc/input.h @@ -39,6 +39,7 @@ extern char builtins_location_check[(BUILTINS_LOCATION extern expanded_location expand_location (source_location); 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. This could be removed but it hardly seems worth the effort. */ -- Dodji
Re: [PATCH 05/11] Make expand_location resolve to locus in main source file
On 04/25/2012 11:31 AM, Dodji Seketeli wrote: +#define EXPANSION_POINT_LOCATION_FILE(LOC) \ + ((expand_location_to_expansion_point (LOC)).file) +#define EXPANSION_POINT_LOCATION_LINE(LOC) \ + ((expand_location_to_expansion_point (LOC)).line) +#define EXPANSION_POINT_LOCATION_COLUMN(LOC) \ + ((expand_location_to_expansion_point (LOC)).column) These macros don't seem to be used anywhere. The rest of the patch is OK. Jason
Re: [PATCH 05/11] Make expand_location resolve to locus in main source file
I have re-based my tree on top of a recent tree that incorporates the changes for caret diagnostics and I had to update this patch accordingly. Here is its new version on trunk of today. It basically updates the new diagnostic_show_locus function to point to spelling locations. The patch does pass bootstrap with -ftrack-macro-expansion turned off, and passes bootstrap with the full series with -ftrack-macro-expansion turned on. 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. --- gcc/diagnostic.c |4 ++-- gcc/input.c | 40 +++- gcc/input.h |9 + 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index 4496803..729e865 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -214,7 +214,7 @@ diagnostic_build_prefix (diagnostic_context *context, "must-not-happen" }; 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) s.column = diagnostic->override_column; gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); @@ -266,7 +266,7 @@ diagnostic_show_locus (diagnostic_context * context, || diagnostic->location <= BUILTINS_LOCATION) return; - s = expand_location(diagnostic->location); + s = expand_location_to_spelling_point (diagnostic->location); line = location_get_source_line (s); if (line == NULL) return; diff --git a/gcc/input.c b/gcc/input.c index bf5fe48..e9ba301 100644 --- a/gcc/input.c +++ b/gcc/input.c @@ -32,16 +32,22 @@ struct line_maps *line_table; /* Expand the source location LOC into a human readable location. If LOC resolves to a builtin location, the file name of the readable - location is set to the string "". */ - -expanded_location -expand_location (source_location loc) + location is set to the string "". If EXPANSION_POINT_P is + TRUE and LOC is virtual, then it is resolved to the expansion + point of the involved macro. Otherwise, it is resolved to the + spelling location of the token. */ + +static expanded_location +expand_location_1 (source_location loc, + bool expansion_point_p) { expanded_location xloc; const struct line_map *map; 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); if (loc <= BUILTINS_LOCATION) @@ -109,6 +115,30 @@ location_get_source_line(expanded_location xloc) 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 "". */ + +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 + "". */ + +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_M (ONE_K * ONE_K) diff --git a/gcc/input.h b/gcc/input.h index 4b15222..f755cdf 100644 --- a/gcc/input.h +++ b/gcc/input.h @@ -39,6 +39,7 @@ extern char builtins_location_check[(BUILTINS_LOCATION extern expanded_location expand_location (source_location); 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. This could be removed but it hardly seems worth the effort. */ @@ -50,6 +51,14 @@ extern location_t input_location; #define LOCATION_LINE(LOC) ((expand_location (LOC)).line) #define LOCATION_COLUMN(LOC)((expand_location (LOC)).column) +#define EXPANSION_POINT_LOCATION_FILE(LOC) \ + ((expand_location_to_expansion_point (LOC)).file) +#define EXPANSION_POINT_LOCAT
Re: [PATCH 05/11] Make expand_location resolve to locus in main source file
> On 04/10/2012 03:49 PM, Dodji Seketeli wrote: >> 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. > > Can you elaborate on these failures? For debug info I would think > that the spelling location would be useful information, though I > suppose without any way to "unwind" to the expansion context it could > be a bit confusing. What is the problem for the preprocessor? For the preprocessor, consider a short excerpt of the the test gcc/testsuite/gcc.dg/cpp/avoidpaste1.c: #define f(x) x #define g #define tricky 1.0e ## -1 :: :g: :f(): :f(^): tricky As the comment in the text says, it should preprocess as: :: : : : : :^: 1.0e- 1 but it actually preprocess as: # 1 "/home/dodji/devel/git/gcc/tmp/gcc/testsuite/gcc.dg/cpp/avoidpaste1.c" # 1 "" # 1 "/home/dodji/devel/git/gcc/tmp/gcc/testsuite/gcc.dg/cpp/avoidpaste1.c" # 25 "/home/dodji/devel/git/gcc/tmp/gcc/testsuite/gcc.dg/cpp/avoidpaste1.c" :: : : : : :^: # 14 "/home/dodji/devel/git/gcc/tmp/gcc/testsuite/gcc.dg/cpp/avoidpaste1.c" 1.0e- 1 Note how the "1.0e- 1" is not on the same line as its preceding ":: : : : : :^:". The problem is that the pre-processor code indirectly uses expand_location in many spots. For that kind of existing code that is not related to diagnostics, I am really not confident in changing the underlying implicit assumption of the that function which is, to return the expansion point location. I don't even know before hand where all these spots are in the code base, so auditing it is hard for me. I forgot to say that are also other weird failure like: FAIL: g++.dg/cdce3.C -std=gnu++98 scan-tree-dump cdce "cdce3.C:92: note: function call is shrink-wrapped into error conditions." due to that change. That's why I prefer erring on the safe side by not changing the assumption of existing code, and provide a way to expand locations to their spelling point, just for diagnostics. I think this is already an improvement over what we previously had. And for the debug info cases, I'd propose that if we find specific examples where the unwinding to the spelling locations is better, then we'll consider using it at that moment. -- Dodji
Re: [PATCH 05/11] Make expand_location resolve to locus in main source file
On 04/10/2012 03:49 PM, Dodji Seketeli wrote: 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. Can you elaborate on these failures? For debug info I would think that the spelling location would be useful information, though I suppose without any way to "unwind" to the expansion context it could be a bit confusing. What is the problem for the preprocessor? Jason
[PATCH 05/11] 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. --- gcc/diagnostic.c |2 +- gcc/input.c | 39 ++- gcc/input.h |9 + 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index a68d6ce..337328c 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -183,7 +183,7 @@ diagnostic_build_prefix (diagnostic_context *context, "must-not-happen" }; 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) s.column = diagnostic->override_column; gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); diff --git a/gcc/input.c b/gcc/input.c index 4077f9e..dcd348b 100644 --- a/gcc/input.c +++ b/gcc/input.c @@ -32,16 +32,22 @@ struct line_maps *line_table; /* Expand the source location LOC into a human readable location. If LOC resolves to a builtin location, the file name of the readable - location is set to the string "". */ - -expanded_location -expand_location (source_location loc) + location is set to the string "". If EXPANSION_POINT_P is + TRUE and LOC is virtual, then it is resolved to the expansion + point of the involved macro. Otherwise, it is resolved to the + spelling location of the token. */ + +static expanded_location +expand_location_1 (source_location loc, + bool expansion_point_p) { expanded_location xloc; const struct line_map *map; 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); if (loc <= BUILTINS_LOCATION) @@ -50,6 +56,29 @@ expand_location (source_location loc) return xloc; } +/* 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 "". */ + +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 + "". */ + +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_M (ONE_K * ONE_K) diff --git a/gcc/input.h b/gcc/input.h index f2f3513..5838153 100644 --- a/gcc/input.h +++ b/gcc/input.h @@ -38,6 +38,7 @@ extern char builtins_location_check[(BUILTINS_LOCATION < RESERVED_LOCATION_COUNT) ? 1 : -1]; extern expanded_location expand_location (source_location