File: varargs.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (89 lines) | stat: -rw-r--r-- 3,091 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// RUN: %clang_cc1 -std=c++03 -Wno-c++11-extensions -triple i386-pc-unknown -verify %s
// RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin9 -verify %s

__builtin_va_list ap;

class string;
void f(const string& s, ...) {  // expected-note {{parameter of type 'const string &' is declared here}}
  __builtin_va_start(ap, s); // expected-warning {{passing an object of reference type to 'va_start' has undefined behavior}}
}

void g(register int i, ...) { // expected-warning 0-1{{deprecated}}
  __builtin_va_start(ap, i); // UB in C, OK in C++
}

// Don't crash when there is no last parameter.
void no_params(...) {
  int a;
  __builtin_va_start(ap, a); // expected-warning {{second argument to 'va_start' is not the last named parameter}}
}

// Reject this. The __builtin_va_start would execute in Foo's non-variadic
// default ctor.
void record_context(int a, ...) {
  struct Foo {
    // expected-error@+2 {{'va_start' cannot be used outside a function}}
    // expected-error@+1 {{default argument references parameter 'a'}}
    void meth(int a, int b = (__builtin_va_start(ap, a), 0)) {}
  };
}

// Ensure the correct behavior for promotable type UB checking.
void promotable(int a, ...) {
  enum Unscoped1 { One = 0x7FFFFFFF };
  (void)__builtin_va_arg(ap, Unscoped1); // ok

  enum Unscoped2 { Two = 0xFFFFFFFF };
  (void)__builtin_va_arg(ap, Unscoped2); // ok

  enum class Scoped { Three };
  (void)__builtin_va_arg(ap, Scoped); // ok

  enum Fixed : int { Four };
  (void)__builtin_va_arg(ap, Fixed); // ok

  enum FixedSmall : char { Five };
  (void)__builtin_va_arg(ap, FixedSmall); // expected-warning {{second argument to 'va_arg' is of promotable type 'FixedSmall'; this va_arg has undefined behavior because arguments will be promoted to 'int'}}

  enum FixedLarge : long long { Six };
  (void)__builtin_va_arg(ap, FixedLarge); // ok

  // Ensure that qualifiers are ignored.
  (void)__builtin_va_arg(ap, const volatile int);  // ok

  // Ensure that signed vs unsigned doesn't matter either.
  (void)__builtin_va_arg(ap, unsigned int);

  (void)__builtin_va_arg(ap, bool); // expected-warning {{second argument to 'va_arg' is of promotable type 'bool'; this va_arg has undefined behavior because arguments will be promoted to 'int'}}
}

#if __cplusplus >= 201103L
// We used to have bugs identifying the correct enclosing function scope in a
// lambda.

void fixed_lambda_varargs_function(int a, ...) {
  [](int b) {
    __builtin_va_start(ap, b); // expected-error {{'va_start' used in function with fixed args}}
  }(42);
}
void varargs_lambda_fixed_function(int a) {
  [](int b, ...) {
    __builtin_va_start(ap, b); // correct
  }(42);
}

auto fixed_lambda_global = [](int f) {
  __builtin_va_start(ap, f); // expected-error {{'va_start' used in function with fixed args}}
};
auto varargs_lambda_global = [](int f, ...) {
  __builtin_va_start(ap, f); // correct
};

void record_member_init(int a, ...) {
  struct Foo {
    int a = 0;
    // expected-error@+1 {{'va_start' cannot be used outside a function}}
    int b = (__builtin_va_start(ap, a), 0);
  };
}
#endif