Thread (8 messages) 8 messages, 4 authors, 2013-06-20

RE: [PATCH 2/2] perf tools: Make Power7 events available for perf

From: David Laight <hidden>
Date: 2013-06-20 11:35:17
Also in: lkml

I think we should be able to do something better using the C
preprocessor, this is exactly the sort of thing it's good at.
=20
What I mean is something like we do with =
arch/powerpc/include/asm/systbl.h,
where we define the list of syscalls once, and then include it in
multiple places, using different macro definitions to get different
outputs.
There is a 'neat' trick - you can pass a #define macro the
name of another #define - which is then expanded after the
initial expansion. A description I happen to have is pasted below.

	David

Consider what happens when #define foo(x) x(args) is expanded: foo(bar)
clearly becomes bar(args). If we also have #define bar(args) then bar()
is expanded AFTER foo() allowing us to generate any text including args.
So we have passed the name of one #define as a parameter to a different =
#define.

If we replace the definition of foo with
#define foo(x) x(args1) x(args2) then foo(bar) is equivalent to
bar(args1) bar(args2).
This is useful because foo(baz) expands to baz(args1) baz(args2)
allowing us to feed the same set of arguments to more than one #define.

A simple example:
	#define lights(x) x(red) x(orange) x(green)
	#define xx(colour) LIGHT_##colour,
	enum { lights(xx) NUM_LIGHTS };
	#undef xx
	#define xx(colour) #colour,
	static const char light_names[] =3D { lights(xx) };
	#undef xx

This expands to:
	enum { LIGHT_red, LIGHT_orange, LIGHT_green, NUM_LIGHTS };
	static const char light_names[] =3D { =94red=94, =94orange=94, =
=94green=94, };
(We needed to add NUM_LIGHTS because a trailing comma isn=92t valid in a =
C++ enum.)
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help