Clang Project

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