ClangFormat-Xcode icon indicating copy to clipboard operation
ClangFormat-Xcode copied to clipboard

Which rule to use for C11 _Generic?

Open ghost opened this issue 7 years ago • 2 comments

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.

ghost avatar Jun 20 '18 05:06 ghost

Hello from the future; I too do not know.

jhunterkohler avatar Oct 08 '21 12:10 jhunterkohler

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)

ailijic avatar Dec 09 '21 17:12 ailijic