1 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple i386-pc-unknown
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-apple-darwin9
3 // RUN: %cheri_cc1 -fsyntax-only -verify %s  -target-abi purecap
4 // RUN: %clang_cc1 -fsyntax-only -fms-compatibility -DMS -verify %s -triple x86_64-pc-win32
5 
f1(int a)6 void f1(int a)
7 {
8     __builtin_va_list ap;
9 
10     __builtin_va_start(ap, a, a); // expected-error {{too many arguments to function}}
11     __builtin_va_start(ap, a); // expected-error {{'va_start' used in function with fixed args}}
12 }
13 
f2(int a,int b,...)14 void f2(int a, int b, ...)
15 {
16     __builtin_va_list ap;
17 
18     __builtin_va_start(ap, 10); // expected-warning {{second argument to 'va_start' is not the last named parameter}}
19     __builtin_va_start(ap, a); // expected-warning {{second argument to 'va_start' is not the last named parameter}}
20     __builtin_va_start(ap, b);
21 }
22 
f3(float a,...)23 void f3(float a, ...) { // expected-note 2{{parameter of type 'float' is declared here}}
24     __builtin_va_list ap;
25 
26     __builtin_va_start(ap, a); // expected-warning {{passing an object that undergoes default argument promotion to 'va_start' has undefined behavior}}
27     __builtin_va_start(ap, (a)); // expected-warning {{passing an object that undergoes default argument promotion to 'va_start' has undefined behavior}}
28 }
29 
30 
31 // stdarg: PR3075 and PR2531
f4(const char * msg,...)32 void f4(const char *msg, ...) {
33  __builtin_va_list ap;
34  __builtin_stdarg_start((ap), (msg));
35  __builtin_va_end (ap);
36 }
37 
f5()38 void f5() {
39   __builtin_va_list ap;
40   __builtin_va_start(ap,ap); // expected-error {{'va_start' used in function with fixed args}}
41 }
42 
f6(int a,...)43 void f6(int a, ...) {
44   __builtin_va_list ap;
45   __builtin_va_start(ap); // expected-error {{too few arguments to function}}
46 }
47 
48 // PR3350
49 void
foo(__builtin_va_list authors,...)50 foo(__builtin_va_list authors, ...) {
51   __builtin_va_start (authors, authors);
52   (void)__builtin_va_arg(authors, int);
53   __builtin_va_end (authors);
54 }
55 
f7(int a,...)56 void f7(int a, ...) {
57   __builtin_va_list ap;
58   __builtin_va_start(ap, a);
59   // FIXME: This error message is sub-par.
60   __builtin_va_arg(ap, int) = 1; // expected-error {{expression is not assignable}}
61   int *x = &__builtin_va_arg(ap, int); // expected-error {{cannot take the address of an rvalue}}
62   __builtin_va_end(ap);
63 }
64 
f8(int a,...)65 void f8(int a, ...) {
66   __builtin_va_list ap;
67   __builtin_va_start(ap, a);
68   (void)__builtin_va_arg(ap, void); // expected-error {{second argument to 'va_arg' is of incomplete type 'void'}}
69   __builtin_va_end(ap);
70 }
71 
72 enum E { x = -1, y = 2, z = 10000 };
f9(__builtin_va_list args)73 void f9(__builtin_va_list args)
74 {
75     (void)__builtin_va_arg(args, float); // expected-warning {{second argument to 'va_arg' is of promotable type 'float'}}
76     (void)__builtin_va_arg(args, enum E); // Don't warn here in C
77     (void)__builtin_va_arg(args, short); // expected-warning {{second argument to 'va_arg' is of promotable type 'short'}}
78     (void)__builtin_va_arg(args, char); // expected-warning {{second argument to 'va_arg' is of promotable type 'char'}}
79 }
80 
f10(int a,...)81 void f10(int a, ...) {
82   int i;
83   __builtin_va_list ap;
84   i = __builtin_va_start(ap, a); // expected-error {{assigning to 'int' from incompatible type 'void'}}
85   __builtin_va_end(ap);
86 }
87 
f11(short s,...)88 void f11(short s, ...) {  // expected-note {{parameter of type 'short' is declared here}}
89   __builtin_va_list ap;
90   __builtin_va_start(ap, s); // expected-warning {{passing an object that undergoes default argument promotion to 'va_start' has undefined behavior}}
91   __builtin_va_end(ap);
92 }
93 
f12(register int i,...)94 void f12(register int i, ...) {  // expected-note {{parameter of type 'int' is declared here}}
95   __builtin_va_list ap;
96   __builtin_va_start(ap, i); // expected-warning {{passing a parameter declared with the 'register' keyword to 'va_start' has undefined behavior}}
97   __builtin_va_end(ap);
98 }
99 
100 enum __attribute__((packed)) E1 {
101   one1
102 };
103 
f13(enum E1 e,...)104 void f13(enum E1 e, ...) {
105   __builtin_va_list va;
106   __builtin_va_start(va, e);
107 #ifndef MS
108   // In Microsoft compatibility mode, all enum types are int, but in
109   // non-ms-compatibility mode, this enumeration type will undergo default
110   // argument promotions.
111   // expected-note@-7 {{parameter of type 'enum E1' is declared here}}
112   // expected-warning@-6 {{passing an object that undergoes default argument promotion to 'va_start' has undefined behavior}}
113 #endif
114   __builtin_va_end(va);
115 }
116 
f14(int e,...)117 void f14(int e, ...) {
118   // expected-warning@+3 {{implicitly declaring library function 'va_start'}}
119   // expected-note@+2 {{include the header <stdarg.h>}}
120   // expected-error@+1 {{too few arguments to function call}}
121   va_start();
122   __builtin_va_list va;
123   va_start(va, e);
124 }
125