[PATCH] add test for integer-const-expr-ness

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This use Martin's awesome macro to test if sparse's
notion of integer-const-expr is the same as GCC's.

It test also that the result of this macro is itself a
constant integer expression.

Awesome-macro-by: Martin Uecker <Martin.Uecker@xxxxxxxxxxxxxxxxxxxxx>
Test-originally-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 validation/integer-const-expr.c | 85 +++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
 create mode 100644 validation/integer-const-expr.c

diff --git a/validation/integer-const-expr.c b/validation/integer-const-expr.c
new file mode 100644
index 000000000..f41aa806a
--- /dev/null
+++ b/validation/integer-const-expr.c
@@ -0,0 +1,85 @@
+extern void *malloc(unsigned long);
+
+static inline __attribute__((__const__)) unsigned squarec(unsigned n)
+{
+        return n*n;
+}
+
+static inline unsigned square(unsigned n)
+{
+        return n*n;
+}
+
+static inline unsigned long long bignum(void)
+{
+        return 1000000000000ULL;
+}
+
+static inline __attribute__((__const__)) unsigned long long bignumc(void)
+{
+        return 1000000000000ULL;
+}
+
+// test if x is an integer constant expression [C99,C11 6.6p6]
+#define ICE_P(x) \
+    (__builtin_types_compatible_p(typeof(0?((void*)((long)(x)*0l)):(int*)1),int*))
+
+#define CHX_P(X)	__builtin_choose_expr(ICE_P(X), 1, 0)
+#define CST_P(X)	__builtin_constant_p(ICE_P(X))
+
+#define TEST(R, X)	_Static_assert(ICE_P(X) == R, "ICE_P(" #X ") => " #R);	\
+			_Static_assert(ICE_P(ICE_P(X)), "ICE_P2(" #X ")");	\
+			_Static_assert(CHX_P(X) == R, "CHX_P(" #X ") => " #R);	\
+			_Static_assert(CST_P(X) == 1, "CST_P(" #X ")")
+
+int main(int argc, char *argv[])
+{
+        char fla[3];
+        char vla[argc++];
+        char **p, **q;
+        int x = 5, y = 8;
+        void *v;
+
+        p = &argv[3];
+        q = &argv[6];
+
+        TEST(1, 4);
+        TEST(1, sizeof(long));
+        TEST(1, 5ull - 3u);
+        TEST(1, 3.2);
+        TEST(1, sizeof(fla));
+
+        TEST(0, square(2));
+        TEST(0, square(argc));
+        TEST(0, squarec(2));
+        TEST(0, squarec(argc));
+        TEST(0, 1+argc-argc);
+        TEST(0, 1+argc+argc+1-argc-argc);
+        TEST(0, bignum() - 1);
+        TEST(0, 0*bignum());
+        TEST(0, 0*bignumc());
+        TEST(0, sizeof(vla));
+        TEST(0, p);
+        TEST(0, p < q);
+        TEST(0, p++);
+        TEST(0, main);
+        TEST(0, malloc(8));
+        TEST(0, v = malloc(8));
+        TEST(0, v);
+        TEST(0, x++);
+        TEST(0, y++);
+        TEST(0, (3, 2, 1));
+        TEST(0, ({x++; 0; }));
+        TEST(0, ({square(y--); 0; }));
+        TEST(0, (square(x), 3));
+        TEST(0, (squarec(x), 3));
+        TEST(0, ({squarec(x); 3;}));
+        TEST(0, ({squarec(x);}));
+
+        return 0;
+}
+
+/*
+ * check-name: integer-const-expr
+ * check-command: sparse -Wno-vla $file
+ */
-- 
2.17.1

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux