aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/validation
diff options
authorEdward Cree <ecree@solarflare.com>2017-02-21 12:22:18 +0000
committerChristopher Li <sparse@chrisli.org>2017-03-04 00:45:38 +0800
commita9432b3336e8e584c85ed8a03535aa7564e93514 (patch)
tree92933fde8dffd41ac577d73aad972db1eb51843d /validation
parent0dfda0d1f0fe672c5aabdaf67665ec9b5aeaa4ac (diff)
downloadsparse-dev-a9432b3336e8e584c85ed8a03535aa7564e93514.tar.gz
Allow casting to a restricted type if !restricted_value
If the operand of a cast to a restricted type is an unrestricted value, the cast should not produce a warning, since an equivalent implied cast (e.g. in an initialiser) would not do so. Also adds a test case (bitwise-cast.c) testing implicit and explicit conversions of zero and nonzero integers to bitwise type. Signed-off-by: Edward Cree <ecree@solarflare.com> Acked-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> Tested-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> Signed-off-by: Christopher Li <sparse@chrisli.org>
Diffstat (limited to 'validation')
-rw-r--r--validation/bitwise-cast.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
new file mode 100644
index 00000000..baeca29e
--- /dev/null
+++ b/validation/bitwise-cast.c
@@ -0,0 +1,44 @@
+typedef unsigned int u32;
+typedef u32 __attribute__((bitwise)) __be32;
+
+/* Implicit casts of 0, legal */
+static __be32 foo(void)
+{
+ __be32 x = 0;
+
+ return 0;
+}
+
+/* Explicit cast of 0, legal */
+static __be32 bar(void)
+{
+ return (__be32)0;
+}
+
+/* Implicit casts of nonzero, bad */
+static __be32 baz(void)
+{
+ __be32 x = 0x2a;
+
+ return 99;
+}
+
+/* Explicit cast of nonzero, bad */
+static __be32 quux(void)
+{
+ return (__be32)1729;
+}
+
+/*
+ * check-name: conversions to bitwise types
+ * check-command: sparse -Wbitwise $file
+ * check-error-start
+bitwise-cast.c:21:20: warning: incorrect type in initializer (different base types)
+bitwise-cast.c:21:20: expected restricted __be32 [usertype] x
+bitwise-cast.c:21:20: got int
+bitwise-cast.c:23:16: warning: incorrect type in return expression (different base types)
+bitwise-cast.c:23:16: expected restricted __be32
+bitwise-cast.c:23:16: got int
+bitwise-cast.c:29:17: warning: cast to restricted __be32
+ * check-error-end
+ */