I'm working on a whole-program static analyzer that runs from a gcc plugin during lto1 when invoked via -flto -flto-partition=none as an IPA_PASS registered before "whole-program". I'm currently emitting errors using error_at() with inform() to give extra information on the (possibly interprocedural) execution path taken to reach the error. This works, but leads to unusual-looking output. For example, with: $ gcc -flto -flto-partition=none [...various plugin args...] input-f.c input-g.c input-h.c my code detects a double-free involving an execution path between input-h.c and input-g.c and calls error_at("double-free of r") on a location_t from input-h.c and gets this strange output on stderr: In file included from test.h:1:0, from /usr/include/stdlib.h:489, from input-f.c:14, from :3: input-h.c:13:9: error: double-free of r it then calls inform("q passed to free()") on a location_t from input-g.c (to show the location of the 1st free call) and gets this on stderr: In file included from test.h:1:0, from /usr/include/stdlib.h:489, from input-f.c:14, from :3: input-g.c:11:7: note: q passed to free() In both cases, the "In file included from " hierarchies look bogus: the files are being directly compiled, not included. Debugging, it comes from gcc/diagnostic.c:diagnostic_report_current_module() where I see this code: 275 if (map && diagnostic_last_module_changed (context, map)) 276 { 277 diagnostic_set_last_module (context, map); 278 if (! MAIN_FILE_P (map)) 279 { 280 map = INCLUDED_FROM (line_table, map); 281 if (context->show_column) 282 pp_verbatim (context->printer, 283 "In file included from %s:%d:%d", 284 LINEMAP_FILE (map), 285 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map)); 286 else 287 pp_verbatim (context->printer, 288 "In file included from %s:%d", 289 LINEMAP_FILE (map), LAST_SOURCE_LINE (map)); 290 while (! MAIN_FILE_P (map)) 291 { 292 map = INCLUDED_FROM (line_table, map); This code appears to assume (via MAIN_FILE_P) that there is a concept of a main file, and that everything else is included from it. However in the context of diagnostics emitted during LTO it's not clear that this concept makes sense (or maybe I'm missing something? I also wonder if by attempting to issue diagnostics from within lto1 if I'm in unexplored territory here?) This is called by default_tree_diagnostic_starter FWIW; perhaps lto1 needs its own implementation of this?
Where doe the line_maps that LTO use come from? Perhaps they are not saved/restored correctly?
(In reply to comment #0) > 290 while (! MAIN_FILE_P (map)) > 291 { > 292 map = INCLUDED_FROM (line_table, map); > > This code appears to assume (via MAIN_FILE_P) that there is a concept of a main > file, and that everything else is included from it. However in the context of > diagnostics emitted during LTO it's not clear that this concept makes sense (or > maybe I'm missing something? I also wonder if by attempting to issue > diagnostics from within lto1 if I'm in unexplored territory here?) You definitely are, if you are expecting everything to work just fine ;) > This is called by default_tree_diagnostic_starter FWIW; perhaps lto1 needs its > own implementation of this? Maybe yes. LTO should print the "main" file as well (so it doesn't have a MAIN_FILE_P), but not as included-from but in some other way. Patches welcome ;)
(In reply to comment #2) > > This is called by default_tree_diagnostic_starter FWIW; perhaps lto1 needs its > > own implementation of this? > > Maybe yes. LTO should print the "main" file as well (so it doesn't have > a MAIN_FILE_P), but not as included-from but in some other way. > How does the line_map works in LTO? Is it saved and restored or re-build from scratch?
(In reply to comment #3) > (In reply to comment #2) > > > This is called by default_tree_diagnostic_starter FWIW; perhaps lto1 needs its > > > own implementation of this? > > > > Maybe yes. LTO should print the "main" file as well (so it doesn't have > > a MAIN_FILE_P), but not as included-from but in some other way. > > > > How does the line_map works in LTO? Is it saved and restored or re-build from > scratch? We stream the expanded location and allocate new line-map entries at LTO read time.
(In reply to comment #4) > > We stream the expanded location and allocate new line-map entries at LTO > read time. Where? I guess this precludes any knowledge of what is included from where and for sure no tracking of macro expansion, but it still doesn't explain why MAIN_FILE_P and INCLUDED_FROM do not work reliably. They should be true for every map and return NULL, respectively. That seems like a bug.
(In reply to Dave Malcolm from comment #0) > In both cases, the "In file included from " hierarchies look bogus: the > files are being directly compiled, not included. > Please try the patch in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65536#c21
(In reply to Manuel López-Ibáñez from comment #6) > (In reply to Dave Malcolm from comment #0) > > In both cases, the "In file included from " hierarchies look bogus: the > > files are being directly compiled, not included. > > > > Please try the patch in > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65536#c21 You need also the libcpp hunks from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65536#c18