public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/arsenic/heads/analyzer_extension)] analyzer: detect and analyze virtul function calls
@ 2021-08-15 14:02 Ankur saini
  0 siblings, 0 replies; 2+ messages in thread
From: Ankur saini @ 2021-08-15 14:02 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:173c36d4576ba548be58c07b0182c87434bd6854

commit 173c36d4576ba548be58c07b0182c87434bd6854
Author: Ankur Saini <arsenic@sourceware.org>
Date:   Sun Aug 15 19:19:07 2021 +0530

    analyzer: detect and analyze virtul function calls
    
    2021-08-15  Ankur Saini  <arsenic@sourceware.org>
    
    gcc/analyzer/ChangeLog:
            * analyzer/region-model.cc (region_model::get_rvalue_1): Add case for
            OBJ_TYPE_REF.
    
    gcc/testsuite/ChangeLog:
            *g++.dg/analyzer/vfunc-2.C: New test.
            *g++.dg/analyzer/vfunc-3.C: New test.

Diff:
---
 gcc/analyzer/region-model.cc            |  5 ++++
 gcc/testsuite/g++.dg/analyzer/vfunc-2.C | 44 +++++++++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/analyzer/vfunc-3.C | 36 +++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)

diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc
index 49fe68e10a9..4d4ce6cb715 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -1812,6 +1812,11 @@ region_model::get_rvalue_1 (path_var pv, region_model_context *ctxt) const
 	const region *ref_reg = get_lvalue (pv, ctxt);
 	return get_store_value (ref_reg, ctxt);
       }
+    case OBJ_TYPE_REF:
+      {
+        tree expr = OBJ_TYPE_REF_EXPR (pv.m_tree);
+        return get_rvalue (expr, ctxt);
+      }
     }
 }
 
diff --git a/gcc/testsuite/g++.dg/analyzer/vfunc-2.C b/gcc/testsuite/g++.dg/analyzer/vfunc-2.C
new file mode 100644
index 00000000000..46b68e529e6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/analyzer/vfunc-2.C
@@ -0,0 +1,44 @@
+#include <cstdio>
+#include <cstdlib>
+
+struct A
+{
+    int m_data;
+    A() {m_data = 0;}
+    virtual int deallocate (void) 
+    {
+        return 42;
+    }
+};
+
+struct B: public A
+{
+    int *ptr;
+    int m_data_b;
+    B() {m_data_b = 0;}
+    void allocate ()
+    {
+        ptr = (int*)malloc(sizeof(int));
+    }
+    int deallocate (void) 
+    { 
+        free(ptr);
+        return 0;
+    }
+};
+
+void foo(A *a_ptr)
+{
+    printf("%d\n",a_ptr->deallocate());
+}
+
+void test()
+{
+    B b;
+    A a, *aptr;
+    aptr = &b;
+    b.allocate();
+    foo(aptr);
+    aptr = &a;
+    foo(aptr);
+}
diff --git a/gcc/testsuite/g++.dg/analyzer/vfunc-3.C b/gcc/testsuite/g++.dg/analyzer/vfunc-3.C
new file mode 100644
index 00000000000..bd00bb436c8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/analyzer/vfunc-3.C
@@ -0,0 +1,36 @@
+#include <cstdlib>
+
+struct A
+{
+    virtual int foo (void) 
+    {
+        return 42;
+    }
+};
+
+struct B: public A
+{
+    int *ptr;
+    void alloc ()
+    {
+        ptr = (int*)malloc(sizeof(int));
+    }
+    int foo (void) 
+    { 
+        free(ptr); /* { dg-warning "double-'free' of 'b.B::ptr'" } */
+        return 0;
+    }
+};
+
+int test()
+{
+    struct B b, *bptr=&b;
+    b.alloc();
+    bptr->foo(); /* { dg-message "calling 'B::foo' from 'test'" } */
+    return bptr->foo();
+}
+
+int main()
+{
+    test();
+}


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gcc(refs/users/arsenic/heads/analyzer_extension)] analyzer: detect and analyze virtul function calls
@ 2021-08-15 13:54 Ankur saini
  0 siblings, 0 replies; 2+ messages in thread
