0

I find it difficult to understand the working of a macro defined with the help of preprocessor directives.

The macro,

TRXEM_SPI_BEGIN()

is defined with the help of two preprocessor directives refereed from two header files. Firstly, I wish to state the declaration of the said macro.

#define TRXEM_SPI_BEGIN() st( TRXEM_PORT_OUT &= ~TRXEM_SPI_SC_N_PIN; NOP();)

As the declaration of macro st () is missing here, I found it defined in a different header file and ti is shown below.

#define st(x) do { x } while (__LINE__ == -1)

Now after combining two macros, the true definition of macro TRXEM_SPI_BEGIN() must be,

#define TRXEM_SPI_BEGIN() do {

( TRXEM_PORT_OUT &= ~TRXEM_SPI_SC_N_PIN; NOP(); )

} while (__LINE__ == -1)

This code is written to work inside a microcontroler where TRXEM_PORT_OUT, RXEM_SPI_SC_N_PIN are memory mapped registers and NOP initiates an instruction cycle that does nothing.

As per my understanding, __LINE__ means the line of code in the c file where __LINE__ lies. That line can never be equal to -1. i.e. this loopmust always be running only once provided the __LINE__ can never be placed in -1 place in a .c file. Simply put, -1 can never be the value of __LINE__.

Therefore, I believe a do while() loop here is unnecessary and the same output could have been achieved by simply without using any looping.

I do not understand the functioning of this macro. I would so much appreciate if someone could elaborate on it.

GingerJack
  • 3,044
  • 1
  • 17
  • 19
  • 1
    These are (function type) macros or macros with arguments not functions. – Mohit Jain May 08 '15 at 07:19
  • 4
    possible duplicate of [do { ... } while (0) — what is it good for?](http://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for) – Mohit Jain May 08 '15 at 07:20
  • @mohitJain this question is completely different to what you have marked it duplicated with. – Pasindu wijesinghe May 08 '15 at 07:27
  • OK, read my answer and then check the question that is marked duplicate :) and then you can leave comment or say thank you if your question is solved :) – Mohit Jain May 08 '15 at 07:31
  • 1
    Presumably `st()` means "statementify", i.e. it makes its argument into a proper C statement. The use of `__LINE == -1` instead of just `0` is just a sign of someone being seriously confused. It's a bug and should be fixed. – unwind May 08 '15 at 08:11

1 Answers1

1

As per my understanding, means the line of code in the c file where __LINE__ lies. That line can never be equal to -1. i.e. this loopmust always be running only once provided the __LINE__ can never be placed in -1 place in a .c file. Simply put, -1 can never be the return value to a __LINE__.

Your understanding is exactly correct here. It is there to make sure the code runs exactly once.

Think of following scenario:

#define BAZ foo();bar();

Now if you do

if(some_cond) BAZ;

This is equivalent to:

if(some_cond) foo();
bar();

Which is most possibly not something you want. So you change it to:

#define BAZ {foo();bar();}

This works fine if written as if(some_cond) foo() else wow(); but would fail compilation if written as if(some_cond) foo(); else wow();

So you define BAZ as

/* No semicolon at end */
#define BAZ do {foo();bar();} while(condition_which_is_always_false)

And you can now write the natural code with intuitive semicolon at end.

In your case, condition_which_is_always_false is __LINE__ == -1

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Thanks you @mohitJain. Truly appreciate your effort. When I first studied your reference it didn't make much sense to me. Now it actually does make much sense and thank you so much for your patience. Truly appreciate it. – Pasindu wijesinghe May 08 '15 at 08:32
  • I am glad if it helps. You may also find [this](http://stackoverflow.com/questions/28985515/is-warning-c4127-conditional-expression-is-constant-ever-helpful) interesting. – Mohit Jain May 08 '15 at 08:34