5
votes

Is there a way to get the C preprocessor (GCC) to run two passes, to fully expand macros?

I'm trying to define macros that support port I/O on a microcontroller using abstract names for pins:

#define LED_RED E, (1<<6)

#define SetPin(port, mask)    PORT ## port ## SET = (mask)
#define ClearPin(port, mask)  PORT ## port ## CLR = (mask)
#define ReadPin(port, mask)   (PORT ## port) & (mask)

Then in my code:

SetPin(LED_RED);
ClearPin(LED_RED);
result = ReadPin(LED_RED);

This is meant to expand to:

PORTESET = (1<<6);
PORTECLR = (1<<6);
result = (PORTE) & (1<<6);

This doesn't compile - I get:

error: macro "SetPin" requires 2 arguments, but only 1 given.

Although this does compile and work OK:

SetPin(E, (1<<6));

So...how to get the C compiler to fully expand these macros?

Or, if not that, how to make this kind of thing work?

1
@MichałWalenciak: macros are perfectly appropriate for embedded C for bit toggling.rost0031
I've been using c++ with templates and it was perfect also ;)Michał Walenciak
@MichałWalenciak: Yes, and in Ruby you can even do meta-programming. This question is about C, though.undur_gongor
@MichałWalenciak: That comment is not useful. This is a C question and macros are part of the language. They are also perfectly valid for C++ in some aspects. In any way, they have a different field of usage than templates in C++.too honest for this site
metaprogramming is simply generating or processing some programs as data. You don't need any homoiconic language for that (even if it then much easier). m4 has been used for metaprogramming tasks related to C code even in SunOS3.2 (1985).Basile Starynkevitch

1 Answers

13
votes

You have to pass the arguments through an additional macro.

#define LED_RED E, (1<<6)
#define SetPin2(port, mask)    PORT ## port ## SET = (mask)
#define SetPin(x) SetPin2(x)

SetPin(LED_RED);

This is due to the order of macro replacement:

  1. First, the arguments of a function-like macro are identified. This already fails if the number of arguments is wrong (as in your code).
  2. Then, the argument tokens are put into the replacement list. Unless they are next to ## or #, they get macro-expanded before.
  3. Finally, the resulting replacement list is scanned for further macro replacements.

With the additional macro "in between", the 2nd steps gets the chance to replace LED_RED.