[clang-format] Properly handle the C11 _Generic keyword.
authorEmilia Dreamer <emilia@rymiel.space>
Wed, 11 Jan 2023 04:00:07 +0000 (06:00 +0200)
committerEmilia Dreamer <emilia@rymiel.space>
Wed, 11 Jan 2023 04:00:16 +0000 (06:00 +0200)
commit0904e0bac8314249ee7c1b0658f1a1cc92ccd050
tree0b3e7db21da4c90c410a51f4e4d3e4756c2bb70d
parent51ba660a0700e659e1a97f3ca5879b8617ec5c26
[clang-format] Properly handle the C11 _Generic keyword.

This patch properly recognizes the generic selection expression
introduced in C11, by adding an additional token type for the colons
present in such expressions.

Previously, they would be recognized as
"inline ASM colons" purely by the fact that those are the last thing
checked for.

I tried to avoid adding an addition token type, but since colons by
default like having spaces around them, I chose to add a new type so
that no space is added after the type selector.

Currently, no aspect of the formatting of these expressions in able to
be configured, as I'm not sure what could even be configured here.

One notable thing is that association list is always formatted as
either entirely on one line, if it can fit, or with line breaks
after every comma in the expression (also after the controlling expr.)

This visually makes them more similar to switch statements when long,
matching the behaviour of the selection expression, being that of a sort
of switch on types, but also allows for terseness when only selecting
for a few things.

Fixes https://github.com/llvm/llvm-project/issues/18080

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D139211
clang/lib/Format/ContinuationIndenter.cpp
clang/lib/Format/FormatToken.h
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp