Search code examples
uncrustify

Configure uncrustify to indent c macro


I rely on uncrustify to format C source files and I would like to update from v0.69 to v0.74 but .. I can't get this config consistent:

Let's consider the following example (which is already formatted the way I want it to be):

#if defined(CONFIG_HAS_A)
#define DEVICE_NAME__A_LIST                                                   \
    A1_NAME                                                                   \
    A2_NAME                                                                   \
    ""

#define DEVICE_NAME__A_COUNT                                                  \
    A1_COUNT +                                                                \
    A2_COUNT +                                                                \
    0
#endif

#if defined(CONFIG_HAS_B)
#define DEVICE_NAME__B_LIST                                                   \
    B1_NAME                                                                   \
    B2_NAME                                                                   \
    B3_NAME                                                                   \
    ""

#define DEVICE_NAME__B_COUNT                                                  \
    B1_COUNT +                                                                \
    B2_COUNT +                                                                \
    B3_COUNT +                                                                \
    0
#endif

Using the version 0.74 of uncrustify, the above example code gets formatted to this:

#if defined(CONFIG_HAS_A)
#define DEVICE_NAME__A_LIST                                                   \
    A1_NAME                                                                   \
        A2_NAME                                                                   \
    ""

#define DEVICE_NAME__A_COUNT                                                  \
    A1_COUNT +                                                                \
    A2_COUNT +                                                                \
    0
#endif

#if defined(CONFIG_HAS_B)
#define DEVICE_NAME__B_LIST                                                   \
    B1_NAME                                                                   \
    B2_NAME                                                                   \
    B3_NAME                                                                   \
    ""

#define DEVICE_NAME__B_COUNT                                                  \
    B1_COUNT +                                                                \
    B2_COUNT +                                                                \
    B3_COUNT +                                                                \
    0
#endif

As you can see, the resulting format for the first define (DEVICE_NAME__A_LIST) looks weird and inconsistent compared to the rest of the file ..

What is the right uncrustify config option to avoid this?

NOTE: I found the pp_ignore_define_body option, but it is way too sever as I simply would like the code to be indented one level.

EDIT: I found out that some *_NAME empty macros cause uncrustify to misbehave right after them.


Solution

  • Regarding the few number of files concerned with this issue I wrapped the concerned macros with *INDENT-OFF* and *INDENT-ON*:

    /* *INDENT-OFF* */
    #define DEVICE_NAME__A_LIST    \
        A1_NAME                    \
        A2_NAME                    \
        ""
    /* *INDENT-ON* */
    

    These tags prevent uncrustify from processing the encapsulated content.