aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Sema/warn-jump-bypasses-init.c
blob: 5d7639bdf78d0d7e46d07fc9e79cc4ae46372f1d (plain)
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
// RUN: %clang_cc1 -fsyntax-only -verify=c,both -Wjump-misses-init %s
// RUN: %clang_cc1 -fsyntax-only -verify=c,both -Wc++-compat %s
// RUN: %clang_cc1 -fsyntax-only -verify=good %s
// RUN: %clang_cc1 -fsyntax-only -verify=good -fms-compatibility %s
// RUN: %clang_cc1 -fsyntax-only -verify=cxx,both -x c++ %s
// good-no-diagnostics

void goto_func_1(void) {
  goto ouch;  // c-warning {{jump from this goto statement to its label is incompatible with C++}} \
                 cxx-error {{cannot jump from this goto statement to its label}}
  int i = 12; // both-note {{jump bypasses variable initialization}}

ouch:
  ;
}

void goto_func_2(void) {
  goto ouch;
  static int i = 12; // This initialization is not jumped over, so no warning.

ouch:
  ;
}

void switch_func(int i) {
  switch (i) {
    int x = 12; // both-note {{jump bypasses variable initialization}}
  case 0:       // c-warning {{jump from switch statement to this case label is incompatible with C++}} \
                   cxx-error {{cannot jump from switch statement to this case label}}
    break;
  }
}

// Statement expressions are a bit strange in that they seem to allow for
// jumping past initialization without being diagnosed, even in C++. Perhaps
// this should change?
void f(void) {
  ({
    goto ouch;
    int i = 12;
  });

  for (int i = ({ goto ouch; int x = 10; x;}); i < 0; ++i) {
  }

ouch:
  ;
}

void indirect(int n) {
DirectJump:
  ;

  void *Table[] = {&&DirectJump, &&Later};
  goto *Table[n]; // c-warning {{jump from this indirect goto statement to one of its possible targets is incompatible with C++}} \
                     cxx-error {{cannot jump from this indirect goto statement to one of its possible targets}}

  int x = 12;     // both-note {{jump bypasses variable initialization}}
Later:            // both-note {{possible target of indirect goto statement}}
  ;
}