[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / varargs.c
blob2cb7270f604a0feadcab411f59bccfbffaf8ea11
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
5 void f1(int a)
7 __builtin_va_list ap;
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}}
13 void f2(int a, int b, ...)
15 __builtin_va_list ap;
17 __builtin_va_start(ap, 10); // expected-warning {{second argument to 'va_start' is not the last named parameter}}
18 __builtin_va_start(ap, a); // expected-warning {{second argument to 'va_start' is not the last named parameter}}
19 __builtin_va_start(ap, b);
22 void f3(float a, ...) { // expected-note 2{{parameter of type 'float' is declared here}}
23 __builtin_va_list ap;
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}}
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);
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}}
42 void f6(int a, ...) {
43 __builtin_va_list ap;
44 __builtin_va_start(ap); // expected-error {{too few arguments to function}}
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);
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);
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);
71 enum E { x = -1, y = 2, z = 10000 };
72 void f9(__builtin_va_list args)
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'}}
80 void f10(int a, ...) {
81 int i;
82 __builtin_va_list ap;
83 i = __builtin_va_start(ap, a); // expected-error {{assigning to 'int' from incompatible type 'void'}}
84 __builtin_va_end(ap);
87 void f11(short s, ...) { // expected-note {{parameter of type 'short' is declared here}}
88 __builtin_va_list ap;
89 __builtin_va_start(ap, s); // expected-warning {{passing an object that undergoes default argument promotion to 'va_start' has undefined behavior}}
90 __builtin_va_end(ap);
93 void f12(register int i, ...) { // expected-note {{parameter of type 'int' is declared here}}
94 __builtin_va_list ap;
95 __builtin_va_start(ap, i); // expected-warning {{passing a parameter declared with the 'register' keyword to 'va_start' has undefined behavior}}
96 __builtin_va_end(ap);
99 enum __attribute__((packed)) E1 {
100 one1
103 void f13(enum E1 e, ...) {
104 __builtin_va_list va;
105 __builtin_va_start(va, e);
106 #ifndef MS
107 // In Microsoft compatibility mode, all enum types are int, but in
108 // non-ms-compatibility mode, this enumeration type will undergo default
109 // argument promotions.
110 // expected-note@-7 {{parameter of type 'enum E1' is declared here}}
111 // expected-warning@-6 {{passing an object that undergoes default argument promotion to 'va_start' has undefined behavior}}
112 #endif
113 __builtin_va_end(va);
116 void f14(int e, ...) {
117 // expected-error@+3 {{call to undeclared library function 'va_start'}}
118 // expected-note@+2 {{include the header <stdarg.h>}}
119 // expected-error@+1 {{too few arguments to function call}}
120 va_start();
121 __builtin_va_list va;
122 va_start(va, e);