From c46b574d5b21fb2728c76c5cab1c46890c0fb1cd Mon Sep 17 00:00:00 2001 From: xleroy Date: Thu, 21 Aug 2014 13:23:30 +0000 Subject: Support C99 compound literals (by expansion in Unblock pass). git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@2615 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e --- test/regression/Makefile | 2 +- test/regression/Results/compound | 25 +++++++ test/regression/compound.c | 146 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 test/regression/Results/compound create mode 100644 test/regression/compound.c (limited to 'test/regression') diff --git a/test/regression/Makefile b/test/regression/Makefile index bd99675..f4f9623 100644 --- a/test/regression/Makefile +++ b/test/regression/Makefile @@ -16,7 +16,7 @@ TESTS=int32 int64 floats floats-basics \ expr1 expr6 funptr2 initializers initializers2 initializers3 \ volatile1 volatile2 volatile3 \ funct3 expr5 struct7 struct8 struct11 casts1 casts2 char1 \ - sizeof1 sizeof2 binops bool for1 switch switch2 + sizeof1 sizeof2 binops bool for1 switch switch2 compound # Can run, but only in compiled mode, and have reference output in Results diff --git a/test/regression/Results/compound b/test/regression/Results/compound new file mode 100644 index 0000000..b7d007b --- /dev/null +++ b/test/regression/Results/compound @@ -0,0 +1,25 @@ +temp1 = "/temp/XXXXXX" +temp2 = "/temp/XXXXXX" +mutated temp1 = "/temp/!XXXXX" +{x = 12, y = 45} +{x = 42, y = -42} +{from = {x = 42, y = 43}, to = {x = 44, y = 45} +{from = {x = 39, y = 40}, to = {x = 41, y = 42} +{x = -41, y = -41} +ptrs contains 4 4 4 4 4 +ptrs contains 0 1 2 3 4 +structure = {a = 12, b = "a"} +foo = { "x", "y", "z" } +x = {a = 1, b[0] = 'a', b[1] = 'b'} +{f = 0.250000} +{i = 11} +1 + 3 = 4 +{x = 0, y = 0} +{x = 1, y = 1} +{x = 2, y = 2} +{x = 1, y = 0} +{x = 0, y = 2} +"first", "second", NULL +77 +{ n = 3, p -> {0,1,2,0} } +{ n = 4, p -> {0,1,2,3} } diff --git a/test/regression/compound.c b/test/regression/compound.c new file mode 100644 index 0000000..7c6bd2a --- /dev/null +++ b/test/regression/compound.c @@ -0,0 +1,146 @@ +/* Testing compound literals */ + +#include + +struct point { int x, y; }; +struct line { struct point from, to; }; + +void printpoint (struct point p) +{ + printf("{x = %d, y = %d}\n", p.x, p.y); +} + +void printpointref (struct point * p) +{ + printf("{x = %d, y = %d}\n", p->x, p->y); +} + +void printline (struct line l) +{ + printf("{from = {x = %d, y = %d}, to = {x = %d, y = %d}\n", + l.from.x, l.from.y, l.to.x, l.to.y); +} + +static inline struct point diff(struct point a, struct point b) +{ + return (struct point){ b.x - a.x, b.y - a.y }; +} + +/* H&S section 7.4.5 */ + +char * temp1 = (char []) {"/temp/XXXXXX"}; +char * temp2 = "/temp/XXXXXX"; + +int pow2(int n) +{ + if (n >= 0 && n <= 7) + return (const int []) {1,2,4,8,16,32,64,128} [n]; + else + return -1; +} + +void test1(int n) +{ + printf("temp1 = \"%s\"\n", temp1); + printf("temp2 = \"%s\"\n", temp2); + temp1[6] = '!'; + printf("mutated temp1 = \"%s\"\n", temp1); + + printpoint((struct point){.x=12, .y=n+3}); + printpointref(&(struct point){n,-n}); + printline((struct line){n,n+1,n+2,n+3}); + printline((struct line){.from = (struct point){n-3,n-2}, + .to = (struct point){n-1,n}}); + printpoint(diff((struct point){n,n}, (struct point){1,1})); + int * ptrs[5]; + int i = 0; + again: + ptrs[i] = (int [1]){i}; + if (++i < 5) goto again; + printf("ptrs contains %d %d %d %d %d\n", + *(ptrs[0]), *(ptrs[1]), *(ptrs[2]), *(ptrs[3]),*(ptrs[4])); + i = 0; + ptrs[0] = (int [1]){i++}; + ptrs[1] = (int [1]){i++}; + ptrs[2] = (int [1]){i++}; + ptrs[3] = (int [1]){i++}; + ptrs[4] = (int [1]){i++}; + printf("ptrs contains %d %d %d %d %d\n", + *(ptrs[0]), *(ptrs[1]), *(ptrs[2]), *(ptrs[3]),*(ptrs[4])); +} + +/* Examples from GCC's manual */ + +struct foo { int a; char b[2]; } structure; + +char **foo = (char *[]) { "x", "y", "z" }; + +static struct foo x = (struct foo) {1, 'a', 'b'}; +// Dubious examples: GCC refuses them, Clang warns. +// static int y[] = (int []) {1, 2, 3}; +// static int z[] = (int [3]) {1}; + +void test2(int n) +{ + structure = (struct foo) {n, 'a', 0}; + printf("structure = {a = %d, b = \"%s\"}\n", structure.a, structure.b); + printf("foo = { \"%s\", \"%s\", \"%s\" }\n", foo[0], foo[1], foo[2]); + printf("x = {a = %d, b[0] = '%c', b[1] = '%c'}\n", x.a, x.b[0], x.b[1]); +} + +/* Example gathered from various places */ + +union U { float f; int i; }; + +void printU(int kind, const union U u) +{ + switch (kind) { + case 0: printf("{f = %f}\n", u.f); break; + case 1: printf("{i = %d}\n", u.i); break; + } +} + +struct list { char * value; struct list * next; }; + +void printlist(struct list * l) +{ + for (; l != NULL; l = l->next) printf("\"%s\", ", l->value); + printf("NULL\n"); +} + +void printintref(int * p) +{ + printf("%d\n", *p); +} + +struct S { int n; int *p; }; + +void printS(struct S s) +{ + printf("{ n = %d, p -> {%d,%d,%d,%d} }\n", + s.n, s.p[0], s.p[1], s.p[2], s.p[3]); +} + +void test3(void) +{ + printU(0, (const union U){0.25}); + printU(1, (const union U){.i = 11}); + printf("1 + 3 = %d\n", (int){1} + (int){3}); + for (int i = 0; i < 3; i++) printpoint((struct point){i,i}); + printpoint((struct point){1}); + printpoint((struct point){.y=2}); + printlist(&((struct list){"first", &((struct list){"second", NULL})})); + printintref(&((int){77})); + struct S s = (struct S) {3, (int[4]){0,1,2}}; + printS(s); + printS((struct S) {4, (int[]){0,1,2,3}}); +} + +int main(void) +{ + test1(42); + test2(12); + test3(); + return 0; +} + -- cgit v1.2.3