xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/expressions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc void choice(int);
4f4a2713aSLionel Sambuc int choice(bool);
5f4a2713aSLionel Sambuc 
test()6f4a2713aSLionel Sambuc void test() {
7f4a2713aSLionel Sambuc   // Result of ! must be type bool.
8f4a2713aSLionel Sambuc   int i = choice(!1);
9f4a2713aSLionel Sambuc }
10f4a2713aSLionel Sambuc 
11f4a2713aSLionel Sambuc // rdar://8018252
f0()12f4a2713aSLionel Sambuc void f0() {
13f4a2713aSLionel Sambuc   extern void f0_1(int*);
14f4a2713aSLionel Sambuc   register int x;
15f4a2713aSLionel Sambuc   f0_1(&x);
16f4a2713aSLionel Sambuc }
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc namespace test1 {
bar(T & x)19f4a2713aSLionel Sambuc   template <class T> void bar(T &x) { T::fail(); }
bar(volatile T & x)20f4a2713aSLionel Sambuc   template <class T> void bar(volatile T &x) {}
21f4a2713aSLionel Sambuc 
test_ints()22f4a2713aSLionel Sambuc   void test_ints() {
23f4a2713aSLionel Sambuc     volatile int x;
24f4a2713aSLionel Sambuc     bar(x = 5);
25f4a2713aSLionel Sambuc     bar(x += 5);
26f4a2713aSLionel Sambuc   }
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc   enum E { E_zero };
test_enums()29f4a2713aSLionel Sambuc   void test_enums() {
30f4a2713aSLionel Sambuc     volatile E x;
31f4a2713aSLionel Sambuc     bar(x = E_zero);
32f4a2713aSLionel Sambuc     bar(x += E_zero); // expected-error {{incompatible type}}
33f4a2713aSLionel Sambuc   }
34f4a2713aSLionel Sambuc }
35f4a2713aSLionel Sambuc 
test2(int x)36f4a2713aSLionel Sambuc int test2(int x) {
37f4a2713aSLionel Sambuc   return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
38f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
39f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
40f4a2713aSLionel Sambuc 
41f4a2713aSLionel Sambuc   return x && sizeof(int) == 4;  // no warning, RHS is logical op.
42f4a2713aSLionel Sambuc   return x && true;
43f4a2713aSLionel Sambuc   return x && false;
44f4a2713aSLionel Sambuc   return x || true;
45f4a2713aSLionel Sambuc   return x || false;
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc   return x && (unsigned)0; // expected-warning {{use of logical '&&' with constant operand}} \
48f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
49f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   return x || (unsigned)1; // expected-warning {{use of logical '||' with constant operand}} \
52f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   return x || 0; // expected-warning {{use of logical '||' with constant operand}} \
55f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
56f4a2713aSLionel Sambuc   return x || 1; // expected-warning {{use of logical '||' with constant operand}} \
57f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
58f4a2713aSLionel Sambuc   return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
59f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
60f4a2713aSLionel Sambuc   return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
61f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
62f4a2713aSLionel Sambuc   return x && 0; // expected-warning {{use of logical '&&' with constant operand}} \
63f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
64f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
65f4a2713aSLionel Sambuc   return x && 1; // expected-warning {{use of logical '&&' with constant operand}} \
66f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
67f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
68f4a2713aSLionel Sambuc   return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
69f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
70f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
71f4a2713aSLionel Sambuc   return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
72f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
73f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
74f4a2713aSLionel Sambuc   return x || (0); // expected-warning {{use of logical '||' with constant operand}} \
75f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
76f4a2713aSLionel Sambuc   return x || (1); // expected-warning {{use of logical '||' with constant operand}} \
77f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
78f4a2713aSLionel Sambuc   return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
79f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
80f4a2713aSLionel Sambuc   return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
81f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
82f4a2713aSLionel Sambuc   return x && (0); // expected-warning {{use of logical '&&' with constant operand}} \
83f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
84f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
85f4a2713aSLionel Sambuc   return x && (1); // expected-warning {{use of logical '&&' with constant operand}} \
86f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
87f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
88f4a2713aSLionel Sambuc   return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
89f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
90f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
91f4a2713aSLionel Sambuc   return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
92f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
93f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
94f4a2713aSLionel Sambuc }
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc template<unsigned int A, unsigned int B> struct S
97f4a2713aSLionel Sambuc {
98f4a2713aSLionel Sambuc   enum {
99f4a2713aSLionel Sambuc     e1 = A && B,
100f4a2713aSLionel Sambuc     e2 = A && 7      // expected-warning {{use of logical '&&' with constant operand}} \
101f4a2713aSLionel Sambuc                      // expected-note {{use '&' for a bitwise operation}} \
102f4a2713aSLionel Sambuc                      // expected-note {{remove constant to silence this warning}}
103f4a2713aSLionel Sambuc   };
104f4a2713aSLionel Sambuc 
fooS105f4a2713aSLionel Sambuc   int foo() {
106f4a2713aSLionel Sambuc     int x = A && B;
107f4a2713aSLionel Sambuc     int y = B && 3;  // expected-warning {{use of logical '&&' with constant operand}} \
108f4a2713aSLionel Sambuc                      // expected-note {{use '&' for a bitwise operation}} \
109f4a2713aSLionel Sambuc                      // expected-note {{remove constant to silence this warning}}
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc     return x + y;
112f4a2713aSLionel Sambuc   }
113f4a2713aSLionel Sambuc };
114f4a2713aSLionel Sambuc 
test3()115f4a2713aSLionel Sambuc void test3() {
116f4a2713aSLionel Sambuc   S<5, 8> s1;
117f4a2713aSLionel Sambuc   S<2, 7> s2;
118f4a2713aSLionel Sambuc   (void)s1.foo();
119f4a2713aSLionel Sambuc   (void)s2.foo();
120f4a2713aSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc namespace pr16992 {
123*0a6a1f1dSLionel Sambuc   typedef int T;
getsz()124*0a6a1f1dSLionel Sambuc   unsigned getsz() {
125*0a6a1f1dSLionel Sambuc     return (sizeof T());
126*0a6a1f1dSLionel Sambuc   }
127*0a6a1f1dSLionel Sambuc }
128*0a6a1f1dSLionel Sambuc 
test4()129*0a6a1f1dSLionel Sambuc void test4() {
130*0a6a1f1dSLionel Sambuc   #define X 0
131*0a6a1f1dSLionel Sambuc   #define Y 1
132*0a6a1f1dSLionel Sambuc   bool r1 = X || Y;
133*0a6a1f1dSLionel Sambuc 
134*0a6a1f1dSLionel Sambuc   #define Y2 2
135*0a6a1f1dSLionel Sambuc   bool r2 = X || Y2; // expected-warning {{use of logical '||' with constant operand}} \
136*0a6a1f1dSLionel Sambuc                      // expected-note {{use '|' for a bitwise operation}}
137*0a6a1f1dSLionel Sambuc }
138