GCC implements new Fortran warnings for unused and undefined variables.
GCC now provides new warnings for Fortran code concerning undefined variables and variables set but never used, improving code quality and catching potential e…
This commit introduces new -Wundefined-vars and -Wunused-but-set-variable warnings for Fortran, addressing issues where variables are used without prior definition or set to a value that is never subsequently read. The implementation tracks variable usage and definition within a namespace, carefully avoiding false positives by excluding certain conditions. This significantly enhances compiler diagnostics for Fortran developers, helping them identify and correct potential logical flaws and dead code, ultimately leading to more robust and maintainable programs.
In Details
This change extends Fortran's static analysis capabilities by tracking value_set, value_used, and allocated states for variables within the gfc_resolve pass. It identifies cases where a variable is referenced before being assigned a value or where a value is assigned but never read. The warnings are issued during the warn_unused_vs_set phase, called from gfc_resolve, with a focus on conservative detection to minimize false positives by checking various conditions in find_unused_vs_set. This operates at the level of individual statements within a namespace, without full control-f…
For Context
Compilers perform static analysis to detect potential problems in code without actually running it. This update enhances how GCC checks Fortran code for certain common programming mistakes. Specifically, it introduces new warnings for two scenarios: when a variable is used in your program before it has been given a value (which can lead to unpredictable behavior), and when a variable is assigned a value but that value is never used later in the program (which can indicate dead code or a logical error). These warnings help developers write cleaner, more reliable Fortran code by flagging issues that might otherwise go unnoticed.