llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.67k stars 11.38k forks source link

clang-format: leading spaces in multi-line macros when using tabs #56081

Open gmarull opened 2 years ago

gmarull commented 2 years ago

In Zephyr RTOS we have added a .clang-format that matches our style guide (similar to the Linux Kernel one). We use tabs for indentation, and spaces to align when needed.

We have found that in multiline macros (something we use extensively), empty lines start with leading spaces. We would expect to have a tab first instead, as other macro lines do.

#define MYDRIVER_DEFINE(i)                 \
    struct mydriver_data data##i;      \ /* starts with a tab */
                                           \ /* starts with spaces, should start with a tab first as other lines */
    struct mydriver_config config##i;  \
    ...

It turns out to be a problem on our side since we also run checkpatch.pl (from Linux) on our patches: it complains about bad indentation/leading spaces. The best solution I have come out is by patching checkpatch so that it ignores such edge case: https://github.com/zephyrproject-rtos/zephyr/pull/46611, however, I wonder if this is an issue clang-format could address, or, if there's an option I have missed.

llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-format

stony8 commented 1 year ago

Just FYI: With clang-format 14.0.6 and a little "bisecting" the content of .clang-format, I came to the interesting conclusion that just setting "Cpp11BracedListStyle: true" magically fixes the misaligned line continuation backslashes I saw in long macro calls.

gmarull commented 1 year ago

I've added Cpp11BracedListStyle: true to our latest .clang-format, but it doesn't resolve the problem described here.

stony8 commented 1 year ago

What a pity. Then it only helps with your .clang-format on v3.1-branch.

gmarull commented 1 year ago

Just tried with v3.1, not solving the problem either.

stony8 commented 1 year ago

Please note that I commented in this LLVM issue to bring to the attention of the clang-format maintainers that Cpp11BracedListStyle has a seemingly totally unrelated effect on trailing backslash alignment. Not sure if I highjacked the issue somehow.

Regarding Zephyr's .clang-format, here's my example, formatted with .clang-format on v3.1-branch. It can be repaired both by using .clang-format from main (dddb5dd) or just setting Cpp11BracedListStyle: true.

#define GPIO_FOO_INIT(n)                                                                             \
    static const struct foo_gpio_config foo_gpio_config_##n = {                                \
        .common =                                                                          \
            {                                                                          \
                .port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n),               \
            },                                                                         \
        .base = DT_INST_REG_ADDR(n),                                                       \
        .config_func = port_##n##_foo_config_func,                                         \
    }; \
                                                                                                     \
    static struct foo_gpio_runtime foo_gpio_runtime_##n;