PR modula2/127991: Fix tree-dump crash on anonymous constants
Fixes a crash in the tree-dumper when encountering anonymous constants in Modula-2.
This commit resolves a crash in GCC’s tree-dumper that occurred when processing anonymous constants in Modula-2 code. The m2decl_DeclareKnownConstant function was creating an IDENTIFIER_NODE without setting its string, leading the dumper to dereference a null string when it attempted to print the anonymous constant’s name. The fix ensures that CONST_DECL is built with a NULL_TREE name, allowing the dumper to use the declaration’s generated name instead and preventing the crash. A regression test has been added.
In Details
In Modula-2, anonymous constants declared via m2decl_DeclareKnownConstant result in a CONST_DECL where DECL_NAME is an IDENTIFIER_NODE with a null string. The tree dumper, specifically within the pim (Pass Information Manager) pass, attempts to print DECL_NAME which leads to a null pointer dereference. This commit modifies m2decl_DeclareKnownConstant to construct the CONST_DECL with NULL_TREE as the name, which triggers the dumper to fall back to using the symbol's generated name, thus avoiding the crash.
- tree-dump
- A GCC internal tool that visualizes the compiler's internal representation (the 'tree') at various stages of compilation. It's useful for debugging and understanding compiler transformations.
- anonymous constant
- A constant value that is not given a specific identifier or name within the program's scope.
- IDENTIFIER_NODE
- A GCC internal representation node for an identifier (a name used in the source code).
- CONST_DECL
- A GCC internal representation node for a constant declaration.
- NULL_TREE
- A special GCC internal tree node representing a null or undefined value.
- Modula-2
- A procedural programming language that supports structured programming and modular programming.