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!

0
6 comments

Hello!

On what build system is your project based?

0

It's a CMake project

0

Could you please provide a definition of LTSTRS, for example?

0
typedef uint8_t const *const *VTStrs;
#define LTSTRS (VTStrs)(char const *const [])
typedef unsigned int const (*VTSizes)[2];
#define LTSIZES (VTSizes)(unsigned const int [][2])

They are helper macro to write compound literals.

I'd like to keep compound literals always inline.

0

What CLion version do you use?

On my side the result of Code | Reformat Code with your settings in CLion 2020.2.4 is:

0

(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.

#define initializeString(...) vtInitializeString( __VA_ARGS__ )
#define initializeBlank(...) vtInitializeBlank( __VA_ARGS__ )

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:

#define initializeObj(OBJ)  (OBJ)->x = 0;           \
(OBJ)->y = 0; \
(OBJ)->visible = true; \
(OBJ)->penSize = 0; \
(OBJ)->penChar = '#'; \
(OBJ)->currentSprite = (OBJ)->sprites

#define initializeCharMap(CHARMAP, WIDTH, HEIGHT) (CHARMAP)->width = (WIDTH); \
(CHARMAP)->height = (HEIGHT); \
(CHARMAP)->chars = malloc(sizeofChars(CHARMAP))

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.

0

Please sign in to leave a comment.