Undesired formatting of C compound literals
Hi, I'm looking for some insight about the formatter behaviour in CLion.
I have the following C code:
initializeBlank(&canvas, 2, LTSIZES{{60, 10}, {1, 1}}); //LTSIZES is a macro for a cast
initializeString(objs[1], 1, LTSTRS{"World!"}); //LTSTRS is a macro for a cast
I'd like to keep them the way they are, but the formatter creates this super expanded thing:
initializeBlank(&canvas, 2, LTSIZES{
{
60, 10
}, {
1, 1
}
});
initializeString(objs[1], 1, LTSTRS{
"World!"
});
I've been tweaking the settings for a while but found no way to change it. The "keep line breaks when formatting" makes it worse because it leaves potentially wrong newlines in the source. I know there's also the CLang formatter, but I find it much less powerful than the IDE built-in one, so, no good either. It looks like a bug? Could it be?
Here's my formatter settings: 2020_11_07_QB88PS3VHg2Dw7DA (file: Default.xml)
Thank You!
Please sign in to leave a comment.
Hello!
On what build system is your project based?
It's a CMake project
Could you please provide a definition of LTSTRS, for example?
They are helper macro to write compound literals.
I'd like to keep compound literals always inline.
What CLion version do you use?
On my side the result of Code | Reformat Code with your settings in CLion 2020.2.4 is:
(CLion 2020.2.4) By reproducing your example, and getting the same behaviour as you, I discovered that macro functions are the issue.
C has no namespaces, everything is global. The functions declared in the header are prefixed with "vt". An additional header provides unprefixed "aliases", at the user's discretion.
Those break the formatter. To be fair, macros in general break the formatter, it's always a pain to format them by hand, fighting against the editor. Here's some examples:
Here I had to indentate everything by hand, and it's still a bit skewed. The preprocessor is a very important and powerful tool in C, if used right, but in CLion, macros are treated as second-class citizens.
No IDE is perfect, but CLion is good, would be a very welcome improvement in new versions.