From ff38d90d5d35f50e2ad44936be4e218086090000 Mon Sep 17 00:00:00 2001
From: matthias <uso.cosmo.ray@gmail.com>
Date: Wed, 2 Jan 2019 14:12:25 +0100
Subject: [PATCH] Add attribute cleanup test

---
 tests/tests2/101_cleanup.c      | 124 ++++++++++++++++++++++++++++++++
 tests/tests2/101_cleanup.expect |  20 ++++++
 2 files changed, 144 insertions(+)
 create mode 100644 tests/tests2/101_cleanup.c
 create mode 100644 tests/tests2/101_cleanup.expect

diff --git a/tests/tests2/101_cleanup.c b/tests/tests2/101_cleanup.c
new file mode 100644
index 00000000..7c3b0ca4
--- /dev/null
+++ b/tests/tests2/101_cleanup.c
@@ -0,0 +1,124 @@
+#include <tcclib.h>
+
+void check2(char **hum);
+
+void check(int *j)
+{
+    char * __attribute__ ((cleanup(check2))) stop_that = "wololo";
+    int chk = 0;
+
+    {
+	char * __attribute__ ((cleanup(check2))) stop_that = "plop";
+
+	{
+	  non_plopage:
+	    printf("---- %d\n", chk);
+	}
+	if (!chk) {
+	    chk = 1;
+	    goto non_plopage;
+	}
+    }
+
+    {
+	char * __attribute__ ((cleanup(check2))) stop_that = "tata !";
+
+	goto out;
+	stop_that = "titi";
+    }
+  again:
+    chk = 2;
+    {
+	char * __attribute__ ((cleanup(check2))) cascade1 = "1";
+	{
+	    char * __attribute__ ((cleanup(check2))) cascade2 = "2";
+	    {
+		char * __attribute__ ((cleanup(check2))) cascade3 = "3";
+
+		goto out;
+		cascade3 = "nope";
+	    }
+	}
+    }
+  out:
+    if (chk != 2)
+	goto again;
+    {
+	{
+	    char * __attribute__ ((cleanup(check2))) out = "last goto out";
+	    ++chk;
+	    if (chk != 3)
+		goto out;
+	}
+    }
+    return;
+}
+
+void check_oh_i(char *oh_i)
+{
+    printf("c: %c\n", *oh_i);
+}
+
+void goto_hell(double *f)
+{
+    printf("oo: %f\n", *f);
+}
+
+char *test()
+{
+    char *__attribute__ ((cleanup(check2))) str = "I don't think this should be print(but gcc got it wrong too)";
+
+    return str;
+}
+
+void test_ret_subcall(char *that)
+{
+    printf("should be print before\n");
+}
+
+void test_ret()
+{
+    char *__attribute__ ((cleanup(check2))) that = "that";
+    return test_ret_subcall(that);
+}
+
+void test_ret2()
+{
+  char *__attribute__ ((cleanup(check2))) that = "-that";
+  {
+    char *__attribute__ ((cleanup(check2))) that = "this should appear only once";
+  }
+  {
+    char *__attribute__ ((cleanup(check2))) that = "-that2";
+    return;
+  }
+}
+
+int main()
+{
+    int i __attribute__ ((__cleanup__(check))) = 0, not_i;
+    int chk = 0;
+
+    {
+	__attribute__ ((__cleanup__(check_oh_i))) char oh_i = 'o', o = 'a';
+    }
+
+  naaaaaaaa:
+    if (!chk) {
+	__attribute__ ((__cleanup__(check_oh_i))) char oh_i = 'f';
+	double __attribute__ ((__cleanup__(goto_hell))) f = 2.6;
+
+	chk = 1;
+	goto naaaaaaaa;
+    }
+    i = 105;
+    printf("because what if free was call inside cleanup function\n", test());
+    test_ret();
+    test_ret2();
+    return i;
+}
+
+void check2(char **hum)
+{
+    printf("str: %s\n", *hum);
+}
diff --git a/tests/tests2/101_cleanup.expect b/tests/tests2/101_cleanup.expect
new file mode 100644
index 00000000..24e6ba3a
--- /dev/null
+++ b/tests/tests2/101_cleanup.expect
@@ -0,0 +1,20 @@
+c: a
+c: o
+oo: 2.600000
+c: f
+str: I don't think this should be print(but gcc got it wrong too)
+because what if free was call inside cleanup function
+should be print before
+str: that
+str: this should appear only once
+str: -that2
+str: -that
+---- 0
+---- 1
+str: plop
+str: tata !
+str: 3
+str: 2
+str: 1
+str: last goto out
+str: wololo