public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Macro expansion and the preprocessor
@ 2011-01-14 17:57 Adam Mercer
  2011-01-14 18:31 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Adam Mercer @ 2011-01-14 17:57 UTC (permalink / raw)
  To: GCC Help

Hi

In one of our projects we have several functions that are mostly
identical, all that varies is the datatypes on which they operate.
When this code was originally written, over 10 years ago, this was
achieved by using m4 to generate the source multiple times and making
the appropriate substitutions. Whilst this works I don't like it at
all and think that it would be cleaner to use the preprocessor. I've
been trying to code this up but am running into problems:

I have two source files, Grid.c:

#include <lal/LALStdlib.h>
#include <lal/AVFactories.h>
#include <lal/Grid.h>

#define TYPECODE Z
#define TYPE COMPLEX16
#include "Grid_source.c"
#undef TYPECODE
#undef TYPE

and Grid_source.c:

#define GTYPE TYPE##Grid
#define FUNC(f) LAL##TYPECODE##f
#define CFUNC FUNC(CreateGrid)
#define DFUNC FUNC(DestroyGrid)
#define CREATEFUNC FUNC(CreateArray)
#define DESTROYFUNC FUNC(DestroyArray)

void
CFUNC ( LALStatus *stat, GTYPE **grid, UINT4Vector *dimLength, UINT4 dimension )
{
  printf("Hello, World!\n");
}

These are snippets but are enough to illustrate the problem. When I
build it fails with the following:

$ make
 CC     Grid.lo
In file included from Grid.c:106:
Grid_source.c:9: error: expected declaration specifiers or '...'
before 'TYPEGrid'
Grid_source.c:10: warning: no previous prototype for 'LALTYPECODECreateGrid'
Grid_source.c: In function 'LALTYPECODECreateGrid'
$

So it seems that the expansions aren't be done as I would expect them to be.

Can anyone see what I'm doing wrong?

I've been trying to find some documentation for using the preprocessor
in this fashion but have been unsuccessful, does anyone know of a good
resource?

Cheers

Adam

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Macro expansion and the preprocessor
  2011-01-14 17:57 Macro expansion and the preprocessor Adam Mercer
@ 2011-01-14 18:31 ` Jonathan Wakely
  2011-01-14 19:00   ` Adam Mercer
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2011-01-14 18:31 UTC (permalink / raw)
  To: Adam Mercer; +Cc: GCC Help

On 14 January 2011 17:55, Adam Mercer wrote:
>
> So it seems that the expansions aren't be done as I would expect them to be.
>
> Can anyone see what I'm doing wrong?

If you use the -E option to gcc it will print the preprocessor output,
so you can see what it's doing.

Life will be much easier if you keep your example much simpler, until
you get the macros working e.g.

$ cat test.c
#define TYPE COMPLEX16
#define GTYPE TYPE##Grid
GTYPE g;
#undef TYPE

Now you can see what's happening

$ gcc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"


TYPEGrid g;


The problem is that the ## operator doesn't expand its arguments, so
TYPE is used verbatim.

You need to add some levels of indirection, so that GTYPE expands to a
macro which expands TYPE, and passes that to another macro which does
the concatenation.

$ cat test.c
#define TYPE COMPLEX16
#define DO_JOIN2(a, b) a##b
#define JOIN2(a, b) DO_JOIN2(a, b)
#define GTYPE JOIN2(TYPE, Grid)
GTYPE g;
#undef TYPE


$ gcc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"




COMPLEX16Grid g;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Macro expansion and the preprocessor
  2011-01-14 18:31 ` Jonathan Wakely
@ 2011-01-14 19:00   ` Adam Mercer
  0 siblings, 0 replies; 3+ messages in thread
From: Adam Mercer @ 2011-01-14 19:00 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: GCC Help

On Fri, Jan 14, 2011 at 12:31, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:

> The problem is that the ## operator doesn't expand its arguments, so
> TYPE is used verbatim.
>
> You need to add some levels of indirection, so that GTYPE expands to a
> macro which expands TYPE, and passes that to another macro which does
> the concatenation.

Thanks a lot, that does the trick!

Cheers

Adam

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-01-14 19:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-14 17:57 Macro expansion and the preprocessor Adam Mercer
2011-01-14 18:31 ` Jonathan Wakely
2011-01-14 19:00   ` Adam Mercer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).