0

Trying to do some macros on Code Composer for a MSP430FRx project. The expansion is not working out as expected.

The macro definitions

#define SMCLK PORT_PIN(1, 0)

#define PORT_PIN(port, pin) port,pin

#define GPIO_SEL0_SET(port, pin) \
    _GPIO_SEL0_SET(port, pin)

#define _GPIO_SEL0_SET(port, pin) \
    BIT_SET(P ## port ## SEL0, BIT ## pin)

#define BIT_SET(reg, bit) reg |= bit

This is the expansion that Code Composer does:

GPIO_SEL0_SET(SMCLK) -> GPIO_SEL0_SET(PORT_PIN(1, 0)) -> GPIO_SEL0_SET(1,0) -> _GPIO_SEL0_SET(1,0, )

Here already there is a non intended result. The last expansion added a comma. The expexted expansion was _GPIO_SEL0_SET(1,0)

_GPIO_SEL0_SET(1,0, ) -> BIT_SET(P1SEL0, BIT0) ) -> BIT_SET(P1SEL0,(0x0001)) ) -> P1SEL0 |= (0x0001) )

It ends up having an extra parenthesis.

The weirdest steps happens here: enter image description here

enter image description here

What went wrong in this expansion?

Burst
  • 23
  • 4
  • `GPIO_SEL0_SET(SMCLK)` should give you an error because the macro takes two arguments, not one... I don't know what Code Composer is (IDE? Compiler?), but apparently its C preprocessor implementation is broken. – Shawn Apr 07 '21 at 13:19
  • @Shawn SMCLK expands to `1,0`. – Clifford Apr 07 '21 at 13:31
  • That's just one argument... – Shawn Apr 07 '21 at 13:33
  • The macro explorer is provided by the IDE . Do you know that the pre-processor is also expanding it in that manner? It is likely that the IDE is in error rather than your code. It is a convolutes and ill-advised use of macros in any case - probably bet avoided. `SMCLK` expanding to two arguments is especially insidious. – Clifford Apr 07 '21 at 13:34
  • @Shawn : Fair point. I believe this is an IDE feature not the actual tool-chain pre-processor. The IDE not being a real pre-processor just expands as best it can without checking for errors. – Clifford Apr 07 '21 at 13:37
  • @Shawn : You should post an answer - the issue is exactly that SMCLK does not expand to two arguments because the pre-processor does not work that way. I'd post an answer, but clearly it caught me out too so that would seem like intellectual theft. My solution would be not to try to use macros like that in the first place. – Clifford Apr 07 '21 at 13:39
  • @Clifford I was looking for a decent existing duplicate question. – Shawn Apr 07 '21 at 13:47

0 Answers0