ClangFormat-Xcode
ClangFormat-Xcode copied to clipboard
Which rule to use for C11 _Generic?
While working on https://github.com/dotnet/corefx/pull/30495, I realized that clang-format has transformed:
#define LIMIT_MAX(T) _Generic(((T)0), \
unsigned int: UINT_MAX, \
unsigned long: ULONG_MAX, \
unsigned long long: ULLONG_MAX)
into:
#define LIMIT_MAX(T) _Generic(((T)0), \
unsigned int \
: UINT_MAX, \
unsigned long \
: ULONG_MAX, \
unsigned long long \
: ULLONG_MAX)
and couldn't quite figure out which *colon* rule needs to be set to leave alone _Generic block: https://clang.llvm.org/docs/ClangFormatStyleOptions.html.
Hello from the future; I too do not know.
This is a workaround. Define a macro that can be used for each line of _Generic, the macro allows you to omit the colon. Then using the Mozilla style you get the following.
#define GEN_LINE(type, fn) \
type: \
fn
#define absValue(m) \
_Generic((m), \
GEN_LINE(F32, F32_absValue), \
GEN_LINE(F64, F64_absValue), \
GEN_LINE(F80, F80_absValue), \
GEN_LINE(int, Int_absValue), \
GEN_LINE(long, Long_absValue), \
GEN_LINE(long long, LongLong_absValue))(m)