From: Ankur saini @ 2021-08-15 13:54 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:6d9a32f11efb06be10682a8e48ad5d02e47cf2f0

commit 6d9a32f11efb06be10682a8e48ad5d02e47cf2f0
Author: Ankur Saini <arsenic@sourceware.org>
Date:   Sun Aug 15 19:19:07 2021 +0530

    analyzer: detect and analyze virtul function calls
    
    2021-07-29  Ankur Saini  <arsenic@sourceware.org>
    
    gcc/analyzer/ChangeLog:
            * analyzer/region-model.cc (region_model::get_rvalue_1): Add case for
            OBJ_TYPE_REF.
    
    gcc/testsuite/ChangeLog:
            *g++.dg/analyzer/vfunc-2.C: New test.
            *g++.dg/analyzer/vfunc-3.C: New test.

Diff:
---
 gcc/analyzer/region-model.cc            |  5 ++++
 gcc/testsuite/g++.dg/analyzer/vfunc-2.C | 44 +++++++++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/analyzer/vfunc-3.C | 36 +++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)

diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc
index 1e9654b6743..459dd5ee0fa 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -1812,6 +1812,11 @@ region_model::get_rvalue_1 (path_var pv, region_model_context *ctxt) const
 	const region *ref_reg = get_lvalue (pv, ctxt);
 	return get_store_value (ref_reg, ctxt);
       }
+    case OBJ_TYPE_REF:
+      {
+        tree expr = OBJ_TYPE_REF_EXPR (pv.m_tree);
+        return get_rvalue (expr, ctxt);
+      }
     }
 }
 
diff --git a/gcc/testsuite/g++.dg/analyzer/vfunc-2.C b/gcc/testsuite/g++.dg/analyzer/vfunc-2.C
new file mode 100644
index 00000000000..46b68e529e6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/analyzer/vfunc-2.C
@@ -0,0 +1,44 @@
+#include <cstdio>
+#include <cstdlib>
+
+struct A
+{
+    int m_data;
+    A() {m_data = 0;}
+    virtual int deallocate (void) 
+    {
+        return 42;
+    }
+};
+
+struct B: public A
+{
+    int *ptr;
+    int m_data_b;
+    B() {m_data_b = 0;}
+    void allocate ()
+    {
+        ptr = (int*)malloc(sizeof(int));
+    }
+    int deallocate (void) 
+    { 
+        free(ptr);
+        return 0;
+    }
+};
+
+void foo(A *a_ptr)
+{
+    printf("%d\n",a_ptr->deallocate());
+}
+
+void test()
+{
+    B b;
+    A a, *aptr;
+    aptr = &b;
+    b.allocate();
+    foo(aptr);
+    aptr = &a;
+    foo(aptr);
+}
diff --git a/gcc/testsuite/g++.dg/analyzer/vfunc-3.C b/gcc/testsuite/g++.dg/analyzer/vfunc-3.C
new file mode 100644
index 00000000000..bd00bb436c8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/analyzer/vfunc-3.C
@@ -0,0 +1,36 @@
+#include <cstdlib>
+
+struct A
+{
+    virtual int foo (void) 
+    {
+        return 42;
+    }
+};
+
+struct B: public A
+{
+    int *ptr;
+    void alloc ()
+    {
+        ptr = (int*)malloc(sizeof(int));
+    }
+    int foo (void) 
+    { 
+        free(ptr); /* { dg-warning "double-'free' of 'b.B::ptr'" } */
+        return 0;
+    }
+};
+
+int test()
+{
+    struct B b, *bptr=&b;
+    b.alloc();
+    bptr->foo(); /* { dg-message "calling 'B::foo' from 'test'" } */
+    return bptr->foo();
+}
+
+int main()
+{
+    test();
+}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-15 14:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-15 14:02 [gcc(refs/users/arsenic/heads/analyzer_extension)] analyzer: detect and analyze virtul function calls Ankur saini
  -- strict thread matches above, loose matches on Subject: below --
2021-08-15 13:54 Ankur saini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).