clang-format behaviour for braced lists indent

Hello,

I’ve introduced clang-format (current version 8.0) to enforce some company coding guideline but I am struggling with list initializers.

The formatter works nicely with function declarations and function calls and aligns arguments on break like that:

static void someUnitInternalFunction(
const uint32 someParameter,
uint32 *somePointer,
uint32 normalParameter,
uint8 anotherParameterLong);

uint32 SomeUnitWithSomeVeryLongFunctionName(
uint32 parameterOne,
uint32 parameterTwo,
uint32 parameterThree,
uint32 parameterFour)
{
someUnitInternalFunction(
someInternalVariableWithSomeVeryLongName,
&yetAnotherInternalVariableLongNameStyle,
nowThisNameIsShorter,
nowThisNameIsShorter);
}

This matches the configured continuation indent width. For lists, however, the indent does not match what I’ve expected: A list that exceeds the configured margin is formatted as following (I’m using Cpp11BracedListStyle: true):

static uint8 CddDp83848Reg = {CDDDP83848_BMCR_REGISTER,
CDDDP83848_BMSR_REGISTER,
CDDDP83848_PHYIDR1_REGISTER,
CDDDP83848_PHYIDR2_REGISTER,
CDDDP83848_PHYSTS_REGISTER,
CDDDP83848_RBR_REGISTER};

Whereas I’d expect it the following style:

static uint8 CddDp83848Reg = {
CDDDP83848_BMCR_REGISTER,
CDDDP83848_BMSR_REGISTER,
CDDDP83848_PHYIDR1_REGISTER,
CDDDP83848_PHYIDR2_REGISTER,
CDDDP83848_PHYSTS_REGISTER,
CDDDP83848_RBR_REGISTER};

According to the documentation

Fundamentally, C++11 braced lists are formatted exactly like function calls would be formatted in their place. If the braced list follows a name (e.g. a type or variable name), clang-format formats as if the {} were the parentheses of a function call with that name. If there is no name, a zero-length name is assumed.

And I have the following setting: ContinuationIndentWidth: 4. As you can see the function call is formatted nicely whereas the list initializer isn’t, according to the documentation the formatting should be the same.

Can someone please help me out here? I’ve checked several settings and can’t make clang-format indent initializers according to the (admittedly a bit special) style.

Thanks and BR,
Martin

Attached demo file and clang-format settings

ATT11905.clang-format (3.82 KB)

SomeUnit.c (2.31 KB)

(forwarding this to cfe-dev and BCC’ing llvm-dev, since cfe-dev is a better place for clang-format queries)

Hi Shoaib,

Is this what you need? Can you send the .clang-format file you used?

build$ cat BracedList.cpp

static uint8 CddDp83848Reg = {CDDDP83848_BMCR_REGISTER,

CDDDP83848_BMSR_REGISTER,

CDDDP83848_PHYIDR1_REGISTER,

CDDDP83848_PHYIDR2_REGISTER,

CDDDP83848_PHYSTS_REGISTER,

CDDDP83848_RBR_REGISTER};

build$ bin/clang-format BracedList.cpp

static uint8 CddDp83848Reg = {

CDDDP83848_BMCR_REGISTER,

CDDDP83848_BMSR_REGISTER,

CDDDP83848_PHYIDR1_REGISTER,

CDDDP83848_PHYIDR2_REGISTER,

CDDDP83848_PHYSTS_REGISTER,

CDDDP83848_RBR_REGISTER};

build$

Regards,

Owen

Hi,

Shoaib was so nice to forward this to the correct list. Yes, that’s what I’d need. Deafult or LLVM style would generate this but only because of the low ColumnLimit, which is 120 (and in future even 150) for my case. At least that’s what I’ve figured when comparing against the LLVM style dump.

I’ve attached the clang-format file as well as the source file - maybe the files got lost while forwarding.

Cheers, Martin

ATT11905.clang-format (3.82 KB)

SomeUnit.c (2.31 KB)

Hi Martin,

Please see below:

build$ cp ATT11905.clang-format .clang-format

build$ bin/clang-format SomeUnit.c > SomeUnit.c.c-f

build$ diff SomeUnit.c SomeUnit.c.c-f

30,35c30,36

< static uint8 CddDp83848Reg = {CDDDP83848_BMCR_REGISTER,

< CDDDP83848_BMSR_REGISTER,

< CDDDP83848_PHYIDR1_REGISTER,

< CDDDP83848_PHYIDR2_REGISTER,

< CDDDP83848_PHYSTS_REGISTER,

< CDDDP83848_RBR_REGISTER};

Hi Owen,

looking good, thanks a lot! Will this patch be part of the 9.0 release?

Cheers.M

I have committed the fix into r371571. It won’t be part of 9.0.0, but you can get it from trunk if you want it now.

Regards,

Owen

Ok cool, will have a look at it and maybe build clang-format for internal use. Thanks a lot for looking into it / fixing it !

Cheers.M

Hi Martin,

It seems to work well using the clang-format file you gave me before:

bin$ clang-format -version

clang-format version 10.0.0 (https://github.com/llvm/llvm-project.git 90c78073f73eac58f4f8b4772a896dc8aac023bc)

bin$ clang-format

// shows behaviour of clang-format for line-breaks with initializers

static t_component_list _component_list_variable

= {E_COMPONENT_LIST_VALUE_0,

E_COMPONENT_LIST_VALUE_1,

E_COMPONENT_LIST_VALUE_0,

E_COMPONENT_LIST_VALUE_1,

E_COMPONENT_LIST_VALUE_0,

E_COMPONENT_LIST_OTHER_VALUE};

// shows behaviour of clang-format for line-breaks with initializers

static t_component_list _component_list_variable = {

E_COMPONENT_LIST_VALUE_0,

E_COMPONENT_LIST_VALUE_1,

E_COMPONENT_LIST_VALUE_0,

E_COMPONENT_LIST_VALUE_1,

E_COMPONENT_LIST_VALUE_0,

E_COMPONENT_LIST_OTHER_VALUE};

bin$ cat .clang-format