macro_fn_comma_swallow2.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Test the __VA_ARGS__ comma swallowing extensions of various compiler dialects.
  2. // RUN: %clang_cc1 -E %s | FileCheck -check-prefix=GCC -strict-whitespace %s
  3. // RUN: %clang_cc1 -E -std=c99 %s | FileCheck -check-prefix=C99 -strict-whitespace %s
  4. // RUN: %clang_cc1 -E -std=c11 %s | FileCheck -check-prefix=C99 -strict-whitespace %s
  5. // RUN: %clang_cc1 -E -x c++ %s | FileCheck -check-prefix=GCC -strict-whitespace %s
  6. // RUN: %clang_cc1 -E -std=gnu99 %s | FileCheck -check-prefix=GCC -strict-whitespace %s
  7. // RUN: %clang_cc1 -E -fms-compatibility %s | FileCheck -check-prefix=MS -strict-whitespace %s
  8. // RUN: %clang_cc1 -E -DNAMED %s | FileCheck -check-prefix=GCC -strict-whitespace %s
  9. // RUN: %clang_cc1 -E -std=c99 -DNAMED %s | FileCheck -check-prefix=C99 -strict-whitespace %s
  10. #ifndef NAMED
  11. # define A(...) [ __VA_ARGS__ ]
  12. # define B(...) [ , __VA_ARGS__ ]
  13. # define C(...) [ , ## __VA_ARGS__ ]
  14. # define D(A,...) [ A , ## __VA_ARGS__ ]
  15. # define E(A,...) [ __VA_ARGS__ ## A ]
  16. #else
  17. // These are the GCC named argument versions of the C99-style variadic macros.
  18. // Note that __VA_ARGS__ *may* be used as the name, this is not prohibited!
  19. # define A(__VA_ARGS__...) [ __VA_ARGS__ ]
  20. # define B(__VA_ARGS__...) [ , __VA_ARGS__ ]
  21. # define C(__VA_ARGS__...) [ , ## __VA_ARGS__ ]
  22. # define D(A,__VA_ARGS__...) [ A , ## __VA_ARGS__ ]
  23. # define E(A,__VA_ARGS__...) [ __VA_ARGS__ ## A ]
  24. #endif
  25. 1: A() B() C() D() E()
  26. 2: A(a) B(a) C(a) D(a) E(a)
  27. 3: A(,) B(,) C(,) D(,) E(,)
  28. 4: A(a,b,c) B(a,b,c) C(a,b,c) D(a,b,c) E(a,b,c)
  29. 5: A(a,b,) B(a,b,) C(a,b,) D(a,b,)
  30. // The GCC ", ## __VA_ARGS__" extension swallows the comma when followed by
  31. // empty __VA_ARGS__. This extension does not apply in -std=c99 mode, but
  32. // does apply in C++.
  33. //
  34. // GCC: 1: [ ] [ , ] [ ] [ ] [ ]
  35. // GCC: 2: [ a ] [ , a ] [ ,a ] [ a ] [ a ]
  36. // GCC: 3: [ , ] [ , , ] [ ,, ] [ , ] [ ]
  37. // GCC: 4: [ a,b,c ] [ , a,b,c ] [ ,a,b,c ] [ a ,b,c ] [ b,ca ]
  38. // GCC: 5: [ a,b, ] [ , a,b, ] [ ,a,b, ] [ a ,b, ]
  39. // Under C99 standard mode, the GCC ", ## __VA_ARGS__" extension *does not*
  40. // swallow the comma when followed by empty __VA_ARGS__.
  41. //
  42. // C99: 1: [ ] [ , ] [ , ] [ ] [ ]
  43. // C99: 2: [ a ] [ , a ] [ ,a ] [ a ] [ a ]
  44. // C99: 3: [ , ] [ , , ] [ ,, ] [ , ] [ ]
  45. // C99: 4: [ a,b,c ] [ , a,b,c ] [ ,a,b,c ] [ a ,b,c ] [ b,ca ]
  46. // C99: 5: [ a,b, ] [ , a,b, ] [ ,a,b, ] [ a ,b, ]
  47. // Microsoft's extension is on ", __VA_ARGS__" (without explicit ##) where
  48. // the comma is swallowed when followed by empty __VA_ARGS__.
  49. //
  50. // MS: 1: [ ] [ ] [ ] [ ] [ ]
  51. // MS: 2: [ a ] [ , a ] [ ,a ] [ a ] [ a ]
  52. // MS: 3: [ , ] [ , , ] [ ,, ] [ , ] [ ]
  53. // MS: 4: [ a,b,c ] [ , a,b,c ] [ ,a,b,c ] [ a ,b,c ] [ b,ca ]
  54. // MS: 5: [ a,b, ] [ , a,b, ] [ ,a,b, ] [ a ,b, ]
  55. // FIXME: Item 3(d) in MS output should be [ ] not [ , ]