http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50889
Bug #: 50889 Summary: PLUGIN_FINISH_UNIT arrives a bit to late for really high-level (AST-based) in-memory code transformation/instrumentation Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: plugins AssignedTo: unassig...@gcc.gnu.org ReportedBy: krik_evryw...@hotmail.com The PLUGIN_FINISH_UNIT callback is called in toplev.c at line 668 (in version 4.6.2 of gcc), at the very end of the compile_file () function. Unfortunately, it seems to mean the heavy work of lowering the AST is already done by the time it reaches this callback, and to the point to having almost finished emitting the assembly code, as suggested by the comment and statement just after the callback call: /* This must be at the end. Some target ports emit end of file directives into the assembly file here, and hence we can not output anything to the assembly file after this point. */ targetm.asm_out.file_end (); At least that's the way I understand it, I'm very new to gcc. That's way too late to do some high-level transformations on the AST before it gets lowered and, thus, the "new" (freshly in-memory modified) code gets included into the lower phases of the process. I've come to this basic conclusion after having checked the work the interesting functions called by the parse_file () hook do, and having checked against the way GCC-XML has modified gcc 4.2.1 to call its own hook to browse through the high-level AST generated when gcc has finished parsing the source file. I would like to request adding a simple additional callback type, something like "PLUGIN_PREFINISH_UNIT", "PLUGIN_PRE_FINISH_UNIT", "PLUGIN_FINISH_PARSE_UNIT" or whatever name suits, which would be called at line 585 (still talking against version 4.6.2), just before the "if (flag_syntax_only || flag_wpa)" line, that's the best place I can spot (and the one looking the more logical to me). It's just a matter of adding a few lines here and there (code and docs). I could appropriately alter a checked-out revision of version 4.7.0, or whatever is called the "trunk" for now, if it's ok and it can help this gets implemented. As to the reason why, for example, I would like to automatically add a companion variable to some variables based on their type, through a plugin. Which is not safe to do with some naive look-up, and far from trivial to do otherwise because of redoing what gcc already does very much that I could ever do, that's parsing the source code. Doing this with a plugin would heavily simplify and help the analysis, and thus the insertion at proper place in the AST. For completeness, I should mention it seems to me the bug 44968 ("structs saved (in a vector) during PLUGIN_FINISH_TYPE are mangled by the time of PLUGIN_FINISH_UNIT") is a bit related to this request, as the author seems to have expected the AST would be the raw one, not an already lowered one.