[1/2] analyzer: return a concrete offset for cast_regions
Commit Message
This patch fixes a bug where maybe_fold_sub_svalue did not fold the
access of a single char from a string to a char when the offset was zero
because get_relative_concrete_offset did return false for cast_regions.
Regrtested on Linux x86_64.
2022-09-02 Tim Lange <mail@tim-lange.me>
gcc/analyzer/ChangeLog:
* region.cc (cast_region::get_relative_concrete_offset):
New overloaded method.
* region.h: Add cast_region::get_relative_concrete_offset.
gcc/testsuite/ChangeLog:
* gcc.dg/analyzer/fold-string-to-char.c: New test.
---
gcc/analyzer/region.cc | 10 ++++++++++
gcc/analyzer/region.h | 2 ++
gcc/testsuite/gcc.dg/analyzer/fold-string-to-char.c | 8 ++++++++
3 files changed, 20 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/analyzer/fold-string-to-char.c
Comments
On Fri, 2022-09-02 at 16:08 +0200, Tim Lange wrote:
> This patch fixes a bug where maybe_fold_sub_svalue did not fold the
> access of a single char from a string to a char when the offset was
> zero
> because get_relative_concrete_offset did return false for
> cast_regions.
>
> Regrtested on Linux x86_64.
Thanks; this patch is OK for trunk.
Dave
@@ -1556,6 +1556,16 @@ cast_region::dump_to_pp (pretty_printer *pp, bool simple) const
}
}
+/* Implementation of region::get_relative_concrete_offset vfunc
+ for cast_region. */
+
+bool
+cast_region::get_relative_concrete_offset (bit_offset_t *out) const
+{
+ *out = (int) 0;
+ return true;
+}
+
/* class heap_allocated_region : public region. */
/* Implementation of region::dump_to_pp vfunc for heap_allocated_region. */
@@ -1087,6 +1087,8 @@ public:
void accept (visitor *v) const final override;
void dump_to_pp (pretty_printer *pp, bool simple) const final override;
+ bool get_relative_concrete_offset (bit_offset_t *out) const final override;
+
const region *get_original_region () const { return m_original_region; }
private:
new file mode 100644
@@ -0,0 +1,8 @@
+#include "analyzer-decls.h"
+
+void test_1 (void)
+{
+ char str[] = "Hello";
+ char *ptr = str;
+ __analyzer_eval (ptr[0] == 'H'); /* { dg-warning "TRUE" } */
+}