Analyzer: Introduce `-Wanalyzer-div-by-zero` warning
The GCC analyzer now issues a `-Wanalyzer-div-by-zero` warning for potential division-by-zero errors.
The GCC static analyzer now includes a -Wanalyzer-div-by-zero warning, which detects potential division-by-zero errors during compilation. The analyzer identifies division operations where the divisor could be zero, issuing a warning to alert developers. This new diagnostic capability helps catch potential runtime errors early, improving code reliability and preventing unexpected program termination. The implementation includes modifications to the analyzer’s region model and adds new test cases to verify the warning’s effectiveness.
In Details
The GCC static analyzer performs interprocedural analysis to detect potential errors at compile time. This commit introduces a new warning, -Wanalyzer-div-by-zero, which adds a div_by_zero_diagnostic to the analysis. The analyzer now checks division operations (including modulus) within region_model::get_gassign_result and emits a warning if the divisor could be zero. The changes affect analyzer.opt, region-model.cc, and svalue.cc. The analyzer infra isn't commonly touched outside the analyzer team.
For Context
Static analysis is a technique used to detect potential errors in code at compile time, before the program is run. The GCC static analyzer examines the code for various issues, such as memory leaks, use of uninitialized variables, and division by zero. This commit introduces a new warning, -Wanalyzer-div-by-zero, that specifically identifies potential division-by-zero errors. Division by zero is a common programming error that causes a program to crash. By detecting these errors early, developers can fix them before the program is deployed, improving its reliability.