diff mbox

[1/3] typecheck: introduce assign_if() and assign_if_enabled()

Message ID 1393261707-30565-2-git-send-email-joshc@codeaurora.org (mailing list archive)
State RFC, archived
Headers show

Commit Message

Josh Cartwright Feb. 24, 2014, 5:08 p.m. UTC
The assign_if() and assign_if_enable() macros are intended to be used
in static initializers for function pointers, where the pointer is
expected to be NULL when a compile-time condition does not hold.

These macros allow for implementing this behavior, without requiring the
functions be wrapped in #ifdef conditionals, and while providing
typesafety regardless of the value of the conditional.

For example, the following pattern is common:

	#ifdef CONFIG_FOO
	static void foo_callback(void)
	{
	}
	#else
	#define foo_callback NULL
	#endif

	static struct foo_object foo_obj = {
		.callback = foo_callback,
	};

Usage of assign_if_enabled() allows for achieving the same effect
without the preprocessor conditional, and in addition, allowing the
compiler to typecheck the function regardless of CONFIG_FOO.

	static void foo_callback(void)
	{
	}

	static struct foo_object foo_obj = {
		.callback = assign_if_enabled(CONFIG_FOO, foo_callback),
	};

Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Josh Cartwright <joshc@codeaurora.org>
---
 include/linux/typecheck.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Greg KH Feb. 27, 2014, 7 p.m. UTC | #1
On Mon, Feb 24, 2014 at 11:08:25AM -0600, Josh Cartwright wrote:
> The assign_if() and assign_if_enable() macros are intended to be used
> in static initializers for function pointers, where the pointer is
> expected to be NULL when a compile-time condition does not hold.
> 
> These macros allow for implementing this behavior, without requiring the
> functions be wrapped in #ifdef conditionals, and while providing
> typesafety regardless of the value of the conditional.
> 
> For example, the following pattern is common:
> 
> 	#ifdef CONFIG_FOO
> 	static void foo_callback(void)
> 	{
> 	}
> 	#else
> 	#define foo_callback NULL
> 	#endif
> 
> 	static struct foo_object foo_obj = {
> 		.callback = foo_callback,
> 	};
> 
> Usage of assign_if_enabled() allows for achieving the same effect
> without the preprocessor conditional, and in addition, allowing the
> compiler to typecheck the function regardless of CONFIG_FOO.
> 
> 	static void foo_callback(void)
> 	{
> 	}
> 
> 	static struct foo_object foo_obj = {
> 		.callback = assign_if_enabled(CONFIG_FOO, foo_callback),
> 	};
> 
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Josh Cartwright <joshc@codeaurora.org>
> ---
>  include/linux/typecheck.h | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
> 
> diff --git a/include/linux/typecheck.h b/include/linux/typecheck.h
> index eb5b74a..04134c7 100644
> --- a/include/linux/typecheck.h
> +++ b/include/linux/typecheck.h
> @@ -21,4 +21,22 @@
>  	(void)__tmp; \
>  })
>  
> +/*
> + * Intended for use in static object initializers,
> + * assign_if(const_expr, function) evaluates to 'function' if 'const_expr',
> + * otherwise NULL.
> + *
> + * The type of the assign_if() expression is typeof(function), and therefore
> + * can provide typechecking regardless of 'const_expr'.
> + *
> + * gcc considers 'function' to be used and will not generate a 'defined but not
> + * used' warning when not 'const_expr', however, gcc is smart enough to
> + * eliminate 'function' if assign_if() is the only reference.
> + */

What version of gcc started doing this?  Does llvm also do this?

thanks,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Josh Cartwright Feb. 27, 2014, 11:48 p.m. UTC | #2
On Thu, Feb 27, 2014 at 11:00:32AM -0800, Greg Kroah-Hartman wrote:
> On Mon, Feb 24, 2014 at 11:08:25AM -0600, Josh Cartwright wrote:
> > +/*
> > + * Intended for use in static object initializers,
> > + * assign_if(const_expr, function) evaluates to 'function' if 'const_expr',
> > + * otherwise NULL.
> > + *
> > + * The type of the assign_if() expression is typeof(function), and therefore
> > + * can provide typechecking regardless of 'const_expr'.
> > + *
> > + * gcc considers 'function' to be used and will not generate a 'defined but not
> > + * used' warning when not 'const_expr', however, gcc is smart enough to
> > + * eliminate 'function' if assign_if() is the only reference.
> > + */
> 
> What version of gcc started doing this?  Does llvm also do this?

I'll need to dig up some old gcc's to give this a more thorough
testing; testing with clang 3.4, and it appears to have the same
behavior, at least when I throw a trivial usecase at it.

	$ clang --version
	clang version 3.4 (tags/RELEASE_34/final)
	Target: x86_64-unknown-linux-gnu
	Thread model: posix

	$ cat test.c
	static void foo(void)
	{
		extern void BROKEN(void);
		BROKEN();
	}
	void (*callback)(void) = 0 ? foo : 0;

	$ clang -Wall -Werror -c test.c

	$ size test.o
	   text	   data	    bss	    dec	    hex	filename
	      0	      0	      8	      8	      8	/tmp/test.o

	$ nm test.o
	0000000000000000 B callback

Although, given the feedback on the other patches, assign_if() may
become just be a solution in search of a problem :).

Thanks,
  Josh
diff mbox

Patch

diff --git a/include/linux/typecheck.h b/include/linux/typecheck.h
index eb5b74a..04134c7 100644
--- a/include/linux/typecheck.h
+++ b/include/linux/typecheck.h
@@ -21,4 +21,22 @@ 
 	(void)__tmp; \
 })
 
+/*
+ * Intended for use in static object initializers,
+ * assign_if(const_expr, function) evaluates to 'function' if 'const_expr',
+ * otherwise NULL.
+ *
+ * The type of the assign_if() expression is typeof(function), and therefore
+ * can provide typechecking regardless of 'const_expr'.
+ *
+ * gcc considers 'function' to be used and will not generate a 'defined but not
+ * used' warning when not 'const_expr', however, gcc is smart enough to
+ * eliminate 'function' if assign_if() is the only reference.
+ */
+#define assign_if(const_expr,function)	\
+	((const_expr) ? function : NULL)
+
+#define assign_if_enabled(option,function)	\
+	assign_if(IS_ENABLED(option), function)
+
 #endif		/* TYPECHECK_H_INCLUDED */