cformattingclang-format

Line break after each preprocessor condition with clang-format


I would like to have clang-format insert a line break after each preprocessor condition after the operators.

For example:

#if ((CONDITION_A_SWITCH == ENABLED) || (CONDITION_A_SWITCH == ENABLED) || (CONDITION_A_SWITCH == ENABLED))
[...]
#endif

would then look something like this:

#if (   (CONDITION_A_SWITCH == ENABLED) \
     || (CONDITION_A_SWITCH == ENABLED) \
     || (CONDITION_A_SWITCH == ENABLED) )
[...]
#endif

In the current config I have it would do this basically, but only if the ColumnLimit is exceeded and it would not add line breaks to all of the statements.

# if ((CONDITION_A_SWITCH == ENABLED) || (CONDITION_A_SWITCH == ENABLED)                           \
      || (CONDITION_A_SWITCH == ENABLED))
[...]
# endif

Is there any way to achieve this?


Solution

  • In the meantime clang-format 20 does provide the new config entry BreakBinaryOperations .

    The config

    BasedOnStyle: Google
    
    BreakBinaryOperations: OnePerLine
    ColumnLimit: 100
    

    produces this formatting:

    #if ((CONDITION_A_SWITCH == ENABLED) || \
         (CONDITION_A_SWITCH == ENABLED) || \
         (CONDITION_A_SWITCH == ENABLED))
    [...]
    #endif