Debian Codegraph @

clang/test/Sema/varargs.c

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