Skip to content

Nested macros are not (always) expanded #31

@versat

Description

@versat

See original thread at cppcheck (https://sourceforge.net/p/cppcheck/discussion/general/thread/f6c4e29d/)

Code like this:

#define BUTTON_PORT_NUMBER 2
#define BTN_PIEZO_PIN (0x04)

/// Helper macro to concatenate the parameters
#define CONCATENATOR(x, y) x ## y
/// Helper macro to evaluate the parameters before concatenating
#define EVALUATOR(x, y) CONCATENATOR(x, y)
/// Helper macro to evaluate button port number and add it to 'P'
#define BUTTON_PORT EVALUATOR(P, BUTTON_PORT_NUMBER)

/// Direction port for button
#define BUTTON_PORT_DIR  (EVALUATOR(BUTTON_PORT, DIR))

void func(void)
{
    BUTTON_PORT_DIR &= (uint8_t)(~BTN_PIEZO_PIN);
}

Results in this code:

void func ( void )
{
 (  EVALUATOR(P,2)DIR  ) &= ( uint8_t ) ( ~  (  0x04  ) ) ;
}

It should be:

void func ( void )
{
 (  P2DIR  ) &= ( uint8_t ) ( ~  (  0x04  ) ) ;
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions