xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/switch.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2*f4a2713aSLionel Sambuc 
test()3*f4a2713aSLionel Sambuc void test() {
4*f4a2713aSLionel Sambuc   bool x = true;
5*f4a2713aSLionel Sambuc   switch (x) { // expected-warning {{bool}}
6*f4a2713aSLionel Sambuc     case 0:
7*f4a2713aSLionel Sambuc       break;
8*f4a2713aSLionel Sambuc   }
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc   int n = 3;
11*f4a2713aSLionel Sambuc   switch (n && true) { // expected-warning {{bool}}
12*f4a2713aSLionel Sambuc     case 1:
13*f4a2713aSLionel Sambuc       break;
14*f4a2713aSLionel Sambuc   }
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc // PR5518
18*f4a2713aSLionel Sambuc struct A {
19*f4a2713aSLionel Sambuc   operator int(); // expected-note{{conversion to integral type}}
20*f4a2713aSLionel Sambuc };
21*f4a2713aSLionel Sambuc 
x()22*f4a2713aSLionel Sambuc void x() {
23*f4a2713aSLionel Sambuc   switch(A()) {
24*f4a2713aSLionel Sambuc   }
25*f4a2713aSLionel Sambuc }
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc enum E { e1, e2 };
28*f4a2713aSLionel Sambuc struct B : A {
29*f4a2713aSLionel Sambuc   operator E() const; // expected-note{{conversion to enumeration type}}
30*f4a2713aSLionel Sambuc };
31*f4a2713aSLionel Sambuc 
x2()32*f4a2713aSLionel Sambuc void x2() {
33*f4a2713aSLionel Sambuc   switch (B()) { // expected-error{{multiple conversions}}
34*f4a2713aSLionel Sambuc   }
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc struct C; // expected-note{{forward declaration}}
38*f4a2713aSLionel Sambuc 
x3(C & c)39*f4a2713aSLionel Sambuc void x3(C &c) {
40*f4a2713aSLionel Sambuc   switch (c) { // expected-error{{incomplete class type}}
41*f4a2713aSLionel Sambuc   }
42*f4a2713aSLionel Sambuc }
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc namespace test3 {
45*f4a2713aSLionel Sambuc   enum En { A, B, C };
foo()46*f4a2713aSLionel Sambuc   template <En how> void foo() {
47*f4a2713aSLionel Sambuc     int x = 0, y = 5;
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc     switch (how) { //expected-warning {{no case matching constant switch condition '2'}}
50*f4a2713aSLionel Sambuc     case A: x *= y; break;
51*f4a2713aSLionel Sambuc     case B: x += y; break;
52*f4a2713aSLionel Sambuc     // No case for C, but it's okay because we have a constant condition.
53*f4a2713aSLionel Sambuc     }
54*f4a2713aSLionel Sambuc   }
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc   template void foo<A>();
57*f4a2713aSLionel Sambuc   template void foo<B>();
58*f4a2713aSLionel Sambuc   template void foo<C>(); //expected-note {{in instantiation}}
59*f4a2713aSLionel Sambuc }
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc // PR9304 and rdar://9045501
click_check_header_sizes()62*f4a2713aSLionel Sambuc void click_check_header_sizes() {
63*f4a2713aSLionel Sambuc   switch (0 == 8) {  // expected-warning {{switch condition has boolean value}}
64*f4a2713aSLionel Sambuc   case 0: ;
65*f4a2713aSLionel Sambuc   }
66*f4a2713aSLionel Sambuc }
67*f4a2713aSLionel Sambuc 
local_class(int n)68*f4a2713aSLionel Sambuc void local_class(int n) {
69*f4a2713aSLionel Sambuc   for (;;) switch (n) {
70*f4a2713aSLionel Sambuc   case 0:
71*f4a2713aSLionel Sambuc     struct S {
72*f4a2713aSLionel Sambuc       void f() {
73*f4a2713aSLionel Sambuc         case 1: // expected-error {{'case' statement not in switch statement}}
74*f4a2713aSLionel Sambuc         break; // expected-error {{'break' statement not in loop or switch statement}}
75*f4a2713aSLionel Sambuc         default: // expected-error {{'default' statement not in switch statement}}
76*f4a2713aSLionel Sambuc         continue; // expected-error {{'continue' statement not in loop statement}}
77*f4a2713aSLionel Sambuc       }
78*f4a2713aSLionel Sambuc     };
79*f4a2713aSLionel Sambuc     S().f();
80*f4a2713aSLionel Sambuc     []{
81*f4a2713aSLionel Sambuc       case 2: // expected-error {{'case' statement not in switch statement}}
82*f4a2713aSLionel Sambuc       break; // expected-error {{'break' statement not in loop or switch statement}}
83*f4a2713aSLionel Sambuc       default: // expected-error {{'default' statement not in switch statement}}
84*f4a2713aSLionel Sambuc       continue; // expected-error {{'continue' statement not in loop statement}}
85*f4a2713aSLionel Sambuc     }();
86*f4a2713aSLionel Sambuc   }
87*f4a2713aSLionel Sambuc }
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc namespace Conversion {
90*f4a2713aSLionel Sambuc   struct S {
91*f4a2713aSLionel Sambuc     explicit operator int(); // expected-note {{conversion}}
92*f4a2713aSLionel Sambuc   };
f(T t)93*f4a2713aSLionel Sambuc   template<typename T> void f(T t) {
94*f4a2713aSLionel Sambuc     switch (t) { // expected-error {{explicit conversion}}
95*f4a2713aSLionel Sambuc     case 0:
96*f4a2713aSLionel Sambuc       return;
97*f4a2713aSLionel Sambuc     default:
98*f4a2713aSLionel Sambuc       break;
99*f4a2713aSLionel Sambuc     }
100*f4a2713aSLionel Sambuc   }
101*f4a2713aSLionel Sambuc   template void f(S); // expected-note {{instantiation of}}
102*f4a2713aSLionel Sambuc }
103