travisjeffery / ClangFormat-Xcode

Xcode plug-in to to use clang-format from in Xcode and consistently format your code with Clang
https://twitter.com/travisjeffery
MIT License
2.89k stars 288 forks source link

Which rule to use for C11 _Generic? #131

Open ghost opened 6 years ago

ghost commented 6 years ago

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.

HunterKohler commented 2 years ago

Hello from the future; I too do not know.

ailijic commented 2 years ago

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)