aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
-rw-r--r--evaluate.c6
-rw-r--r--linearize.c21
-rw-r--r--validation/builtin-arith.c52
-rw-r--r--validation/function-pointer-type.c12
-rw-r--r--validation/linear/call-basic.c57
-rw-r--r--validation/linear/call-builtin.c17
-rw-r--r--validation/linear/call-casted-pointer.c31
-rw-r--r--validation/linear/call-complex-pointer.c32
-rw-r--r--validation/linear/call-direct.c17
-rw-r--r--validation/linear/call-indirect.c15
-rw-r--r--validation/linear/call-inline.c18
-rw-r--r--validation/optim/call-complex-pointer.c13
-rw-r--r--validation/sizeof-builtin.c15
-rw-r--r--validation/sizeof-function.c49
14 files changed, 339 insertions, 16 deletions
diff --git a/evaluate.c b/evaluate.c
index 85f1b21b..4be4e8e5 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1776,7 +1776,6 @@ static struct symbol *evaluate_dereference(struct expression *expr)
if (ctype->type == SYM_NODE)
ctype = ctype->ctype.base_type;
- node = alloc_symbol(expr->pos, SYM_NODE);
target = ctype->ctype.base_type;
examine_symbol_type(target);
@@ -1784,7 +1783,11 @@ static struct symbol *evaluate_dereference(struct expression *expr)
default:
expression_error(expr, "cannot dereference this type");
return NULL;
+ case SYM_FN:
+ *expr = *op;
+ return expr->ctype;
case SYM_PTR:
+ node = alloc_symbol(expr->pos, SYM_NODE);
node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
merge_type(node, ctype);
break;
@@ -1802,6 +1805,7 @@ static struct symbol *evaluate_dereference(struct expression *expr)
* When an array is dereferenced, we need to pick
* up the attributes of the original node too..
*/
+ node = alloc_symbol(expr->pos, SYM_NODE);
merge_type(node, op->ctype);
merge_type(node, ctype);
break;
diff --git a/linearize.c b/linearize.c
index eff7d95f..2e146de7 100644
--- a/linearize.c
+++ b/linearize.c
@@ -1279,15 +1279,10 @@ static pseudo_t linearize_call_expression(struct entrypoint *ep, struct expressi
}
fn = expr->fn;
-
- if (fn->ctype)
- ctype = &fn->ctype->ctype;
-
fntype = fn->ctype;
- if (fntype) {
- if (fntype->type == SYM_NODE)
- fntype = fntype->ctype.base_type;
- }
+ ctype = &fntype->ctype;
+ if (fntype->type == SYM_NODE)
+ fntype = fntype->ctype.base_type;
add_symbol(&insn->fntypes, fntype);
FOR_EACH_PTR(expr->args, arg) {
@@ -1296,13 +1291,9 @@ static pseudo_t linearize_call_expression(struct entrypoint *ep, struct expressi
add_symbol(&insn->fntypes, arg->ctype);
} END_FOR_EACH_PTR(arg);
- if (fn->type == EXPR_PREOP) {
- if (fn->unop->type == EXPR_SYMBOL) {
- struct symbol *sym = fn->unop->symbol;
- if (sym->ctype.base_type->type == SYM_FN)
- fn = fn->unop;
- }
- }
+ if (fn->type == EXPR_PREOP && fn->op == '*' && is_func_type(fn->ctype))
+ fn = fn->unop;
+
if (fn->type == EXPR_SYMBOL) {
call = symbol_pseudo(ep, fn->symbol);
} else {
diff --git a/validation/builtin-arith.c b/validation/builtin-arith.c
new file mode 100644
index 00000000..d08c93da
--- /dev/null
+++ b/validation/builtin-arith.c
@@ -0,0 +1,52 @@
+
+
+void test(void (*fun)(void));
+void test(void (*fun)(void))
+{
+ typedef typeof(__builtin_trap) t; // OK
+ void (*f)(void);
+ int i;
+
+ f = __builtin_trap;
+ f = &__builtin_trap;
+ f = *__builtin_trap; // OK for GCC
+ f = __builtin_trap + 0;
+ f = __builtin_trap + 1;
+ f = __builtin_trap - 1;
+
+ // (void) __builtin_trap;
+ f = (void*) __builtin_trap;
+ f = (unsigned long) __builtin_trap;
+
+ i = !__builtin_trap;
+ i = (__builtin_trap > fun);
+ i = (__builtin_trap == fun);
+ i = (fun < __builtin_trap);
+ i = (fun == __builtin_trap);
+
+ __builtin_trap - fun;
+ fun - __builtin_trap;
+}
+
+/*
+ * check-name: builtin arithmetic
+ * check-command: sparse -Wno-decl $file
+ * check-known-to-fail
+ *
+ * check-error-start
+builtin-arith.c:10:xx: error: ...
+builtin-arith.c:11:xx: error: ...
+builtin-arith.c:13:xx: error: arithmetics on pointers to functions
+builtin-arith.c:14:xx: error: arithmetics on pointers to functions
+builtin-arith.c:15:xx: error: arithmetics on pointers to functions
+builtin-arith.c:18:xx: error: ...
+builtin-arith.c:19:xx: error: ...
+builtin-arith.c:21:xx: error: ...
+builtin-arith.c:22:xx: error: ...
+builtin-arith.c:23:xx: error: ...
+builtin-arith.c:24:xx: error: ...
+builtin-arith.c:25:xx: error: ...
+builtin-arith.c:27:24: error: subtraction of functions? Share your drugs
+builtin-arith.c:28:13: error: subtraction of functions? Share your drugs
+ * check-error-end
+ */
diff --git a/validation/function-pointer-type.c b/validation/function-pointer-type.c
new file mode 100644
index 00000000..ebc4007b
--- /dev/null
+++ b/validation/function-pointer-type.c
@@ -0,0 +1,12 @@
+extern int fun(void);
+
+void fa(void) { int (*f)(void); f = &fun; }
+void f0(void) { int (*f)(void); f = fun; } // C99,C11 6.3.2.1p4
+void f1(void) { int (*f)(void); f = *fun; } // C99,C11 6.5.3.2p4
+void f2(void) { int (*f)(void); f = **fun; } // C99,C11 6.5.3.2p4
+void f3(void) { int (*f)(void); f = ***fun; } // C99,C11 6.5.3.2p4
+
+/*
+ * check-name: type of function pointers
+ * check-command: sparse -Wno-decl $file
+ */
diff --git a/validation/linear/call-basic.c b/validation/linear/call-basic.c
new file mode 100644
index 00000000..60517e2e
--- /dev/null
+++ b/validation/linear/call-basic.c
@@ -0,0 +1,57 @@
+extern int fun(int a);
+
+int symbol(int a)
+{
+ fun(a);
+}
+
+int pointer0(int a, int (*fun)(int))
+{
+ fun(a);
+}
+
+int pointer1(int a, int (*fun)(int))
+{
+ (*fun)(a);
+}
+
+int builtin(int a)
+{
+ __builtin_popcount(a);
+}
+
+/*
+ * check-name: basic function calls
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-start
+symbol:
+.L0:
+ <entry-point>
+ call.32 %r2 <- fun, %arg1
+ ret.32 %r2
+
+
+pointer0:
+.L2:
+ <entry-point>
+ call.32 %r5 <- %arg2, %arg1
+ ret.32 %r5
+
+
+pointer1:
+.L4:
+ <entry-point>
+ call.32 %r8 <- %arg2, %arg1
+ ret.32 %r8
+
+
+builtin:
+.L6:
+ <entry-point>
+ call.32 %r11 <- __builtin_popcount, %arg1
+ ret.32 %r11
+
+
+ * check-output-end
+ */
diff --git a/validation/linear/call-builtin.c b/validation/linear/call-builtin.c
new file mode 100644
index 00000000..b1511359
--- /dev/null
+++ b/validation/linear/call-builtin.c
@@ -0,0 +1,17 @@
+typedef unsigned int u32;
+
+u32 ff(u32 a) { return __builtin_popcount(a); }
+
+u32 f0(u32 a) { return (__builtin_popcount)(a); }
+u32 f1(u32 a) { return (*__builtin_popcount)(a); } // C99,C11 6.5.3.2p4
+u32 f2(u32 a) { return (**__builtin_popcount)(a); } // C99,C11 6.5.3.2p4
+u32 f3(u32 a) { return (***__builtin_popcount)(a); } // C99,C11 6.5.3.2p4
+
+/*
+ * check-name: builtin calls
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: load
+ * check-output-pattern(5): call\..*__builtin_.*, %arg1
+ */
diff --git a/validation/linear/call-casted-pointer.c b/validation/linear/call-casted-pointer.c
new file mode 100644
index 00000000..610d6748
--- /dev/null
+++ b/validation/linear/call-casted-pointer.c
@@ -0,0 +1,31 @@
+typedef int (*fun_t)(void*);
+
+int foo(void *a, void *fun)
+{
+ return ((fun_t)fun)(a);
+}
+
+int bar(void *a, void *fun)
+{
+ return ((int (*)(void *))fun)(a);
+}
+
+int qux(void *a, void *fun)
+{
+ return (*(fun_t)fun)(a);
+}
+
+int quz(void *a, void *fun)
+{
+ return (*(int (*)(void *))fun)(a);
+}
+
+/*
+ * check-name: call via casted function pointer
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: load
+ * check-output-pattern(4): ptrcast\..* %arg2
+ * check-output-pattern(4): call\..* %arg1
+ */
diff --git a/validation/linear/call-complex-pointer.c b/validation/linear/call-complex-pointer.c
new file mode 100644
index 00000000..ea8232f1
--- /dev/null
+++ b/validation/linear/call-complex-pointer.c
@@ -0,0 +1,32 @@
+int foo(int p, int (*f0)(int), int (*f1)(int), int arg)
+{
+ return (p ? f0 : f1)(arg);
+}
+
+/*
+ * check-name: call-complex-pointer
+ * check-command: test-linearize -m64 -Wno-decl $file
+ *
+ * check-output-start
+foo:
+.L0:
+ <entry-point>
+ cbr %arg1, .L2, .L3
+
+.L2:
+ phisrc.64 %phi1 <- %arg2
+ br .L4
+
+.L3:
+ ptrcast.64 %r5 <- (64) %arg3
+ phisrc.64 %phi2 <- %r5
+ br .L4
+
+.L4:
+ phi.64 %r6 <- %phi1, %phi2
+ call.32 %r7 <- %r6, %arg4
+ ret.32 %r7
+
+
+ * check-output-end
+ */
diff --git a/validation/linear/call-direct.c b/validation/linear/call-direct.c
new file mode 100644
index 00000000..52f86306
--- /dev/null
+++ b/validation/linear/call-direct.c
@@ -0,0 +1,17 @@
+extern int fun(void);
+
+int ff(void) { return fun(); }
+
+int f0(void) { return (fun)(); }
+int f1(void) { return (*fun)(); } // C99,C11 6.5.3.2p4
+int f2(void) { return (**fun)(); } // C99,C11 6.5.3.2p4
+int f3(void) { return (***fun)(); } // C99,C11 6.5.3.2p4
+
+/*
+ * check-name: direct calls
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: load
+ * check-output-pattern(5): call\..* fun
+ */
diff --git a/validation/linear/call-indirect.c b/validation/linear/call-indirect.c
new file mode 100644
index 00000000..1275910c
--- /dev/null
+++ b/validation/linear/call-indirect.c
@@ -0,0 +1,15 @@
+int gg(int (*fun)(void)) { return fun(); }
+
+int g0(int (*fun)(void)) { return (fun)(); }
+int g1(int (*fun)(void)) { return (*fun)(); } // C99,C11 6.5.3.2p4
+int g2(int (*fun)(void)) { return (**fun)(); } // C99,C11 6.5.3.2p4
+int g3(int (*fun)(void)) { return (***fun)(); } // C99,C11 6.5.3.2p4
+
+/*
+ * check-name: indirect calls
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: load
+ * check-output-pattern(5): call\..* %arg1
+ */
diff --git a/validation/linear/call-inline.c b/validation/linear/call-inline.c
new file mode 100644
index 00000000..a33f0a1c
--- /dev/null
+++ b/validation/linear/call-inline.c
@@ -0,0 +1,18 @@
+static inline int fun(void) { return 42; }
+
+int fi(void) { return fun(); }
+
+int i0(void) { return (fun)(); }
+int i1(void) { return (*fun)(); } // C99,C11 6.5.3.2p4
+int i2(void) { return (**fun)(); } // C99,C11 6.5.3.2p4
+int i3(void) { return (***fun)(); } // C99,C11 6.5.3.2p4
+
+/*
+ * check-name: inline calls
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: load
+ * check-output-excludes: call
+ * check-output-pattern(5): ret\..* \\$42
+ */
diff --git a/validation/optim/call-complex-pointer.c b/validation/optim/call-complex-pointer.c
new file mode 100644
index 00000000..6cfeb6ab
--- /dev/null
+++ b/validation/optim/call-complex-pointer.c
@@ -0,0 +1,13 @@
+int foo(int p, int (*f0)(int), int (*f1)(int), int arg)
+{
+ return (p ? f0 : f1)(arg);
+}
+/*
+ * check-name: call-complex-pointer
+ * check-command: test-linearize -Wno-decl $file
+ * check-known-to-fail
+ *
+ * check-output-ignore
+ * check-output-excludes: ptrcast\.
+ * check-output-contains: select\.
+ */
diff --git a/validation/sizeof-builtin.c b/validation/sizeof-builtin.c
new file mode 100644
index 00000000..7123e4de
--- /dev/null
+++ b/validation/sizeof-builtin.c
@@ -0,0 +1,15 @@
+int test(void);
+int test(void)
+{
+ return sizeof &__builtin_trap;
+}
+
+/*
+ * check-name: sizeof-builtin
+ * check-command: sparse -Wno-decl $file
+ * check-known-to-fail
+ *
+ * check-error-start
+sizeof-function.c:4:16: error: expression using addressof on a builtin function
+ * check-error-end
+ */
diff --git a/validation/sizeof-function.c b/validation/sizeof-function.c
new file mode 100644
index 00000000..27d535d4
--- /dev/null
+++ b/validation/sizeof-function.c
@@ -0,0 +1,49 @@
+extern int fun(void);
+extern int (*ptr)(void);
+
+static inline int inl(int *a)
+{
+ return *a + 1;
+}
+
+
+int test(void);
+int test(void)
+{
+ unsigned int s = 0;
+
+ // OK
+ s += sizeof &fun;
+ s += sizeof ptr;
+ s += sizeof &ptr;
+ s += sizeof &inl;
+
+ // KO
+ s += sizeof fun;
+ s += sizeof *fun;
+
+ s += sizeof *ptr;
+
+ s += sizeof inl;
+ s += sizeof *inl;
+
+ s += sizeof __builtin_trap;
+ s += sizeof *__builtin_trap;
+
+ return s;
+}
+
+/*
+ * check-name: sizeof-function
+ * check-command: sparse -Wno-decl $file
+ *
+ * check-error-start
+sizeof-function.c:22:14: warning: expression using sizeof on a function
+sizeof-function.c:23:14: warning: expression using sizeof on a function
+sizeof-function.c:25:14: warning: expression using sizeof on a function
+sizeof-function.c:27:14: warning: expression using sizeof on a function
+sizeof-function.c:28:14: warning: expression using sizeof on a function
+sizeof-function.c:30:14: warning: expression using sizeof on a function
+sizeof-function.c:31:14: warning: expression using sizeof on a function
+ * check-error-end
+ */