]> git.ipfire.org Git - thirdparty/gcc.git/commit
analyzer: use value_range and range_op in eval_condition [PR98447]
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 6 Feb 2026 15:54:52 +0000 (10:54 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 6 Feb 2026 15:54:52 +0000 (10:54 -0500)
commit498fb8a24516b10897dfbe08bb8f7574b2d28839
tree64cced41f2b14acb983a896668b40bf41c857adc
parent87d9a9fd394f3cab1ab4dd62fa30bab806b03ffc
analyzer: use value_range and range_op in eval_condition [PR98447]

PR analyzer/98447 covers a false positive from -fanalyzer.
The root cause is that the analyzer's constraint-handling code
doesn't "know" that if X := Y % 64, then X < 64.

This patch adds some minimal usage of value_range and range_op to
-fanalyzer.  It adds a new svalue::maybe_get_value_range vfunc, which
attempts to get a value_range for an svalue (but bails out for awkward
cases), and if value_ranges are available, uses them in eval_condition
to determine if the result is known based on range_op.

Doing so fixes the above false positive, improves a couple of other
tests in the DejaGnu testsuite, and eliminates 31 false +ves in the
analyzer integration testsuite (out of ~2200), along with a slight
speedup to the latter (albeit testing with a debug build of gcc).

A deeper integration with ranger could probably be made, but that is
clearly stage 1 material.

gcc/analyzer/ChangeLog:
PR analyzer/98447
* common.h: Include "value-range.h".
* region-model.cc: Include "value-relation.h" and "range-op.h".
(region_model::eval_condition): Try using range_op to see if we
can get a known boolean from the value_ranges of the operands.
* svalue.cc: Include "value-relation.h" and "range-op.h".
(type_can_have_value_range_p): New.
(svalue::maybe_get_value_range): New.
(constant_svalue::maybe_get_value_range): New.
(unknown_svalue::maybe_get_value_range): New.
(unaryop_svalue::maybe_get_value_range): New.
(binop_svalue::maybe_get_value_range): New.
* svalue.h (svalue::maybe_get_value_range): New vfunc decl.
(constant_svalue::maybe_get_value_range): New decl.
(unknown_svalue::maybe_get_value_range): New decl.
(unaryop_svalue::maybe_get_value_range): New decl.
(binop_svalue::maybe_get_value_range): New decl.

gcc/testsuite/ChangeLog:
PR analyzer/98447
* c-c++-common/analyzer/conditionals-pr98447-1.c: New test.
* c-c++-common/analyzer/conditionals-pr98447-2.c: New test.
* c-c++-common/analyzer/null-deref-pr108400-SoftEtherVPN-WebUi.c:
Updated for false positive being fixed for C.
* gcc.dg/analyzer/data-model-1.c: Update expected output to
reflect improved output.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/analyzer/common.h
gcc/analyzer/region-model.cc
gcc/analyzer/svalue.cc
gcc/analyzer/svalue.h
gcc/testsuite/c-c++-common/analyzer/conditionals-pr98447-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/analyzer/conditionals-pr98447-2.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/analyzer/null-deref-pr108400-SoftEtherVPN-WebUi.c
gcc/testsuite/gcc.dg/analyzer/data-model-1.c