From: Rusty Russell <rusty@rustcorp.com.au>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 1/5] cast_if_type: allow macros functions which take more than one type.
Date: Mon, 4 Feb 2008 23:14:18 +1100 [thread overview]
Message-ID: <200802042314.18738.rusty@rustcorp.com.au> (raw)
In-Reply-To: <200802042311.18762.rusty@rustcorp.com.au>
To create functions which can take two types, but still warn on any
other types, we need a way of casting one type and no others.
To make things more complex, it should correctly handle function args,
NULL, and be usable in initializers. __builtin_choose_expr was introduced
in gcc 3.1 (we need >= 3.2 anyway).
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
---
include/linux/compiler-gcc.h | 17 +++++++++++++++++
include/linux/compiler-intel.h | 2 ++
2 files changed, 19 insertions(+)
diff -r e6626cc7bdc2 include/linux/compiler-gcc.h
--- a/include/linux/compiler-gcc.h Sun Jan 20 18:51:51 2008 +1100
+++ b/include/linux/compiler-gcc.h Sun Jan 20 18:57:14 2008 +1100
@@ -53,3 +53,20 @@
#define noinline __attribute__((noinline))
#define __attribute_const__ __attribute__((__const__))
#define __maybe_unused __attribute__((unused))
+
+/**
+ * cast_if_type - allow an alternate type
+ * @expr: the expression to optionally cast
+ * @oktype: the type to allow.
+ * @desttype: the type to cast to.
+ *
+ * This is used to accept a particular alternate type for an expression:
+ * because any other types will not be cast, they will cause a warning as
+ * normal.
+ *
+ * Note that the unnecessary trinary forces functions to devolve into
+ * function pointers as users expect. */
+#define cast_if_type(expr, oktype, desttype) \
+ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):NULL), \
+ oktype), \
+ (desttype)(expr), (expr))
diff -r e6626cc7bdc2 include/linux/compiler-intel.h
--- a/include/linux/compiler-intel.h Sun Jan 20 18:51:51 2008 +1100
+++ b/include/linux/compiler-intel.h Sun Jan 20 18:57:14 2008 +1100
@@ -29,3 +29,5 @@
#endif
#define uninitialized_var(x) x
+
+#define cast_if_type(expr, oktype, desttype) ((desttype)(expr))
next prev parent reply other threads:[~2008-02-04 12:14 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-04 12:11 [PATCH 0/5] typesafe callbacks Rusty Russell
2008-02-04 12:14 ` Rusty Russell [this message]
2008-02-04 12:16 ` [PATCH 2/5] typesafe: Convert stop_machine Rusty Russell
2008-02-04 12:17 ` [PATCH 3/5] typesafe: kthread_create and kthread_run Rusty Russell
2008-02-04 12:18 ` [PATCH 4/5] typesafe: request_irq and devm_request_irq Rusty Russell
2008-02-04 12:19 ` [PATCH 5/5] typesafe: TIMER_INITIALIZER and setup_timer Rusty Russell
2008-02-04 14:57 ` Al Viro
2008-02-05 3:41 ` Rusty Russell
2008-03-05 2:55 ` Rusty Russell
2008-03-06 10:40 ` Al Viro
2008-03-10 1:07 ` Rusty Russell
2008-03-10 2:03 ` Al Viro
2008-03-10 3:42 ` Rusty Russell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200802042314.18738.rusty@rustcorp.com.au \
--to=rusty@rustcorp.com.au \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.