analyzer: consider initializers for globals [PR96651]
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 17 Aug 2020 20:35:10 +0000 (16:35 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Wed, 19 Aug 2020 01:21:35 +0000 (21:21 -0400)
commit623bc0276849d48ada5a7a2e3e94bd79de42c3db
tree9443ad633d75cbd19c7b95565d3b2a26f3419a4e
parent400abebf48a90d0797718ab7c3864de331e85b70
analyzer: consider initializers for globals [PR96651]

PR analyzer/96651 reports a false positive in which a global
that can't have been touched yet is checked in "main".  The analyzer
fails to reject code paths in which the initial value of the global
makes the path condition impossible.

This patch detects cases where the code path begins at the entrypoint
of "main", and extracts values from initializers for globals that
can't have been touched yet, rather than using a symbolic
"INIT_VAL(REG)", fixing the false positive.

gcc/analyzer/ChangeLog:
PR analyzer/96651
* region-model.cc (region_model::called_from_main_p): New.
(region_model::get_store_value): Move handling for globals into...
(region_model::get_initial_value_for_global): ...this new
function, and add logic for extracting values from decl
initializers.
* region-model.h (decl_region::get_svalue_for_constructor): New
decl.
(decl_region::get_svalue_for_initializer): New decl.
(region_model::called_from_main_p): New decl.
(region_model::get_initial_value_for_global): New.
* region.cc (decl_region::maybe_get_constant_value): Move logic
for getting an svalue from a CONSTRUCTOR node to...
(decl_region::get_svalue_for_constructor): ...this new function.
(decl_region::get_svalue_for_initializer): New.
* store.cc (get_svalue_for_ctor_val): Rewrite in terms of
region_model::get_rvalue.
* store.h (binding_cluster::get_map): New accessor.

gcc/testsuite/ChangeLog:
PR analyzer/96651
* gcc.dg/analyzer/pr96651-1.c: New test.
* gcc.dg/analyzer/pr96651-2.c: New test.
gcc/analyzer/region-model.cc
gcc/analyzer/region-model.h
gcc/analyzer/region.cc
gcc/analyzer/store.cc
gcc/analyzer/store.h
gcc/testsuite/gcc.dg/analyzer/pr96651-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/pr96651-2.c [new file with mode: 0644]