aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-05-11 14:23:11 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-05-11 22:11:20 +0200
commita6aaa59cec44972513f1544eb4c2d389eadf14a6 (patch)
treea30ea4333afbd60cc4daa0aa94c314f2313f1389
parente35efe330c6ae7d154197c29b127560d569016d0 (diff)
downloadsparse-dev-a6aaa59cec44972513f1544eb4c2d389eadf14a6.tar.gz
avoid useless warning for 'bool <- restricted type' conversion
Conversion to bool is special in C since this conversion is essentially the result of the comparison with zero. As such, some operations which are normally unsafe to do with restricted types, like casting to an unrestricted type, are in fact safe to do when converting to bool and issuing a warning in those case is useless, confusing and causes people to add useless casts in the code in order to shut up the warning. Fix this by catching such 'bool <- restricted type' conversion and avoid such warnings. CC: Al Viro <viro@zeniv.linux.org.uk> Originally-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--evaluate.c6
-rw-r--r--validation/bool-cast-bad.c3
-rw-r--r--validation/bool-cast-implicit.c3
-rw-r--r--validation/bool-cast-restricted.c25
4 files changed, 31 insertions, 6 deletions
diff --git a/evaluate.c b/evaluate.c
index 97685791..3dc26fc0 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1345,6 +1345,12 @@ static int check_assignment_types(struct symbol *target, struct expression **rp,
return 1;
} else if (!(sclass & TYPE_RESTRICT))
goto Cast;
+ if (t == &bool_ctype) {
+ if (is_fouled_type(s))
+ warning((*rp)->pos, "%s degrades to integer",
+ show_typename(s->ctype.base_type));
+ goto Cast;
+ }
*typediff = "different base types";
return 0;
}
diff --git a/validation/bool-cast-bad.c b/validation/bool-cast-bad.c
index b7e7c058..a0b091e1 100644
--- a/validation/bool-cast-bad.c
+++ b/validation/bool-cast-bad.c
@@ -15,9 +15,6 @@ static _Bool fstse(struct s a) { return (_Bool)a; }
* check-command: sparse $file
*
* check-error-start
-bool-cast-bad.c:8:41: warning: incorrect type in return expression (different base types)
-bool-cast-bad.c:8:41: expected bool
-bool-cast-bad.c:8:41: got restricted le16 [usertype] a
bool-cast-bad.c:9:42: warning: cast from restricted le16
bool-cast-bad.c:10:41: warning: incorrect type in return expression (different base types)
bool-cast-bad.c:10:41: expected bool
diff --git a/validation/bool-cast-implicit.c b/validation/bool-cast-implicit.c
index ee8b705b..9d89443b 100644
--- a/validation/bool-cast-implicit.c
+++ b/validation/bool-cast-implicit.c
@@ -21,8 +21,5 @@ static _Bool fres(le16 a) { return a; }
* check-output-excludes: cast\\.
*
* check-error-start
-bool-cast-implicit.c:15:36: warning: incorrect type in return expression (different base types)
-bool-cast-implicit.c:15:36: expected bool
-bool-cast-implicit.c:15:36: got restricted le16 [usertype] a
* check-error-end
*/
diff --git a/validation/bool-cast-restricted.c b/validation/bool-cast-restricted.c
new file mode 100644
index 00000000..f6776b05
--- /dev/null
+++ b/validation/bool-cast-restricted.c
@@ -0,0 +1,25 @@
+typedef unsigned int __attribute__((bitwise)) large_t;
+#define LBIT ((__attribute__((force)) large_t) 1)
+
+_Bool lfoo(large_t x) { return x; }
+_Bool lbar(large_t x) { return ~x; }
+_Bool lbaz(large_t x) { return !x; }
+_Bool lqux(large_t x) { return x & LBIT; }
+
+
+typedef unsigned short __attribute__((bitwise)) small_t;
+#define SBIT ((__attribute__((force)) small_t) 1)
+
+_Bool sfoo(small_t x) { return x; }
+_Bool sbar(small_t x) { return ~x; }
+_Bool sbaz(small_t x) { return !x; }
+_Bool squx(small_t x) { return x & SBIT; }
+
+/*
+ * check-name: bool-cast-restricted.c
+ * check-command: sparse -Wno-decl $file
+ *
+ * check-error-start
+bool-cast-restricted.c:14:32: warning: restricted small_t degrades to integer
+ * check-error-end
+ */