xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/condition.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   int x;
5*f4a2713aSLionel Sambuc   if (x) ++x;
6*f4a2713aSLionel Sambuc   if (int x=0) ++x;
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc   typedef int arr[10];
9*f4a2713aSLionel Sambuc   while (arr x={0}) ; // expected-error {{an array type is not allowed here}}
10*f4a2713aSLionel Sambuc   while (int f()=0) ; // expected-error {{a function type is not allowed here}}
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc   struct S {} s;
13*f4a2713aSLionel Sambuc   if (s) ++x; // expected-error {{value of type 'struct S' is not contextually convertible to 'bool'}}
14*f4a2713aSLionel Sambuc   while (struct S x=s) ; // expected-error {{value of type 'struct S' is not contextually convertible to 'bool'}}
15*f4a2713aSLionel Sambuc   do ; while (s); // expected-error {{value of type 'struct S' is not contextually convertible to 'bool'}}
16*f4a2713aSLionel Sambuc   for (;s;) ; // expected-error {{value of type 'struct S' is not contextually convertible to 'bool'}}
17*f4a2713aSLionel Sambuc   switch (s) {} // expected-error {{statement requires expression of integer type ('struct S' invalid)}}
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc   while (struct NewS *x=0) ;
20*f4a2713aSLionel Sambuc   while (struct S {} *x=0) ; // expected-error {{types may not be defined in conditions}}
21*f4a2713aSLionel Sambuc   while (struct {} *x=0) ; // expected-error {{types may not be defined in conditions}}
22*f4a2713aSLionel Sambuc   switch (enum {E} x=0) ; // expected-error {{types may not be defined in conditions}}
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc   if (int x=0) { // expected-note 2 {{previous definition is here}}
25*f4a2713aSLionel Sambuc     int x;  // expected-error {{redefinition of 'x'}}
26*f4a2713aSLionel Sambuc   }
27*f4a2713aSLionel Sambuc   else
28*f4a2713aSLionel Sambuc     int x;  // expected-error {{redefinition of 'x'}}
29*f4a2713aSLionel Sambuc   while (int x=0) int x; // expected-error {{redefinition of 'x'}} expected-note {{previous definition is here}}
30*f4a2713aSLionel Sambuc   while (int x=0) { int x; } // expected-error {{redefinition of 'x'}} expected-note {{previous definition is here}}
31*f4a2713aSLionel Sambuc   for (int x; int x=0; ) ; // expected-error {{redefinition of 'x'}} expected-note {{previous definition is here}}
32*f4a2713aSLionel Sambuc   for (int x; ; ) int x; // expected-error {{redefinition of 'x'}} expected-note {{previous definition is here}}
33*f4a2713aSLionel Sambuc   for (; int x=0; ) int x; // expected-error {{redefinition of 'x'}} expected-note {{previous definition is here}}
34*f4a2713aSLionel Sambuc   for (; int x=0; ) { int x; } // expected-error {{redefinition of 'x'}} expected-note {{previous definition is here}}
35*f4a2713aSLionel Sambuc   switch (int x=0) { default: int x; } // expected-error {{redefinition of 'x'}} expected-note {{previous definition is here}}
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc int* get_int_ptr();
39*f4a2713aSLionel Sambuc 
test2()40*f4a2713aSLionel Sambuc void test2() {
41*f4a2713aSLionel Sambuc   float *ip;
42*f4a2713aSLionel Sambuc   if (int *ip = ip) {
43*f4a2713aSLionel Sambuc   }
44*f4a2713aSLionel Sambuc }
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc // Make sure we do function/array decay.
test3()47*f4a2713aSLionel Sambuc void test3() {
48*f4a2713aSLionel Sambuc   if ("help")
49*f4a2713aSLionel Sambuc     (void) 0;
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc   if (test3) // expected-warning {{address of function 'test3' will always evaluate to 'true'}} \
52*f4a2713aSLionel Sambuc                 expected-note {{prefix with the address-of operator to silence this warning}}
53*f4a2713aSLionel Sambuc     (void) 0;
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
test4(bool (& x)(void))56*f4a2713aSLionel Sambuc void test4(bool (&x)(void)) {
57*f4a2713aSLionel Sambuc   while (x);
58*f4a2713aSLionel Sambuc }
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc template <class>
test5()61*f4a2713aSLionel Sambuc void test5() {
62*f4a2713aSLionel Sambuc   if (struct S {}* p = 0) // expected-error {{types may not be defined in conditions}}
63*f4a2713aSLionel Sambuc     ;
64*f4a2713aSLionel Sambuc }
test5_inst()65*f4a2713aSLionel Sambuc void test5_inst() {
66*f4a2713aSLionel Sambuc    test5<int>();
67*f4a2713aSLionel Sambuc }
68