overscore
overscore

Reputation: 513

#define woes – the macro that isn't

I'm trying to do something like:

#define __attribute__((aligned(2))) __attribute__((space(prog),aligned(2)))

but the compiler yields :

 error: "(" may not appear in macro parameter list

Questions: What gives? How can I do a literal text replace, no bells, no frills ?

Upvotes: 3

Views: 7457

Answers (2)

zwol
zwol

Reputation: 140758

This is not possible with the C preprocessor. You can only define "literal txt replace"s, as you put it, if the text you want to replace is a single identifier ("object-like macro" in C standard parlance). What you wrote causes the preprocessor to think you're trying to define a "function-like macro", with a parameter named "(aligned(2))", which is a syntax error.

I would deal with this problem by wrapping the entire __attribute__ construct in an object-like macro:

#if appropriate condition
#define ATTRIBUTE_ALIGNED_2 __attribute__((space(prog),aligned(2)))
#else
#define ATTRIBUTE_ALIGNED_2 __attribute__((aligned(2)))
#endif

and then replacing __attribute__((aligned(2))) with ATTRIBUTE_ALIGNED_2 throughout the source code.

Upvotes: 4

rix0rrr
rix0rrr

Reputation: 10276

As soon as you start with a parenthesis, you're defining a macro with arguments, and that's bound to some rules.

So you can do, for example:

#define succ(x) (x + 1)

But you can't do:

#define pred(x + 1) x

The preprocessor only does a very limited set of pattern matching.

What you could do instead is something like:

#define __aligned2__ __attribute__((space(prog),aligned(2)))

And use that define instead. If that's not sufficient for your needs, you'll need to do some custom preprocessing using sed instead.

Upvotes: 5

Related Questions