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