xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-self-assign.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wself-assign -verify %s
2f4a2713aSLionel Sambuc 
f()3f4a2713aSLionel Sambuc void f() {
4f4a2713aSLionel Sambuc   int a = 42, b = 42;
5f4a2713aSLionel Sambuc   a = a; // expected-warning{{explicitly assigning}}
6f4a2713aSLionel Sambuc   b = b; // expected-warning{{explicitly assigning}}
7f4a2713aSLionel Sambuc   a = b;
8f4a2713aSLionel Sambuc   b = a = b;
9f4a2713aSLionel Sambuc   a = a = a; // expected-warning{{explicitly assigning}}
10f4a2713aSLionel Sambuc   a = b = b = a;
11*0a6a1f1dSLionel Sambuc   a &= a; // expected-warning{{explicitly assigning}}
12*0a6a1f1dSLionel Sambuc   a |= a; // expected-warning{{explicitly assigning}}
13*0a6a1f1dSLionel Sambuc   a ^= a;
14f4a2713aSLionel Sambuc }
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc // Dummy type.
17f4a2713aSLionel Sambuc struct S {};
18f4a2713aSLionel Sambuc 
false_positives()19f4a2713aSLionel Sambuc void false_positives() {
20f4a2713aSLionel Sambuc #define OP =
21f4a2713aSLionel Sambuc #define LHS a
22f4a2713aSLionel Sambuc #define RHS a
23f4a2713aSLionel Sambuc   int a = 42;
24f4a2713aSLionel Sambuc   // These shouldn't warn due to the use of the preprocessor.
25f4a2713aSLionel Sambuc   a OP a;
26f4a2713aSLionel Sambuc   LHS = a;
27f4a2713aSLionel Sambuc   a = RHS;
28f4a2713aSLionel Sambuc   LHS OP RHS;
29f4a2713aSLionel Sambuc #undef OP
30f4a2713aSLionel Sambuc #undef LHS
31f4a2713aSLionel Sambuc #undef RHS
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc   S s;
34f4a2713aSLionel Sambuc   s = s; // Not a builtin assignment operator, no warning.
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc   // Volatile stores aren't side-effect free.
37f4a2713aSLionel Sambuc   volatile int vol_a;
38f4a2713aSLionel Sambuc   vol_a = vol_a;
39f4a2713aSLionel Sambuc   volatile int &vol_a_ref = vol_a;
40f4a2713aSLionel Sambuc   vol_a_ref = vol_a_ref;
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc 
g()43f4a2713aSLionel Sambuc template <typename T> void g() {
44f4a2713aSLionel Sambuc   T a;
45f4a2713aSLionel Sambuc   a = a; // May or may not be a builtin assignment operator, no warning.
46f4a2713aSLionel Sambuc }
instantiate()47f4a2713aSLionel Sambuc void instantiate() {
48f4a2713aSLionel Sambuc   g<int>();
49f4a2713aSLionel Sambuc   g<S>();
50f4a2713aSLionel Sambuc }
51